Basic use of JDBC in JAVA

JDBC allows users to access any form of tabular data, especially data stored in relational databases.

The execution flow of JDBC is as follows:

  • Connect to data sources, such as databases;
  • Pass query and update instructions for the database (sql)
  • Process the database response and return the result (resultset)

Generally, native JDBC will encapsulate the database connection as a JdbcUtil, the example is as follows:

 1 package com.spring.utility;
 2 
 3 import java.sql.Connection;
 4 import java.sql.DriverManager;
 5 
 6 public class JdbcUtil {
 7     private static String DriverString = "com.mysql.jdbc.Driver";
 8     private static String URL = "jdbc:mysql://localhost:3306/数据库名称";
 9     private static String UserName = 用户名;
10     private static String Password = 密码;
11     public static Connection getConnection(){
12         try{
13             Class.forName(DriverString);
14             Connection cn = DriverManager.getConnection(URL, UserName, Password);
15             return cn;
16         }catch(Exception e){
17             e.printStackTrace();
18         }
19         return null;
20     }
21 }

Then according to the MVC framework, the tool class is generally called at the Dao layer:

  1 package com.spring.dao.impl;
  2 
  3 import java.sql.Connection;
  4 import java.sql.PreparedStatement;
  5 import java.sql.ResultSet;
  6 import java.sql.SQLException;
  7 import java.util.ArrayList;
  8 import java.util.List;
  9 
 10 import com.spring.dao.StudentDao;
 11 import com.spring.entity.StudentEntity;
 12 import com.spring.utility.JdbcUtil;
 13 
 14 /**
 15  * @author Administrator
 16  * 
 17  */
 18 public class StudentDaoImpl implements StudentDao{
 19 
 20     @Override
 21     public int insert(StudentEntity st){
 22         Connection cn = JdbcUtil.getConnection();
 23         int result = 0;
 24         String str = "insert into studententity(name,sex) values(?,?)";
 25         try {
 26             PreparedStatement prp = cn.prepareStatement(str);
 27             prp.setString(1, st.getName());
 28             prp.setInt(2, st.getSex());
 29             result = prp.executeUpdate();
 30         } catch (SQLException e) {
 31             e.printStackTrace();
 32         }finally{
 33             try {
 34                 cn.close();
 35             } catch (SQLException e) {
 36                 e.printStackTrace();
 37             }
 38         }
 39         
 40         return result;
 41     }
 42 
 43     @Override
 44     public int update(StudentEntity st) {
 45         Connection cn = JdbcUtil.getConnection();
 46         int result = 0;
 47         String str = "update studententity set name=?,sex=? where id=?";
 48         try {
 49             PreparedStatement prp = cn.prepareStatement(str);
 50             prp.setString(1, st.getName());
 51             prp.setInt(2, st.getSex());
 52             prp.setInt(3, st.getId());
 53             result = prp.executeUpdate();
 54         } catch (SQLException e) {
 55             e.printStackTrace();
 56         }finally{
 57             try {
 58                 cn.close();
 59             } catch (SQLException e) {
 60                 e.printStackTrace();
 61             }
 62         }
 63         return result;
 64     }
 65 
 66     @Override
 67     public List<StudentEntity> query(String st) {
 68         Connection cn = JdbcUtil.getConnection();
 69         String sql = st;
 70         List<StudentEntity> li = new ArrayList<StudentEntity>();
 71         try {
 72             PreparedStatement prp = cn.prepareStatement(sql);
 73             ResultSet rs = prp.executeQuery();
 74             while(rs.next()){
 75                 StudentEntity stu = new StudentEntity();
 76                 stu.setId(rs.getInt("id"));
 77                 stu.setName(rs.getString("name"));
 78                 stu.setSex(rs.getInt("sex"));
 79                 li.add(stu);
 80             }
 81             
 82         } catch (SQLException e) {
 83             e.printStackTrace();
 84         }
 85         return li;
 86     }
 87 
 88     @Override
 89     public int delete(String st) {
 90         Connection cn = JdbcUtil.getConnection();
 91         String sql = st;
 92         int result=0;
 93         try {
 94             PreparedStatement prp = cn.prepareStatement(sql);
 95             result = prp.executeUpdate(); 
 96             System.out.println("result="+result);
 97         } catch (SQLException e) {
 98             e.printStackTrace();
 99         }
100         return result;
101     }
102 
103 }

After completing the Dao layer, encapsulate the Service layer according to actual needs, and then call it in the Controller layer.

Here are the little mistakes that I tend to make:

1. The insert statement is not written correctly, the insert table (each field) values ​​(the value of each field)

2. When writing insert, if the database primary key is set to auto-increment, you do not need to insert the value in the statement; (otherwise an error will be reported)

3. Query query, when using resultset, the List in the result set List<Entity> uses the JAVA.util package instead of the java.awt package

4. According to the different methods of the precompiled class PreparedStatement, it is necessary to determine which return value is the successful execution; [this is just a note, not an error]

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324860474&siteId=291194637