我是靠谱客的博主 昏睡绿茶,这篇文章主要介绍jdbc篇第2课:使用jdbc操作数据库,现在分享给大家,希望可以做个参考。

  上节课我们成功的连接了数据库,这节课我们试试来用java操作数据库

 

  想要操作数据库,必定要借助sql,java.sql包里提供了一些类和接口来方便我们执行sql语句

 

Demo 01:

复制代码
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
package jdbc; import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException; import java.sql.Statement; //jdbc 操作 mysql 教学 public class JdbcOperateMysqlTeach {     public static void main(String[] args) {         /**          * jdbc 操作 mysql步骤:          * 1. 连接数据库,获得Connection对象          * 2. 使用Connection对象创建Statement对象          * 3. 写好sql          * 4. 使用Statement对象执行sql          * 5. 关闭连接          */         try {             //1 连接数据库,获得Connection对象             Class.forName("com.mysql.jdbc.Driver");             String url = "jdbc:mysql://localhost:3306/teach";             String user = "root";             String password = "root";             Connection connection = DriverManager.getConnection(url,user,password);             //2. 使用Connection对象创建Statement对象             //调用Connection对象的createStatement()方法即可创建一个Statement对象             //注意,只有使用这种方式创建的Statement对象在能操作connection对象连接的数据库             Statement statement = connection.createStatement();             //3. 写好sql             String sql = "insert into t_user(name,age) values ('zhangsan',null);";             //4. 使用Statement对象执行sql             //注意:             //4.1 当sql语句是 insert、update、delete语句时使用executeUpdate()方法             //4.2 当sql语句时 select语句时使用executeQuery()方法             statement.executeUpdate(sql);             //5. 关闭连接             connection.close();         } catch (ClassNotFoundException e) {             e.printStackTrace();         } catch (SQLException e) {             e.printStackTrace();         }     } }

 

运行前:

 

运行后:

 

插入成功

 

Demo 02:

删除zhangsan

 

复制代码
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
package jdbc; import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException; import java.sql.Statement; //jdbc 操作 mysql 教学 public class JdbcOperateMysqlTeach {     public static void main(String[] args) {         /**          * jdbc 操作 mysql步骤:          * 1. 连接数据库,获得Connection对象          * 2. 使用Connection对象创建Statement对象          * 3. 写好sql          * 4. 使用Statement对象执行sql          * 5. 关闭连接          */         try {             //1 连接数据库,获得Connection对象             Class.forName("com.mysql.jdbc.Driver");             String url = "jdbc:mysql://localhost:3306/teach";             String user = "root";             String password = "root";             Connection connection = DriverManager.getConnection(url,user,password);             //2. 使用Connection对象创建Statement对象             //调用Connection对象的createStatement()方法即可创建一个Statement对象             //注意,只有使用这种方式创建的Statement对象在能操作connection对象连接的数据库             Statement statement = connection.createStatement();             //3. 写好sql             String sql = "delete from t_user where id = 2;";             //4. 使用Statement对象执行sql             //注意:             //4.1 当sql语句是 insert、update、delete语句时使用executeUpdate()方法             //4.2 当sql语句时 select语句时使用executeQuery()方法             statement.executeUpdate(sql);                          //5. 关闭连接             connection.close();         } catch (ClassNotFoundException e) {             e.printStackTrace();         } catch (SQLException e) {             e.printStackTrace();         }     } }

 

执行前:

 

执行后:

 

下节课讲查询。

最后

以上就是昏睡绿茶最近收集整理的关于jdbc篇第2课:使用jdbc操作数据库的全部内容,更多相关jdbc篇第2课内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(64)

评论列表共有 0 条评论

立即
投稿
返回
顶部