-
Notifications
You must be signed in to change notification settings - Fork 0
/
Connect.java
36 lines (30 loc) · 997 Bytes
/
Connect.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
package ConnectSQL;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
public class Connect {
public static void main(String[] args) {
// TODO Auto-generated method stub
Connection conn = null;
try{
String userName = "root";
String password = "";
String url = "jdbc:mysql://localhost/test";
Class.forName ("com.mysql.cj.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();
} catch(Exception e){
System.out.println(e.getMessage());
}
}
}