|
/**
* Inquiry
* @author Administrator
*
*/
Public class Dbutilsquery {
Create a data source
Static DataSource ds;
Static {
ds = new combopooleddatasource ();
}
1. Arrayhandler: The query returns the first row of the result set, and the final return is a object[]
@Test
Public void Queryone () {
String sql = "SELECT * from admin;";
To create a core processing class
Queryrunner runner = new Queryrunner (DS);
Try {
Execute Query
object[] obj = (object[]) runner.query (SQL, new Arrayhandler ());
System.out.println (obj);
Catch (SQLException e) {
E.printstacktrace ();
}
}
2. Arraylisthandler: Get all the results and return list<object[]>
@Test
Public void Queryall () {
String sql = "SELECT * from admin;";
To create a core processing class
Queryrunner runner = new Queryrunner (DS);
Try {
Execute Query
list<object[]> obj = (list<object[]>) runner.query (SQL, new Arraylisthandler ());
System.out.println (obj.get (0) [1]);
Catch (SQLException e) {
E.printstacktrace ();
}
}
3. Beanhandler: Encapsulates the first line of the query result to the specified JavaBean object
@Test
Public void Getonebean () {
String sql = "SELECT * from admin;";
To create a core processing class
Queryrunner runner = new Queryrunner (DS);
Try {
Object obj = runner.query (sql, new Beanhandler (ordermeal). Class));
System.out.println (obj);
Catch (SQLException e) {
E.printstacktrace ();
}
}
4. Beanlisthandler: Encapsulates each row of the result set as the specified JavaBean object, and then adds the object to the ArrayList collection
@Test
Public void Getallbean () {
String sql = "SELECT * from admin;";
To create a core processing class
Queryrunner runner = new Queryrunner (DS);
Try {
Object obj = runner.query (sql, new Beanlisthandler (ordermeal). Class));
System.out.println (obj);
Catch (SQLException e) {
E.printstacktrace ();
}
}
5. Columnlisthandler to query the specified columns of all records, add the collection, or add the values of the first column to the collection if no columns are specified
@Test
Public void GetColumn () {
String sql = "SELECT * from admin;";
To create a core processing class
Queryrunner runner = new Queryrunner (DS);
Try {
Object obj = runner.query (sql, new columnlisthandler ("user"));
System.out.println (obj);
Catch (SQLException e) {
E.printstacktrace ();
}
}
6. Keyedhandler: The query returns the Map collection, takes the specified column as key, encapsulates each record as value, and the final result map< the specified column as key,map< column name, column value >> Map;
@Test
Public void getkeyed () {
String sql = "SELECT * from admin;";
To create a core processing class
Queryrunner runner = new Queryrunner (DS);
Try {
map<integer,map<string,string>> obj = (Map<integer, map<string, string>>) runner.query (SQL, New Keyedhandler ("user"));
System.out.println (obj);
Catch (SQLException e) {
E.printstacktrace ();
}
}
9. Scalarhandler: Returns the first column of the first row of the query result
@Test
Public void Getoneone () {
String sql = "SELECT * from admin;";
To create a core processing class
Queryrunner runner = new Queryrunner (DS);
Try {
Object obj = runner.query (sql, new scalarhandler ("meal"));
System.out.println (obj);
Catch (SQLException e) {
E.printstacktrace ();
}
}
}
|