Java Connection MySQL needs a driver package, the latest version is:http://dev.mysql.com/downloads/connector/j/, extract the jar library file, and then import the library file in the corresponding project.
Download steps:
Link: Http://pan.baidu.com/s/1o8PhY2Y Password: a1g1
Example of connection code:
Packagecom.josft.test;ImportJava.sql.*; Public classMysqldemo {//JDBC driver name and database URL Static FinalString jdbc_driver = "Com.mysql.jdbc.Driver"; Static FinalString Db_url = "Jdbc:mysql://localhost:3306/jsoft"; //The user name and password of the database need to be set according to their own Static FinalString USER = "root"; Static FinalString PASS = "123456"; Public Static voidMain (string[] args) {Connection conn=NULL; Statement stmt=NULL; Try{ //registering the JDBC driverClass.forName ("Com.mysql.jdbc.Driver"); //Open LinkSYSTEM.OUT.PRINTLN ("Connect database ..."); Conn=drivermanager.getconnection (Db_url,user,pass); //Execute QuerySYSTEM.OUT.PRINTLN ("instantiating statement to ..."); stmt=conn.createstatement (); String SQL; SQL= "SELECT id, name, url from websites"; ResultSet RS=stmt.executequery (SQL); //Expand the result set database while(Rs.next ()) {//Retrieving by Fields intid = rs.getint ("id"); String name= rs.getstring ("name"); String URL= rs.getstring ("url"); //Output DataSystem.out.print ("ID:" +ID); System.out.print (", Site name:" +name); System.out.print (", Site URL:" +URL); System.out.print ("\ n"); } //Close after completionRs.close (); Stmt.close (); Conn.close (); }Catch(SQLException se) {//Handling JDBC ErrorsSe.printstacktrace (); }Catch(Exception e) {//Handling Class.forName ErrorsE.printstacktrace (); }finally{ //Close Resource Try{ if(stmt!=NULL) Stmt.close (); }Catch(SQLException se2) {}//don't do anything. Try{ if(conn!=NULL) Conn.close (); }Catch(SQLException se) {se.printstacktrace (); }} System.out.println ("Goodbye!"); }}
Reference: http://www.runoob.com/java/java-mysql-connect.html
Java connection MySQL