1. Connect to database and get database connection variables
Note When connecting to the database
(1) Open DB Browser New database Driver, note add Driver jars Add the package, my is Mysql-connector-java-5.0.3-bin.jar
(2) To copy the database jar package to the Web-inf\lib under the project
import Java.sql.connection;//java Package public class DBConnection { Private String dbdriver= "Com.mysql.jdbc.Driver"; private string Dburl= "Jdbc:mysql://[ip address]:[port number/[database name]";//Change the private string dbuser= "root" according to the actual situation; Private String dbpass= "root"; Public Connection Getconn () {Connection conn=null; try {class.forname (dbdriver); } catch (ClassNotFoundException e) {e.printstacktrace (); try {conn = drivermanager.getconnection (dburl,dbuser,db Pass);//Note is a three parameter} catch (SQLException e) {E.print StackTrace (); } return conn; } }
2. Insert Operation
public int Insert () { int i=0; String sql= "INSERT into (table name) (column name 1, listing 2) VALUES (?,?)"; Connection cnn=getconn (); try{ preparedstatement prestmt =cnn.preparestement (SQL); Prestmt.setstring (1, value); Prestmt.setstring (2, value);//or: Prestmt.setint (1, value); I=prestmt.executeupdate (); } catch (SQLException e) { e.printstacktrace (); } Return i;//returns the number of rows affected, 1 for execution success }
3. Update Operations
public int update { int i=0; String sql= "Update (table name) set (column name 1) =?, List 2=? where (column name) =? ";//note to have a where condition Connection cnn=getconn (); try{ preparedstatement prestmt =cnn.preparestatement (SQL); Prestmt.setstring (1, (value)); Prestmt.setstring (2, (value));//or: Prestmt.setint (1, value); Prestmt.setint (3, (value)); I=prestmt.executeupdate (); } catch (SQLException e) { e.printstacktrace (); } Return i;//returns the number of rows affected, 1 for execution success }
4. Find Operations
Public String Select { String sql = ' SELECT * FROM (table name) where (column name) = (value) "; Connection CNN = Getconn ();//Here for the method written by yourself Getconn () Get the connection try { Statement stmt = Conn.createstatement (); ResultSet rs = stmt.executequery (sql); if (Rs.next ()) { int m1 = Rs.getint (1),//or rs.getstring (1), based on the value type of the column in the database, the parameter is the first column of String m2 = Rs.getstring (2); } You can write the found value to the class and return the corresponding object } catch (SQLException e) { e.printstacktrace (); } Return (the variable of the corresponding value); }
5. Delete Operation
public int Delete () { String sql = ' Delete from (table name) where (column name) = (value) "; int i=0; Connection conn = Getconn ();//Here for the method written by yourself Getconn () Get the connection try { Statement stmt = Conn.createstatement (); i = stmt.executeupdate (sql); } catch (SQLException e) { e.printstacktrace (); } Return i;//If the return is 1, the execution succeeds; }
Java connection MySQL database additions and deletions change operation record