The reason why JAVAWEB dbutils cannot find the content when executing the SQL command and traversing the result set is as follows:
When traversing the result set, only traversing the bean object will output only the first line of content (the first line is the object instantiated by the UserEntity class), so here you need re.getRepoTableName() to call the corresponding content through the object.
In this way, the value can be obtained
PS: JavaWeb's DBUtils is detailed as follows:
1. What are DBUtils and their functions
DBUtils is written by apache. DBUtils is a practical database operation tool in Java programming, small, simple and practical.
DBUtils encapsulates operations on JDBC, simplifying operations on JDBC. You can write less code.
1. For the data table reading operation, it can convert the results into Java collections such as List, Array, Set, etc., which is convenient for programmers to operate;
2. The writing operation of data tables is also very simple (just write SQL statements)
3. You can use data sources, use JNDI, database connection pooling and other technologies to optimize performance-reuse already built database connection objects
2. Three core objects of DBUtils
2.1. QueryRunner class
QueryRunner provides an API for operating SQL statements. It has three main methods: query() is used to execute select, update() is used to perform insert update delete, batch() is used to perform batch processing. The following will introduce the usage of these methods in detail.
2.2. ResultSetHandler interface
It is used to define how to encapsulate the result set after select operation. It has a total of 9 commonly used implementation classes. I will introduce in detail how to use it.
2.3. DbUtils class
It is a tool class that defines the method of closing resources and transaction processing
3. How to use DBUtils framework
3.1. Use steps
Import the corresponding jar package
Create a QueryRunner object
Use the query method to execute the select statement
Encapsulate result sets using ResultSetHandler
Use the DbUtils class to release resources
3.2. Example
Note: I am using the C3P0 connection pool
import java.sql.ResultSet;import java.sql.SQLException;import java.util.ArrayList;import java.util.List;import org.apache.commons.dbutils.QueryRunner;import org.apache.commons.dbutils.ResultSetHandler;import org.apache.commons.dbutils.handlers.BeanListHandler;import org.junit.Test;import com.jxlg.domain.User;public class TestSelect { @Test public void testSelect(){ //Create a QueryRunner object QueryRunner qr = new QueryRunner(C3P0Util.getDataSource()); try { // new ResultSetHandler<List<User>>Tell us how to encapsulate the result set List<User> list = qr.query("select * from user", new ResultSetHandler<List<User>>(){ @Override //After the query statement executes the select statement, the result is passed in the form of a return value public List<User> handle(ResultSet rs) throws SQLException { List<User> list = new ArrayList<User>(); while(rs.next()){ User u = new User(); u.setId(rs.getInt(1)); u.setUsername(rs.getString(2)); u.setPassword(rs.getString(3)); u.setEmail(rs.getString(4)); u.setBirthday(rs.getDate(5)); list.add(u); } return list; } }); for (User user : list) { System.out.println(user); } } catch (SQLException e) { e.printStackTrace(); }} @Test public void testSelect2(){ //Create a QueryRunner object QueryRunner qr = new QueryRunner(C3P0Util.getDataSource()); try { //Execute the sql statement and return the result List<User> list = qr.query("select * from user where id=? and username=?", new BeanListHandler<User>(User.class),1,"tom"); for (User user : list) { System.out.println(user); } } catch (SQLException e) { e.printStackTrace(); } }} 4. Detailed explanation of the three core objects of DBUtils
4.1. QueryRunner object
4.1.1. Constructor
new QueryRunner(); Its transactions can be controlled manually.
In other words, there must be a Connection object in the methods (such as query, update, batch) called by this object.
new QueryRunner(DataSource ds); Its transactions are automatically controlled. One SQL and one transaction.
There is no Connection object in the methods (such as query, update, batch) called by this object.
4.1.2. Common methods
4.2. ResultSetHandler interface
4.2.1. It has 9 result processors
ArrayHandler: Suitable for 1 record. Encapsulate each column of the record into an array Object[]
ArrayListHandler: Suitable for multiple records. Encapsulate each column value of each record into an array Object[], and encapsulate the array into a List ColumnListHandler: Take the data of a certain column. Encapsulated into List.
KeyedHandler: Take multiple records, each record is encapsulated into one map, and then encapsulate this map into another map, and the key is the specified field value.
MapHandler: Suitable for 1 record. Put the column name and column value of the current record in one Map MapListHandler: suitable for multiple records. Encapsulate each record into a map, and then encapsulate the map into a List ScalarHandler: suitable for single row and single column data BeanHandler
BeanListHandler
4.2.2. Example
import static org.junit.Assert.*;import java.sql.SQLException;import java.util.List;import java.util.Map;import java.util.Map.Entry;import org.apache.commons.dbutils.QueryRunner;import org.apache.commons.dbutils.handlers.ArrayHandler;import org.apache.commons.dbutils.handlers.BeanHandler;import org.apache.commons.dbutils.handlers.ColumnListHandler;import org.apache.commons.dbutils.handlers.KeyedHandler;import org.apache.commons.dbutils.handlers.MapHandler;import org.apache.commons.dbutils.handlers.MapListHandler;import org.apache.commons.dbutils.handlers.ScalarHandler;import org.junit.Test;import com.jxlg.domain.User;public class TestResultSetHandler { @Test public void test1() { //ArrayHandler: Suitable for 1 record. Encapsulate each column of the value of the record into an array Object[] QueryRunner qr = new QueryRunner(C3P0Util.getDataSource()); try { Object[] o = qr.query("select * from user where id=?", new ArrayHandler(),5); for (Object object : o) { System.out.println(object); } } catch (SQLException e) { e.printStackTrace(); } } @Test public void test2() throws SQLException { //ArrayListHandler: Suitable for multiple records. Encapsulate each column of each record into an array Object[], and encapsulate the array into a List QueryRunner qr = new QueryRunner(C3P0Util.getDataSource()); List<Object[]> list = qr.query("select * from user", new ArrayListHandler()); for (Object[] objects : list) { for (Object object : objects) { System.out.println(object); } System.out.println("----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- } } @Test public void test3() throws SQLException { //ColumnListHandler: Get the data of a certain column. Encapsulate into List QueryRunner qr = new QueryRunner(C3P0Util.getDataSource()); List<Object> list = qr.query("select username,password from user ", new ColumnListHandler(1)); for (Object object : list) { System.out.println(object); } } @Test public void test4() throws SQLException { //KeyedHandler: Take multiple records, each record is encapsulated into a map, //Package this map into another map, and the key is the specified field value. QueryRunner qr = new QueryRunner(C3P0Util.getDataSource()); //The key of the large map is a column of data in the table, and the key of the small map is the column name of the table, so the key of the large map uses the Object type and the String is the small. Map<Object, Map<String, Object>> map = qr.query("select * from user", new KeyedHandler(1)); for (Map.Entry<Object, Map<String,Object>> m : map.entrySet()) { System.out.println(m);//It is id to, because "1" is set. for (Map.Entry<String, Object> mm : m.getValue().entrySet()) { System.out.println(mm);//Take out the key and value in the small map } System.out.println("-------------------------------"); } } @Test public void test5() throws SQLException { //MapHandler: Suitable for 1 record. Put the column name and column value of the current record in a Map QueryRunner qr = new QueryRunner(C3P0Util.getDataSource()); Map<String, Object> map = qr.query("select * from user", new MapHandler()); for (Map.Entry<String, Object> m : map.entrySet()) { System.out.println(m.getKey()+"/t"+m.getValue()); //By default to get the first row of data, you need to go to other rows and add conditions} } @Test public void test6() throws SQLException { //MapListHandler: Suitable for multiple records. Encapsulate each record into a map, and then encapsulate the Map into List QueryRunner qr = new QueryRunner(C3P0Util.getDataSource()); List<Map<String, Object>> list = qr.query("select * from user", new MapListHandler()); for (Map<String, Object> map : list) { for (Map.Entry<String, Object> m : map.entrySet()) { System.out.println(m); } System.out.println("-------------"); } } @Test public void test7() throws SQLException { //ScalarHandler: Suitable for single row and single column data QueryRunner qr = new QueryRunner(C3P0Util.getDataSource()); Object o = qr.query("select * from user", new ScalarHandler(2)); System.out.println(o); } @Test public void test8() throws SQLException { //BeanHandler: Suitable for single row and single column data QueryRunner qr = new QueryRunner(C3P0Util.getDataSource()); User user = qr.query("select * from user", new BeanHandler<User>(User.class)); System.out.println(user); } }5. Use DBUtils to make an example of adding, deleting, modifying and searching
import static org.junit.Assert.*;import java.sql.SQLException;import java.util.Date;import javax.crypto.spec.OAEPParameterSpec;import org.apache.commons.dbutils.QueryRunner;import org.junit.Test;public class TestInCURD { @Test public void testInsert() { //Create a QueryRunner object QueryRunner qr = new QueryRunner(C3P0Util.getDataSource()); try { qr.update("insert into user (username,password,email,birthday)values(?,?,?,?)", "guapi","4646","[email protected]",new Date()); } catch (SQLException e) { e.printStackTrace(); } } @Test public void testUpdate() { //Create a QueryRunner object QueryRunner qr = new QueryRunner(C3P0Util.getDataSource()); try { qr.update("update user set username=?,password=? where id=4 ", "meizimeizi","520520"); } catch (SQLException e) { e.printStackTrace(); } } @Test public void testDelete() { //Create a QueryRunner object QueryRunner qr = new QueryRunner(C3P0Util.getDataSource()); try { qr.update("delete from user where id=? ",4); } catch (SQLException e) { e.printStackTrace(); } } @Test public void testBatch() { //Create a QueryRunner object QueryRunner qr = new QueryRunner(C3P0Util.getDataSource()); try { Object[][] params = new Object[10][]; //High-dimensional represents how many times the SQL statement is executed for(int i =0;i<params.length;i++){ params[i] = new Object[]{"guapi"+i,"4646","[email protected]",new Date()}; } qr.batch("insert into user (username,password,email,birthday)values(?,?,?,?)", params ); } catch (SQLException e) { e.printStackTrace(); } }} Summarize
The above is the reason why JavaWeb dbutils cannot be found when executing SQL commands and traversing the result set. I hope it will be helpful to everyone. If you have any questions, please leave me a message and the editor will reply to everyone in time. Thank you very much for your support to Wulin.com!