1. Get the database connection and query code
PackageConnectionmysql;Importjava.sql.Connection;ImportJava.sql.DriverManager;ImportJava.sql.ResultSet;Importjava.sql.SQLException;Importjava.sql.Statement; Public classConnectionmysql {//database connection user name PrivateString userName = "root"; //Database connection Password PrivateString pwd = "910214"; //setting up the database PrivateString database = "JSP"; //setting up the JDBC driver PrivateString dbdriver = "Com.mysql.jdbc.Driver"; //setting the database connection URL PrivateString dbconnect = "jdbc:mysql://localhost:3306/" +database; //Connection Variables PrivateConnection conn =NULL; PrivateStatement stmt =NULL; ResultSet RS=NULL; /*database-Driven registration*/ PublicConnectionmysql () {Try{class.forname (dbdriver); } Catch(Exception ex) {System.out.println ("Connection failed:" +ex.getmessage ()); } } /*Establish database connection and its data query*/ PublicResultSet executeQuery (String sql)throwssqlexception{rs=NULL; Try{conn=drivermanager.getconnection (Dbconnect, UserName, PWD); stmt=conn.createstatement (); RS=stmt.executequery (SQL); }Catch(Exception ex) {System.out.println ("Connection failed:" +ex.getmessage ()); }//finally{// //To close a database connection//stmt.close ();//conn.close ();// } returnrs; } /*Establish database connections and database queries*/ Public voidExcuteupdate (String sql)throwsSQLException {stmt=NULL; Try{ //connecting to a databaseconn =drivermanager.getconnection (Dbconnect, UserName, PWD); stmt=conn.createstatement (); Stmt.executeupdate (SQL); }Catch(Exception ex) {//Throwing Exceptions manually Throw NewSQLException (Ex.getmessage ()); }//finally{//stmt.close ();//conn.close ();// } } /** Consider database performance issues and need to release database resources, so the shutdown method*/ //Closing Statement Statements Public voidclosestmt () {Try{stmt.close (); }Catch(SQLException ex) {System.out.println ("Failed to close database:" +ex.getmessage ()); } } //Close Connection Public voidCloseconn () {Try{conn.close (); }Catch(SQLException ex) {System.out.println ("Shutdown Connection failed:" +ex.getmessage ()); } } }
2. Test code
PackageConnectionmysql;ImportJava.sql.ResultSet;Importjava.sql.SQLException; Public classtestmysqlconnection {/*** Run Program Test*/ Public Static voidMain (string[] args) {//TODO auto-generated Method StubConnectionmysql Conmysql=NewConnectionmysql (); //querying database SQL statementsString sql = "Select Username,password from Userlogin"; Try{ //return query result setResultSet rs =conmysql.executequery (SQL); //Print query Data if(Rs.next ()) {String username= rs.getstring (1); String Password= Rs.getstring (2); System.out.println ("User name:" +username); System.out.println ("Password:" +password);//System.out.println (rs.getstring (2)); } }Catch(SQLException ex) {System.out.println ("Query failed:" +ex.getmessage ()); }finally{ //To close a database connectionconmysql.closestmt (); Conmysql.closeconn (); } }}
Java Connection MySQL database implementation code