When we make a database connection, we tend to connect the database, close the operation, execute the SQL statement and write it in the DAO layer.
The database operation is encapsulated, and the object is passed through dynamic parameters,
Through the self-built generic implementation of the return class received, the current program is as follows:
/* Copyright (C), 1988-1999, huawei tech. co., ltd. filename:jdbcutil.java author: light version : version 1.0 Date: 2018/7/12description: Encapsulation of JDBC Linked database// module Description Version: Modular Implementation of JDBC Connection database operations, reducing development code about the amount of database operations// version information Function List: // main functions and their functions 1.getconnection database links 2.close Perform database shutdown 3.executeUpdate execute data Delete and change 4.executeQuery Execute data query History: // history change record <author> <time> <version > <desc> david 96/10/12 1.0& nbsp; build this moudle ************************************ ***********************/ Public classJdbcutil {//link address, setting encoding is available and Utf-8 Public StaticString url="Jdbc:mysql://192.168.80.131:3306/db1?useunicode=true&characterencoding=utf8"; //Database user name Public StaticString user="Root"; //Database Password Public StaticString pwd="123456"; /** Link to the database*/ Public StaticConnection getconnection () {Connection con=NULL; Try { //Load DriverClass.forName ("Com.mysql.jdbc.Driver"); //Create a linkcon=drivermanager.getconnection (URL, USER, PWD); } Catch(ClassNotFoundException e) {//TODO auto-generated Catch blockE.printstacktrace (); } Catch(SQLException e) {//TODO auto-generated Catch blockE.printstacktrace (); } //Back to Connection returncon; } /** Database off*/ Public Static voidClose (Connection con,preparedstatement pstmt,resultset rs) {Try { //determine if it is being manipulated if(rs!=NULL) Rs.close (); if(pstmt!=NULL) Pstmt.close (); if(con!=NULL) Con.close (); } Catch(SQLException e) {//TODO auto-generated Catch blockE.printstacktrace (); } } /** CREATE database execution operation, return the number of rows affected * @param String SQL * @param Object params * @return int result*/ Public Static intExecuteupdate (String sql,object ...params){ //Create a linkConnection con=getconnection (); PreparedStatement pstmt=NULL; intresult=0; Try { //precompiled SQL statements to prevent SQL injectionpstmt=con.preparestatement (SQL); //Pass parameters, if the arguments exist if(params!=NULL){ //to make a cyclic pass for(intI=0;i<params. length;i++) {Pstmt.setobject (i+1,params[i]); } } //executes the SQL statement, returning the number of rows affectedresult=pstmt.executeupdate (); } Catch(SQLException e) {//TODO auto-generated Catch blockE.printstacktrace (); }finally{Close (con,pstmt,NULL); } returnresult; } /** Data query, through the self-built generic rowmap, data type definition * @param String SQL * @param rowmap T * @param Object params * @return lis T<t>*/ Public Static<T> list<t> executeQuery (String sql,rowmap<t> rowmap,object ...params){ //Create a generic listList<t> list=NewArraylist<>(); //Create a linkConnection con=getconnection (); PreparedStatement pstmt=NULL; ResultSet RS=NULL; Try { //Binding SQL statementspstmt=con.preparestatement (SQL); //cyclic wearing parameters if(params!=NULL){ for(intI=0;i<params. length;i++) {Pstmt.setobject (i+1,params[i]); } } //EXECUTE statement, receive with result setrs=Pstmt.executequery (); while(Rs.next ()) {//using a self-built generic to implement array additionsT t=rowmap.rowmapping (RS); List.add (t); } } Catch(SQLException e) {//TODO auto-generated Catch blockE.printstacktrace (); }finally{Close (CON,PSTMT,RS); } returnlist; }//using the sample function Public Static intUpdate () {returnExecuteupdate ("INSERT into student (Name,age,sex) VALUES (?,?,?)","name", A,"male");
}
}
Custom self-built generics
import java.sql.resultset;//Self-built generics, implementing rowmappping return T type Public Interface Rowmap<t> { public T rowmapping (ResultSet rs);}
JAVA-JDBC Package Connection Database Tool