4. Execute the query by ResultSet (Silicon Valley, yet notes)

ResultSet: result set, encapsulates the result of the query using JDBC

1. Call Statement object's executeQuery (sql) can get results

2.ResultSet return is actually a data table, a pointer to the first data table in front of the same, can be called next () method detects the next line is valid, the method returns true if effective, and the pointer down, Iterator objects corresponding to the combination hasNext () and next () method.

3. When referring to a line for a bit, the value of each column can be acquired by calling getXxx (index) or getXxx (conlumnName).

4.ResultSet also needs to close.

public static void select(String sql) {
	Connection connection=null;
	Statement statement=null;
	ResultSet resultset=null;
	try {
		//1.获取Connection
		connection=getConnection();
		//2.获取Statement
		statement=connection.createStatement();
		//3.执行查询,得到ResultSet
		resultset=statement.executeQuery(sql);
		while(resultset.next()) {
			String id=resultset.getString(1);
			String username=resultset.getString(2);
			String password=resultset.getString("password");
			System.out.println(id);
			System.out.println(username);
			System.out.println(password);
		}
	}catch(Exception e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}finally {
		release(resultset,statement,connection);
	}
}

() To create the object by calling Statement object excuteQuery method.

ResultSet object encapsulates the result set to perform database operations as a logical table, ResultSet interface is implemented by the database vendor.

ResultSet object maintains a pointer to the current cursor line data, the initial time, the cursor before the first row, through the ResultSet object Next () method to move to the next line.

ResultSet common interface methods:

boolean next()

getString()

......

Published 90 original articles · won praise 48 · views 10000 +

Guess you like

Origin blog.csdn.net/Asher_S/article/details/90243685
Recommended