java中JDBC应用示例实验
时间:2021-07-01 10:21:17
帮助过:42人阅读
package com.baorant;
2
3 import java.sql.Connection;
4 import java.sql.DriverManager;
5 import java.sql.PreparedStatement;
6 import java.sql.SQLException;
7
8 public class JavaDemo {
9
10 public static void main(String[] args)
throws Exception {
11 String username = "root"
;
12 String password = "li19941101"
;
13 String url = "jdbc:mysql://localhost:3306/mvc_user"
;
14 Connection conn;
15 try {
16 //1.加载数据库驱动
17 Class.forName("com.mysql.jdbc.Driver"
);
18 conn =
DriverManager.getConnection(url, username, password);
19 System.out.println("数据库连接成功!"
);
20
21 //第二步:操作数据库;
22 String sql=" insert into user_info ( user_id,user_name,password) values (5,‘song‘,‘ss‘) "
;
23 PreparedStatement pt=
conn.prepareStatement( sql );
24 pt.executeUpdate( );
25 //第三步:关闭数据库;
26 conn.close();
27 System.out.println("数据库操作成功!"
);
28 }
catch (SQLException e) {
29 // TODO Auto-generated catch block
30 e.printStackTrace();
31 }
32 }
33 }
java中JDBC应用示例实验
标签:logs 操作 div nts 数据库连接 ati rate stack 最新