It's simple, but sometimes it only leaves an impression.
Public classJdbcutil {StaticString URL =NULL; StaticString username =NULL; StaticString Password =NULL; StaticString Driverclass =NULL; Static { Try{Properties Properties=NewProperties (); InputStream in= Jdbcutil.class. getResourceAsStream ("/jdbc.properties"); Properties.load (in); URL= Properties.getproperty ("url"); Username= Properties.getproperty ("username"); Password= Properties.getproperty ("Password"); Driverclass= Properties.getproperty ("Driver"); SYSTEM.OUT.PRINTLN (password); Class.forName (Driverclass); } Catch(Exception e) {e.printstacktrace (); System.out.println ("Drive failed to load!" "); Throw NewRuntimeException (e); } } Public StaticConnection getconnection () {Try{Connection conn=drivermanager.getconnection (URL, username, password); returnConn; } Catch(SQLException e) {e.printstacktrace (); Throw NewRuntimeException (e); } } Public Static voidClose (Connection Conn, Statement St, ResultSet rs) {if(Conn! =NULL) { Try{conn.close (); } Catch(Exception e) {Throw NewRuntimeException (e); } } if(St! =NULL) { Try{st.close (); } Catch(Exception e) {Throw NewRuntimeException (e); } } if(rs! =NULL) { Try{rs.close (); } Catch(Exception e) {Throw NewRuntimeException (e); } } } Public Static voidClose (Connection conn, Statement St) {if(Conn! =NULL) { Try{conn.close (); } Catch(Exception e) {Throw NewRuntimeException (e); } } if(St! =NULL) { Try{st.close (); } Catch(Exception e) {Throw NewRuntimeException (e); } } } /** Release Resources*/ Public Static voidreleaseresources (ResultSet ResultSet, Statement Statement, Connection Connection) {Try { if(ResultSet! =NULL) Resultset.close (); } Catch(SQLException e) {e.printstacktrace (); } finally{ResultSet=NULL; Try { if(Statement! =NULL) Statement.close (); } Catch(SQLException e) {e.printstacktrace (); } finally{statement=NULL; Try { if(Connection! =NULL) Connection.close (); } Catch(SQLException e) {e.printstacktrace (); } finally{Connection=NULL; } } } }}
Importjava.sql.Connection;Importjava.sql.PreparedStatement;ImportJava.sql.ResultSet;Importjava.sql.SQLException;Importjava.sql.Statement;ImportBean. Quackresults;//Add new functions to the database/*** 2017/10/21 *@authorLemo **/ Public classDbexcute {PrivateConnection Connection; PrivateStatement Statement; PrivateResultSet ResultSet; //Update Action Public voidupdate (String sql) {Try{Connection=jdbcutil.getconnection (); Statement=connection.createstatement (); //Can create, modify, delete tables, add, delete, modify tuples, and query SQL statements Booleannum=statement.execute (SQL); SYSTEM.OUT.PRINTLN (num); } Catch(SQLException e) {e.printstacktrace (); } finally{Jdbcutil.close (connection, (com.mysql.jdbc.Statement) Statement, ResultSet); } } //Query Operations PublicResultSet Query (String sql) {Try{Connection=jdbcutil.getconnection (); Statement=connection.createstatement (); ResultSet=statement.executequery (SQL); } Catch(SQLException e) {e.printstacktrace (); } finally{jdbcutil.releaseresources (resultSet, statement, connection); } returnResultSet; } //Add Action Public voidaddelement (String sql) {update (SQL); } Public voidaddelement (quackresults aquackresults) {/*String sqlstr = "select* from users where username=?" and password=? "; Connection = Jdbcutil.getconnection (); PreparedStatement Astatement=null; try {astatement=connection.preparestatement (SQLSTR); Astatement.setstring (1, "Zhansgan"); Astatement.setstring (2, "root"); } catch (SQLException e) {//TODO auto-generated catch block E.printstacktrace (); }*/String sqlstr= "INSERT into mine_quack_results values (null,?,?,?,?,?)"; Connection=jdbcutil.getconnection (); PreparedStatement astatement=NULL; Try{astatement=connection.preparestatement (SQLSTR); Astatement.setdouble (1, Aquackresults.getxdata ()); Astatement.setdouble (2, Aquackresults.getydata ()); Astatement.setdouble (3, Aquackresults.getzdata ()); Astatement.settimestamp (4, Aquackresults.getquacktime ()); Astatement.setdouble (5, Aquackresults.getquackgrade ()); System.out.println (Astatement.execute ()); Connection.close (); } Catch(SQLException e) {//TODO auto-generated Catch blockE.printstacktrace (); } } //Delete Operation Public voidremoveelement (String sql) {update (SQL); } //Create a table Public voidcreatetable (String sql) {update (SQL); } //Delete a table Public voiddroptable (String sql) {update (SQL); }}
driver=com.mysql.jdbc.Driverurl=jdbc:mysql://localhost:3306/ks?useunicode=true& Characterencoding=utf8&allowmultiqueries=trueusername=rootpassword=root# Defines the initial number of connections InitialSize=0#定义最大连接数maxActive=20#定义最大空闲maxIdle=20#定义最小空闲minIdle=1# Define the maximum wait time maxwait=60000
JDBC Link Database