-
Notifications
You must be signed in to change notification settings - Fork 0
/
ClassConnect.java
65 lines (56 loc) · 1.72 KB
/
ClassConnect.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
package connectsql;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
public class Connect {
public Connection getConnection() {
Connection conn = null;
try{
String userName = "root";
String password = "";
String DatabaseName = "test";
String url = "jdbc:mysql://localhost/" + DatabaseName;
Class.forName ("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection(url, userName, password);
System.out.println("Ok");
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
return conn;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
Connection conn = null;
try{
//cmt
String userName = "root";
String password = "";
String DatabaseName = "test";
String url = "jdbc:mysql://localhost/" + DatabaseName;
Class.forName ("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection(url, userName, password);
System.out.println("Ok");
// String sql = "SELECT * FROM table21da";
// PreparedStatement stm = conn.prepareStatement(sql);
// ResultSet rs = stm.executeQuery();
// while(rs.next()) {
// System.out.println(rs.getInt(1) + "\t" + rs.getString(2));
// }
//
//
//
// sql = "INSERT INTO table21da(ID, Name) VALUES (913, 'SJDFJDSJFSDJ')";
// stm = conn.prepareStatement(sql);
// stm.execute();
//
// sql = "INSERT INTO table21da(ID, Name) VALUES (897, 'alophuday')";
// stm = conn.prepareStatement(sql);
// stm.execute();
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
}
}