Java Web series 14 (DBUtil) and Java Web series dbutil
Required bytes 1. metadata of the database: the database, or the database table. The field quota in the table includes information such as the database name, driver, and the name of the primary key in the table. This mainly completes some code databases with high universality, there are three types of metadata: the first type: Database metadata to obtain metadata: Connection. getMateData (); get the methods in the Data Metadata DatabaseMateData: getDriverName (), getURL (), getUserName (), getTables (), rs = db. getTables ("", "", "user", new String [] {"TABLE"}); the first two are empty, the third is the TABLE name, And getPrimaryKeys () obtain the table's primary key. Type 2: Get the parameter metadata through getParaneterMetaData () in PraparedStatatment. ParameterMetaData represents a parameter getP. ArameterMetaData getParameterTypeName (); the metadata parameter support in mysql is not very good. Category 3: result set metadata ResultSetMetaData object: getParameterCount (): Get the number of parameters (that is? );
2. DBUtils is an open source JDBC tool library provided by the Apache organization. The classes in the Operation dubuils are all in org. apache .. dbutils in the package is easy to use. In many companies, you do not want to use hibernate. If hibernate is not used well, it may cause performance problems. Many companies use the DBUtils framework to perform jdbc operations. To use the DBUtils framework, first import the jar package. The class to be used has two first: DbUtils some methods:-close (Connection conn)-loadDriver (String driverClassName)-rollback (Connection conn) Second: QueryRunner: to complete the database crud operation method:-constructor, there are two commonly used QueryRunner (). If you use a constructor without parameters, you need to use the following method-update (Connection conn, string SQL, Object... params)-query (Connection conn, String SQL, ResultSetHandler <T> rsh, Object... params) QueryRunner (DataSource ds) (important) if you use a parameter constructor, use the parameter Da Construction Method of taSource,-update (String SQL, Object... params): the query (String SQL, ResultSetHandler <T> rsh, Object... params): Completed query operation (1) added, modified, and deleted operation code using dbutils // added operation QueryRunner runner = new QueryRunner (MyJDBCUtils. getDataSource (); // update (String SQL, Object... params): The runner of the ADD, modify, and delete operations. update ("insert into user values (?,?,?) ", 666," Sun Wukong ", 20000); // modify the runner. update (" update user set sal =? Where id =? ", 555,111); // The delete operation runner. update (" delete from user where id =? ", 222); one interface ResultSetHandler <T> (2) use dbutils to use the query operation to show that if the interface is used to implement more complex queries, however, dbutils provides many implementation classes for this interface. Based on these Implementation classes, you can complete the desired format encapsulation and directly implement the new implementation class to implement the ArrayHandler operation: encapsulate data into an array Object [] objs = runner. query ("select * from user where id =? ", New ArrayHandler (), 111); System. out. println (Arrays. toString (objs); ArrayListHandler: put each row of data in the array first, put multiple arrays in the list List list <Object []> list = runner. query ("select * from user", new ArrayListHandler (); for (Object [] objects: list) {System. out. println (Arrays. toString (objects);} BeanHandler: encapsulate data into an object (important) User user = runner. query ("select * from user where id =? ", New BeanHandler <User> (User. class), 666); System. out. println (user. getId () + "" + user. getName () + "" + user. getSal (); BeanListHandler: Put data in the object, put multiple objects in the list (focus on) List <User> list = runner. query ("select * from user", new BeanListHandler <User> (User. class); for (User user: list) {System. out. println (user. getId () + "" + user. getName () + "" + user. getSal ();} MapHandler: encapsulate data in the map. Map <String, Object> map = Runner. query ("select * from user where id =? ", New MapHandler (), 333); System. out. println (map); MapListHandler: encapsulate data in the map first, and put multiple maps in the list to List <map <String, Object> list = runner. query ("select * from user", new MapListHandler (); for (Map <String, Object> map: list) {System. out. println (map);} ColumnListHandler: queries the value of a column and places the value in list. List <Object> list = runner. query ("select name from user", new ColumnListHandler (); for (Object object: list) {System. out. println (object);} KeyedHandler: first put the data into the map, and put the encapsulated map as a value into another map. Map <Object, Map <String, object> map = runner. query ("select * from user", new KeyedHandler ("name"); System. out. println (map); calarHandler: select count (*) from user; Object o = runner. query ("select count (*) from user", new ScalarHandler (); System. out. println (Long) o); Master implementation class: BeanHandler BeanListHandler MapListHandler
Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.