Complete SQL query and put query results into Vector container for other programs to use
/* * Execute sql query statement*/public static <T> Vector<T> executeQuery(Class<T> clazz, String sql, Object... args) {Connection conn = null;PreparedStatement preparedstatement = null;ResultSet rs = null;Vector<T> vecRs = new Vector<T>();T obj = null;try {conn = JDBCTools.getConnection();preparedstatement = conn.prepareStatement(sql);// Use the sql statement to determine which columns are selected for (int i = 0; i < args.length; i++) {preparedstatement.setObject(i + 1, args[i]);}// Use sql query to obtain the result set// Use reflection to create an object of the entity class// Get the alias of the result street Stud_id Get the metadata of JDBC// Get the value of each column of the result set, and combine the previous step to get a Map key-value pair// Key: Column alias value: column value// Assign value to the attribute of the entity class object using reflection// The key value of the attribute Map is the value of the Map rs = preparedstatement.executeQuery();// Get the metadata ResultSetMetaData rsmd = rs.getMetaData();Map<String, Object> mapMetaData = new HashMap<String, Object>();// Print the column name of a column while (rs.next()) {// Get a row of data in the data table that meets the requirements and put it in the map for (int i = 0; i < rsmd.getColumnCount(); i++) {String columnLabel = rsmd.getColumnLabel(i + 1);Object columnValue = rs.getObject(columnLabel);// System.out.println(columnLabel); mapMetaData.put(columnLabel, columnValue);}//Initialize the T-type object through reflection if (mapMetaData.size() > 0) {obj = clazz.newInstance();for (Map.Entry<String, Object> entry : mapMetaData.entrySet()) {String fieldkey = entry.getKey();Object fieldvalue = entry.getValue();// System.out.println(fieldkey + ":" + fieldvalue); ReflectionUtils.setFieldValue(obj, fieldkey, fieldvalue);//Assign value through reflection}}//Load the object into the Vector container vecRs.add(obj);}}catch (Exception e) {e.printStackTrace();}return vecRs;}The tool-like methods used
Get database connection JDBCTools.getConnection()
/* * Get the connection to the database*/public static Connection getConnection() throws Exception {Connection conn = null;String driver = null;String jdbcUrl = null;String username = null;String password = null;// Get the Properties object Properties properties = new Properties();InputStream in = JDBCTools.class.getClassLoader().getResourceAsStream("jdbc.properties");properties.load(in);driver = properties.getProperty("driver");jdbcUrl = properties.getProperty("jdbcUrl");username = properties.getProperty("user");password = properties.getProperty("password");Class.forName(driver);conn = DriverManager.getConnection(jdbcUrl, username, password);return conn;}ReflectionUtils.setFieldValue(obj,fieldkey,fieldvalue);
Assign the fieldkey attribute of the obj object to fieldvalue
//Set the object's attributes public static void setFieldValue(Object obj,String fieldName,Object value){Field field=getDeclaredField(obj, fieldName);if(field==null){throw new IllegalArgumentException("Could not find field["+ fieldName+"] on target ["+obj+"]");}makeAccessible(field);try{field.set(obj, value);}catch(IllegalAccessException e){System.out.println("Exception that cannot be thrown");}}//Judge whether the modifier of the field is public, and change the access permission of the field accordingly, public static void makeAccessible(Field field){if(!Modifier.isPublic(field.getModifiers())){field.setAccessible(true);}}//Get the field attribute, the attribute may inherit public static Field in the parent class public static Field getDeclaredField(Object obj,String fieldName){for (Class<?> clazz=obj.getClass(); clazz!=Object.class; clazz=clazz.getSuperclass()){try{return clazz.getDeclaredField(fieldName);}catch(Exception e){}}return null;}Summarize
The above is the detailed explanation of the general methods of Java executing SQL statements to implement queries. I hope it will be helpful to everyone. Interested friends can continue to refer to other related topics on this site. If there are any shortcomings, please leave a message to point it out. Thank you friends for your support for this site!