First to download connector/j address: http://www.mysql.com/downloads/connector/j/
This is the official MySQL connection method:
After extracting the jar library file, you need to import the library file in the project
I'm using eclipse:
Java is a bit cumbersome to connect to MySQL, so write a class first to open or close the database:
Dbhelper.java
1 PackageCom.hu.demo; 2 3 Importjava.sql.Connection; 4 ImportJava.sql.DriverManager; 5 Importjava.sql.PreparedStatement; 6 Importjava.sql.SQLException; 7 8 Public classDBHelper {9 Public Static FinalString url = "Jdbc:mysql://127.0.0.1/student"; Ten Public Static FinalString name = "Com.mysql.jdbc.Driver"; One Public Static FinalString user = "root"; A Public Static FinalString password = "root"; - - PublicConnection conn =NULL; the PublicPreparedStatement PST =NULL; - - Publicdbhelper (String sql) { - Try { +Class.forName (name);//Specify connection Type -conn = drivermanager.getconnection (URL, user, password);//Get Connections +PST = conn.preparestatement (SQL);//prepare to execute statement A}Catch(Exception e) { at E.printstacktrace (); - } - } - - Public voidClose () { - Try { in This. Pst.close (); - This. Conn.close (); to}Catch(SQLException e) { + E.printstacktrace (); - } the } *}
Write a demo.java to perform the related query operation
Demo.java
1 PackageCom.hu.demo; 2 3 ImportJava.sql.ResultSet; 4 Importjava.sql.SQLException; 5 6 Public classDemo {7 8 StaticString sql =NULL; 9 StaticDBHelper DB1 =NULL; Ten StaticRESULTSET ret =NULL; One A Public Static voidMain (string[] args) { - test1 (); -Test2 ();///execute an SQL statement with parameters the } - - Private Static voidtest1 () { -sql = "Select *from stuinfo";//SQL statements +DB1 =NewDBHelper (SQL);//Create a DBHelper object - + Try { Aret = Db1.pst.executeQuery ();//EXECUTE statement to get result set at while(Ret.next ()) { - ///extract result set in order -String uid = ret.getstring (1); -String ufname = ret.getstring (2); -String ulname = ret.getstring (3); -String udate = ret.getstring (4); inSYSTEM.OUT.PRINTLN (uid + "\ T" + Ufname + "\ T" + Ulname + "\ T" +udate); -}//Show Data to Ret.close (); +Db1.close ();//Close Connection -}Catch(SQLException e) { the E.printstacktrace (); * } $ } Panax Notoginseng ///SQL statement with Parameters - Private Static voidtest2 () { the ///SQL statement with parameters--------- +sql = "Select *from stuinfo where chess_id =?" and name =? "; ADB1 =NewDBHelper (SQL);//Create a DBHelper object the + Try { - ///Set parameters to the SQL statement-------- $Db1.pst.setString (1, "123"); $Db1.pst.setString (2, "aircraft")); - -ret = Db1.pst.executeQuery ();//EXECUTE statement to get result set the while(Ret.next ()) { - ///Extract the result value according to the column name---------WuyiString uid = ret.getstring ("chess_id"); theString ufname = ret.getstring ("name"); -String ulname = ret.getstring ("Data"); WuString udate = ret.getstring ("Data2"); -SYSTEM.OUT.PRINTLN (uid + "\ T" + Ufname + "\ T" + Ulname + "\ T" +udate); About}//Show Data $Ret.close ();///Close the database resource for the result set -Db1.close ();//Close Connection -}Catch(SQLException e) { - E.printstacktrace (); A } + } the -}
JDBC Connection MySQL