Although object-relational mapping is relatively simple and basic, it is the foundation of the persistence layer framework, so it still needs to be recorded.
When inserting, there is a problem of garbled characters. I have never encountered it before. This time I also write a summary. I will not put it here for the time being. I am afraid it will be too long.
Insert an object into the database
package ORM; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.SQLException; /** * * Insert an object into the database * @author zhuang * */ public class TestJdbcInsert { static void add(User user) { try { Class.forName("com.mysql.jdbc.Driver"); } catch (ClassNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace (); } // use precompiled sql String sql = "insert into user values(?,?,?)"; try ( Connection c = DriverManager.getConnection( "jdbc:mysql://127.0.0.1:3306/jdbc?characterEncoding=UTF-8", "root", "123"); PreparedStatement ps = c.prepareStatement(sql); ) { ps.setInt(1, user.id); ps.setString(2, user.name); ps.setInt(3, user.age); // Use execute or executeUpdate here ps.executeUpdate(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace (); } } public static void main(String[] args) { User user = new User(3, "DZ", 23); add(user); } }
Delete the data corresponding to this User object
package ORM; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.SQLException; /** * Delete the data corresponding to this User object * * @author zhuang * */ public class TestJdbcDelete { static void delete(User user) { try { Class.forName("com.mysql.jdbc.Driver"); } catch (ClassNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace (); } // use precompiled sql String sql = "delete from user where id=?"; try ( Connection c = DriverManager.getConnection( "jdbc:mysql://127.0.0.1:3306/jdbc?characterEncoding=UTF-8", "root", "123"); PreparedStatement ps = c.prepareStatement(sql); ) { ps.setInt(1, user.id); // Use execute or executeUpdate here ps.execute(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace (); } } public static void main(String[] args) { User user = new User(2, "凌志玲", 33); delete(user); } }
Update this User object
package ORM; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.SQLException; /** * Update this User object * * @author zhuang * */ public class TestJdbcUpdate { static void update(User user) { try { Class.forName("com.mysql.jdbc.Driver"); } catch (ClassNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace (); } // use precompiled sql String sql = "update user set name=?,age=? where id=?"; try ( Connection c = DriverManager.getConnection( "jdbc:mysql://127.0.0.1:3306/jdbc?characterEncoding=UTF-8", "root", "123"); PreparedStatement ps = c.prepareStatement(sql); ) { ps.setString(1, user.name); ps.setInt(2, user.age); ps.setInt(3, user.id); // Use execute or executeUpdate here ps.execute(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace (); } } public static void main(String[] args) { User user = new User(1, "Chen Guanxi", 37); update(user); } }
//Get a user object based on the id value
package ORM; import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; /** * //Get a user object according to the id value * * @author zhuang * */ public class TestJdbcGetById { static User user; /** * How to get the user object * * @param id * @return */ static User getUser(int id) { try { // open the driver Class.forName("com.mysql.jdbc.Driver"); } catch (ClassNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace (); } try ( // Get the connection through the driver Connection c = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/jdbc", "root", "123"); Statement s = c.createStatement();) { String sql = "select * from user where id=" + id; ResultSet rs = s.executeQuery(sql); if (rs.next()) { String name = rs.getString("name"); int age = rs.getInt("age"); user = new User(id, name, age); } } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace (); } return user; } public static void main(String[] args) { User u = getUser(1); System.out.println(u.name); } }
Query all User data, convert it to User object, and put it back in a collection
package ORM; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.util.ArrayList; import java.util.List; /** * Query all User data, convert it to User object, and return it in a collection * @author zhuang * */ public class TestJdbcList { static List<User> getList(User user) { // prepare a collection List<User> userList = new ArrayList<>(); try { Class.forName("com.mysql.jdbc.Driver"); } catch (ClassNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace (); } // use precompiled sql String sql = "select * from user"; try ( Connection c = DriverManager.getConnection( "jdbc:mysql://127.0.0.1:3306/jdbc?characterEncoding=UTF-8", "root", "123"); PreparedStatement ps = c.prepareStatement(sql); ) { ps.execute(); ResultSet rs = ps.getResultSet(); //get the collection while(rs.next()) { // Load the fields into the User object user = new User(rs.getInt("id"), rs.getString("name"), rs.getInt("age")); // put the user object into the list userList.add(user); } } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace (); } return userList; } public static void main(String[] args) { User user = new User(1, "", 1); List<User> listUser = getList(user); System.out.println(listUser); } }