Complete reference twelve steps, where the default database has been created with the library table.
Frame showing:
2: SRC --- "Test below to create a class: BaseDao
This class there are two main methods.
1: query method prepareStatement
2: Non-query method (additions and deletions) the executeUpdate
Note points: First: JDBC_DRIVER connect the drive, where the need to see the version of Mysql.
If it is above the 8.0 version, you need to use com.mysql.cj.jdbc.Driver
This version is mysql5.7 so used are: com.mysql.jdbc.Driver
Two: Class.forName (JDBC_DRIVER); this is to load the driver, and a need try catch ClassNotFoundException Throws
Three: DB_URL This is the connection address mysql. mytest is the database name
Four: USER: mysql database account change
Five: PASS: Database Password
Three: the underlying database connection classes:
package Test; import java.sql.*; public class BaseDao { / *** * * @author database connection class * * / private String JDBC_DRIVER = "com.mysql.jdbc.Driver"; private String DB_URL = "jdbc: mysql: // localhost: 3306 / mytest"; // --------- database table to write their own name, as long as the database table name, like here on the line private String USER = "root"; // ---------- Name your own database private String PASS = "123456"; // ----------- your own password database Connection conn = null; / **** @param open the database * / protected Connection getConnetconn() { conn = null; try { Class.forName(JDBC_DRIVER); // open connection System.out.println ( "Database connection ....."); conn = DriverManager.getConnection(DB_URL, USER, PASS); } catch (ClassNotFoundException e) { e.printStackTrace (); } catch (SQLException e) { e.printStackTrace (); } return conn; } / **** @param close the connection * / protected void closeAll(Connection conn, PreparedStatement ps, ResultSet rs) { if (rs != null) try { if (rs != null) rs.close(); if (ps != null) ps.close(); if (conn != null) conn.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace (); } } / *** @param CRUD method * @param accept parameters for the SQL statement and the array of objects * @return returns the number of rows affected * / public int executeUpdate(String sql ,Object []ob) { conn = getConnetconn(); PreparedStatement ps = null; try { ps = prepareStatement(conn, sql, ob); int i = ps.executeUpdate(); return i; } catch (SQLException e) { // TODO Auto-generated catch block // e.printStackTrace (); return 0; } finally { closeAll(conn, ps, null); } } /// *** * inquiry method * / protected PreparedStatement prepareStatement(Connection conn, String sql, Object[] ob) { PreparedStatement ps = null; try { int index = 1; ps = conn.prepareStatement(sql); if (ps != null && ob != null) { for (int i = 0; i < ob.length; i++) { ps.setObject(index, ob[i]); index++; } } } catch (SQLException e1) { e1.printStackTrace (); } return ps; } }
Four: Create a view class, in fact, does not create a class change can be achieved Demo, in order to more intuitive, you can create a class, the class name: UserInfoModel
package Test; public class UserInfoModel { private int userid; private String username; private String useraddr; private String usertel; public int getUserid() { return userid; } public void setUserid(int id) { this.userid = id; } public String getUsername() { return username; } public void setUsername(String username){ this.username = username; } public String getUseraddr() { return useraddr; } public void setUseraddr(String useraddr) { this.useraddr = useraddr; } public String getUsertel() { return usertel; } public void setUsertel(String usertel) { this.usertel = usertel; }
Five: calling the bottom three methods to achieve CRUD
Here: extends the parent class is inherited BaseDao
package Test; import java.sql.*; import java.util.ArrayList; import java.util.List; public class UserInfoDao extends BaseDao{ / * Query methods --- --- * / public List<UserInfoModel> search(String sql,Object...params){ List<UserInfoModel> list =new ArrayList<UserInfoModel>(); Connection conn=this.getConnetconn(); PreparedStatement pst=null; ResultSet rs=null; try { pst=this.prepareStatement(conn, sql, params); rs=pst.executeQuery(); while(rs.next()){ UserInfoModel wor=new UserInfoModel(); wor.setUserid(rs.getInt(1)); wor.setUsername(rs.getString(2)); wor.setUseraddr(rs.getString(3)); wor.setUsertel(rs.getString(4)); list.add(wor); } } catch (SQLException e) { e.printStackTrace (); }finally{ closeAll(conn, pst, rs); } return list; } / * New method * / public int insert(){ UserInfoModel t=new UserInfoModel(); t.setUseraddr("shanghai"); t.setUsername ( "King of Five"); t.setUsertel("13524696896"); String str="INSERT INTO user_info ( user_name,user_addr,user_tel) VALUE(?,?,?)"; return executeUpdate(str, new Object[]{t.getUsername(),t.getUseraddr(),t.getUsertel()}); } /*Modification method*/ public int update() { UserInfoModel r=new UserInfoModel(); r.setUseraddr ( "Nanjing"); r.setUsername ( "test"); r.setUsertel("13524698886"); r.setUserid(7); String sql="UPDATE user_info SET `user_name`=?,`user_addr`=?,`user_tel`=? WHERE user_id=?"; return executeUpdate(sql, new Object[]{r.getUsername(),r.getUseraddr(),r.getUsertel(),r.getUserid()}); } / * Delete method * / public int delete() { UserInfoModel e=new UserInfoModel(); String sql="DELETE FROM `user_info` WHERE user_id=?"; e.setUserid (7); return executeUpdate(sql, new Object[]{e.getUserid()}); } }
Six: In the console method calls inside the main method Main:
package Test; import Test.Common; public class Main { public static void main(String[] args) {
UserInfoDao gave UserInfoDao = new (); dao.search("select user_id, user_name,user_addr,user_tel from user_info"); int insert= dao.insert(); System.out.println ( "an expanding number of success:" + insert); int update=dao.update(); System.out.println ( "successfully modified several:" + update); int delete =dao.delete(); System.out.println ( "deleted successfully several:" + delete);
}
Seven: finished