1. General query operation for customers table
CustomerForQuery
package com.aff.PreparedStatement; import java.lang.reflect.Field; import java.sql.Connection; import java.sql.Date; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.ResultSetMetaData; import org.junit.Test; import com.aff.bean.Customer; import com.aff.utils.JDBCUtils; // Query operations on the Customers table public class CustomerForQuery { @Test public void testqueryForCustomers() { String sql = "select id,name,email from customers where id =?"; Customer customer = queryForCustomers(sql, 12); System.out.println(customer); } // General query operation for customers table public Customer queryForCustomers (String sql, Object ... args) { Connection conn = null; PreparedStatement ps = null ; // Result set ResultSet rs = null ; try { conn = JDBCUtils.getConnection (); // Pre-compiled ps = conn.prepareStatement (sql); // Padded placeholder for ( int i = 0; i <args.length; i ++ ) { ps.setObject(i + 1, args[i]); } rs = ps.executeQuery (); // Get the result set metadata ResultSetMetaData ResultSetMetaData rsmd = rs.getMetaData (); // Get the number of columns in the result set through ResultSetMetaData int columnCount = rsmd.getColumnCount (); if (rs.next ()) { // The object can be transferred once, one piece of data, only one object is created Customer cust = new Customer (); for ( int i = 0; i <columnCount; i ++ ) { // Process the result set, that is, the table Each column in a row in Object Columvalue = rs.getObject (i + 1);// Get column value String columName = rsmd.getColumnName (i + 1); // Get column name // The columnName attribute specified for the cust object is assigned a columnvalue, which is done by reflection. // Get the attribute named columnName, because the column names and attributes in the table are the corresponding Field field = Customer. Class .getDeclaredField (columName); / / The same property as the column name field.setAccessible ( true ); // May be private, this setting makes it accessible // Set the value of this property name to the current cust, and assign the value to columnvalue field.set (cust, columvalue); } return cust; } } catch (Exception e) { e.printStackTrace (); } finally { JDBCUtils.closeResource(conn, ps, rs); } return null; } @Test public void tesQuery() { Connection conn = null; PreparedStatement ps = null ; // Execute and return a result set ResultSet rs = null ; try { conn = JDBCUtils.getConnection(); String sql = "select id ,name, email,birth from customers where id =?"; ps = conn.prepareStatement(sql); ps.setObject(1, 1); rs = ps.executeQuery (); // Process the result set if (rs.next ()) { // Determine whether the next item in the result set has data, if there is data, return true and move the pointer down,
// return a false pointer Without moving down, directly end int id = rs.getInt (1 ); String name = rs.getString(2); String email = rs.getString(3); Date birth = rs.getDate(4); // 方式一 // System.out.println("id = " + id + ",name = " + name + ",email // = " // + email + ",birth = " + birth); // 方式二 // Object[] data = new Object[] { id, name, email, birth }; // 方式三 Customer customer = new Customer(id, name, email, birth); System.out.println(customer); } } catch (Exception e) { e.printStackTrace (); } // Close resource JDBCUtils.closeResource (conn, ps, rs); } }
2. General query for Order table
OrderForQuery
package com.aff.PreparedStatement; import java.lang.reflect.Field; import java.sql.Connection; import java.sql.Date; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.ResultSetMetaData; import org.junit.Test; import com.aff.bean.Order; import com.aff.utils.JDBCUtils; // General query for Order table public class OrderForQuery { / * * For the case where the field name of the table is different from the attribute name of the class * 1. When SQL must be declared, the attribute name of the class used is used to name the field's alias * 2. When using ResultSetMetaData, you need to use getColumnLabel () to replace getColumnName () method to get the class alias * Note: There is no alias for the field in sql, then getColumnLabel () gets the column name */ @Test public void testorderForQuery() { String sql = "select order_id orderId ,order_name orderName,order_date orderDate from `order` where order_id = ?"; Order order = orderForQuery(sql, 1); System.out.println(order); } // General query for Order table public Order orderForQuery (String sql, Object ... args) { Connection conn = null; PreparedStatement ps = null; ResultSet rs = null; try { conn = JDBCUtils.getConnection (); // Execute, get result set ps = conn.prepareStatement (sql); // Fill placeholder for ( int i = 0; i <args.length; i ++ ) { ps.setObject(i + 1, args[i]); } rs = ps.executeQuery (); // Get the metadata of the result set ResultSetMetaData rsmd = rs.getMetaData (); // Get the number of columns int columnCount = rsmd.getColumnCount (); if (rs.next ()) { Order order = new Order (); for ( int i = 0; i <columnCount; i ++ ) { // Get the column value of each column Object columnValue = rs.getObject (i + 1 ); // Get the column name of the column, the column number column name is used to modify the ResultSet (result set), // String columnName = rsmd.getColumnName (i + 1);- // It is not recommended // to get the column instead Alias String ColumnLabel = rsmd.getColumnLabel (i + 1 ); // The property of the object named columnName is assigned to the specified value columnValue by reflection // First get the class Field field = Order. Class .getDeclaredField (ColumnLabel); field.setAccessible(true); field.set(order, columnValue); } return order; } } catch (Exception e) { e.printStackTrace (); } finally { JDBCUtils.closeResource(conn, ps, rs); } return null; } @Test public void testQuery1() { Connection conn = null; PreparedStatement ps = null; ResultSet rs = null; try { conn = JDBCUtils.getConnection(); String sql = "select order_id, order_name,order_date from `order` where order_id = ?"; ps = conn.prepareStatement (sql); // Query id = 1 data ps.setObject (1, 1 ); rs = ps.executeQuery (); if (rs.next ()) { // One row and three columns of data in the corresponding table int id = ( int ) rs.getObject (1 ); String name = (String) rs.getObject(2); Date date = (Date) rs.getObject(3); Order order = new Order(id, name, date); System.out.println(order); } } catch (Exception e) { e.printStackTrace (); } finally { JDBCUtils.closeResource(conn, ps, rs); } } }
Customer
package com.aff.bean; import java.sql.Date; // ORM programming ideas Object relational mapping // A data table corresponds to a java class // A record in the table corresponds to an object of the java class // A field in the table corresponds to an attribute of the java class // java.sql.Date -------------- Date sql type public class Customer { private int id; private String name; private String email; private Date birth; public Customer () { super (); // TODO Auto -generated constructor stub } public Customer ( int id, String name, String email, Date birth) { super(); this.id = id; this.name = name; this.email = email; this.birth = birth; } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getEmail() { return email; } public void setEmail(String email) { this.email = email; } public Date getBirth() { return birth; } public void setBirth(Date birth) { this.birth = birth; } @Override public String toString() { return "Customer [id=" + id + ", name=" + name + ", email=" + email + ", birth=" + birth + "]"; } }
Order
package com.aff.bean; import java.sql.Date; public class Order { private int orderId; private String orderName; private Date orderDate; public Order() { super(); } public Order(int orderId, String orderName, Date orderDate) { super(); this.orderId = orderId; this.orderName = orderName; this.orderDate = orderDate; } public int getOrderId() { return orderId; } public void setOrderId(int orderId) { this.orderId = orderId; } public String getOrderName() { return orderName; } public void setOrderName(String orderName) { this.orderName = orderName; } public Date getOrderDate() { return orderDate; } public void setOrderDate(Date orderDate) { this.orderDate = orderDate; } @Override public String toString() { return "Order [orderId=" + orderId + ", orderName=" + orderName + ", orderDate=" + orderDate + "]"; } }