JDBC_1_JDBC连接数据库步骤

 1 public static void main(String[] args) throws SQLException {
 2         //0.导入mysql的数据库驱动,注意5和8版本的不同使用,一下以5.X为例
 3 
 4         Connection connection = null;
 5         Statement statement = null;
 6 
 7         try {
 8             //1.加载数据库驱动
 9             Class.forName("com.mysql.jdbc.Driver");//JDK5之后此行代码可以省略
10 
11             //2.创建数据库连接
12             String url = "jdbc:mysql://127.0.0.1:3306/db2019";
13             String userName = "root";
14             String passWord = "root";
15             connection = DriverManager.getConnection(url, userName, passWord);
16 
17             //3.定义sql语句
18             String sql = "update payment set serual = 1111 where id = 2";
19 
20             //4.创建sql执行者
21             statement = connection.createStatement();
22 
23             //5.执行sql
24             int count = statement.executeUpdate(sql);//此时返回值count是受影响的行数
25 
26             //6.处理结果集
27             System.out.println(count);
28 
29         } catch (Exception e) {
30             e.printStackTrace();
31         }finally {
32             //7.释放资源
33             statement.close();//后使用的先关闭
34             connection.close();
35         }
36     }

 

posted @ 2022-11-11 16:41  叹叹  阅读(16)  评论(0编辑  收藏  举报