JDBC数据库连接

时间:2021-11-04 04:10:06

常规步骤:

  1.导入驱动jar包

    在java文件夹下新建一个Folder夹(lib),将驱动jar包放入,右键Build Path

  2.注册驱动

  3.获取连接对象

  4.获取语句执行对象

  5.执行sql语句

  6.释放资源(先开的后关)

  例:

 1     public static void main(String[] args) throws ClassNotFoundException, SQLException {
 2         //1.注册驱动
 3         Class.forName("com.mysql.jdbc.Driver");
 4         //2.获取连接对象
 5         String url = "jdbc:mysql://localhost:3303/java?characterEncoding=utf8";
 6         String username = "admin;
 7         String password = "123";
 8         Connection conn=DriverManager.getConnection(url,username,password);
 9         
10         //3.获取语句执行对象
11         Statement sta=conn.createStatement();
12         //4.执行sql
13         String sql="insert into sort(sname) values(‘口红‘)";
14         int row=sta.executeUpdate(sql);
15         System.out.println(row);
16         //6.释放资源(先开的后关)
17         sta.close();
18         conn.close();
19     
20     }

JDBC工具类

  建一个JDBC工具类,我们就不用在每次进行数据库操作时都要进行注册驱动,获取连接对象了,减少了代码的重复性。

  JDBCUtils.java

 1 public class JDBCUtils {
 2     //获取连接对象
 3     public static Connection getConn(){
 4         Connection conn=null;
 5                 try {
 6                     //1.注册驱动
 7                     Class.forName("com.mysql.jdbc.Driver");
 8                     //2.获取连接对象
 9                     String url = "jdbc:mysql://localhost:3303/java?characterEncoding=utf8";
10                     String username = "admin";
11                     String password = "123";
12                     conn=DriverManager.getConnection(url,username,password);
13                 } catch (ClassNotFoundException | SQLException e) {
14                     // TODO Auto-generated catch block
15                     e.printStackTrace();
16                 }
17                 return conn;
18             
19     }
20     //增删改释放资源
21     public static void close(PreparedStatement pst,Connection conn){
22         if(pst!=null){
23             try {
24                 pst.close();
25             } catch (SQLException e) {
26                 // TODO Auto-generated catch block
27                 e.printStackTrace();
28             }
29         }
30         if(conn!=null){
31             try {
32                 conn.close();
33             } catch (SQLException e) {
34                 // TODO Auto-generated catch block
35                 e.printStackTrace();
36             }
37         }
38     }
39     //查询释放资源、
40     public static void close(ResultSet rs, PreparedStatement pst,Connection conn){
41         if(rs!=null){
42             try {
43                 rs.close();
44             } catch (SQLException e) {
45                 // TODO Auto-generated catch block
46                 e.printStackTrace();
47             }
48         }
49         if(pst!=null){
50             try {
51                 pst.close();
52             } catch (SQLException e) {
53                 // TODO Auto-generated catch block
54                 e.printStackTrace();
55             }
56         }
57         if(conn!=null){
58             try {
59                 conn.close();
60             } catch (SQLException e) {
61                 // TODO Auto-generated catch block
62                 e.printStackTrace();
63             }
64         }
65     }
66 }

  然后在进行数据库操作:

 1     public int add(String sname) throws SQLException{
 2         //获取连接对象
 3         Connection conn=JDBCUtils.getConn();
 4         //获取语句执行对象
 5         String sql="insert into sort(sname) values(?)";
 6         PreparedStatement pst=conn.prepareStatement(sql);
 7         //执行sql语句
 8         pst.setString(1, sname);
 9         int row=pst.executeUpdate();
10         //释放资源
11         JDBCUtils.close(pst, conn);
12         return row;
13     }