Java Connection MySQL database implementation code

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.