-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJdbcDemo02.java
More file actions
54 lines (49 loc) · 1.51 KB
/
Copy pathJdbcDemo02.java
File metadata and controls
54 lines (49 loc) · 1.51 KB
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
package jdbc;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Connection;
import java.sql.Statement;
/**
* @author : CodeWater
* @create :2022-03-08-16:09
* @Function Description :crud练习
* account表 添加一条记录 insert 语句
*/
public class JdbcDemo02 {
public static void main(String[] args) {
Statement stmt = null;
Connection conn = null;
try {
Class.forName("com.mysql.jdbc.Driver");
String sql = "insert into account values( null , 'zhaoliu' , 3000 )";
conn = DriverManager.getConnection("jdbc:mysql:///db3", "root", "root");
stmt = conn.createStatement();
int count = stmt.executeUpdate(sql);
System.out.println(count);
if (count > 0) {
System.out.println("添加成功!");
} else {
System.out.println("添加失败");
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
} finally {
if (stmt != null) {
try {
stmt.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (conn != null) {
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
}