Java operation MySQL database, need to drive
Mysql-connector-java to operate, to download the corresponding jar package first, import the required jar packageI am using MAVEN to manage the package, add the following in Maven, directly re-import Reimport the package is downloaded.<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.37</version></dependency>If you want another version, you can go to Maven's http://www.mvnrepository.com/ and search for the corresponding dependencies. Second, start the specific code1. Loading driversclass.forname ("Com.mysql.jdbc.Driver");Description: Load this driver into the JVC 2. Connect to the database (Con is custom)Connection con = drivermanager.getconnection (url, user, password) 3. Create a statement object to execute the SQL statementStatement stmt = Con.createstatement (); 4. Execute SQL statementscan be query, update, and so on, I only use the query, update can be used executeupdate Query statement:res = stmt.executequery (SQL)Description: stmt Creates a statement object for itself, SQL is the SQL specific statement to execute, is a string type 5. Extracting an item from a query resultWhile (Res.next ()) {Long info_id = Res.getlong ("info_id");}Description:1. Because the query results may be more than one, so to traverse, as long as there is this one can be obtained2. Above is the query out of the field named "Info_id" in the info_id variable, is a long variable type (it is important to look at the table design of the field type, some can use int, but there is a long)3. If it is char and other types can be used res.getstring ("XXX") to obtain the corresponding value, and other types can be specific to query the corresponding method I wrote a Java operation for the specific code of MySQL as follows:Package entity;
import java.sql.*;
import java.sql.Connection;import java.sql.Statement; /**
* Created by Lenovo on 2015/12/7.
*/
Public class jdbc{
Statement stmt =null;
ResultSet rs =null;
String result = null;
Public JDBC () {
String Driver = "Com.mysql.jdbc.Driver";
String url = "Jdbc:mysql://10.5.17.66:58885/?useunicode=true&characterencoding=utf-8";
String user = "root";
String password = "123456";
try {
Class.forName (driver);
//System.out.println ("found driver");
try {
Connection con = drivermanager.getconnection (url, user, password);
//SYSTEM.OUT.PRINTLN ("Database connection is successful! ");
this.stmt = Con.createstatement ();
} catch (SQLException e) {
System.out.println ("Database connection failed! ");
e.printstacktrace ();
}
} catch (ClassNotFoundException e) {
System.out.println ("Driver class not found, load driver failed!") ");
e.printstacktrace ();
}
}
//The method returns all SQL results, which need to be traversed and then obtained in the call .
Public ResultSet GetResult (String sql) {
try {this.rs = this.stmt.executeQuery (sql);return this.rs;
}catch (SQLException e) {
System.out.println ("the field to find is not found in SQL!") ");
e.printstacktrace ();
return this.rs;
}
}
//This method is directly get the field name to find the corresponding result, only take one, so will continue to overwrite, take to the last, trial and SQL results only one case, if the result is a lot of data, need to use Excutesql method
Public string GetResult (String sql,string key) {
try {
this.rs = this.stmt.executeQuery (sql);
}catch (SQLException e) {
System.out.println ("the field to find is not found in SQL!") ");
e.printstacktrace ();
}
try{
While (This.rs.next ()) {
This.result = this.rs.getString (key);
}
}catch (Exception e) {
System.out.println ("SQL Gets the result exception!");
e.printstacktrace ();
}
return this.result;
}
Public static void Main (String args[]) {
JDBC test = new JDBC ();
String sql = "SELECT * from Dbwww58com_info.info limit 1";
//There will be multiple columns of results when only the SQL is passed
ResultSet res = test.getresult (sql);
System.out.print (res);
try{
While (Res.next ()) {
Long info_id = Res.getlong ("info_id");
System.out.println ("\ninfo_id:" + info_id);
}
}catch (Exception e) {
System.out.println ("SQL Gets the result exception!");
e.printstacktrace ();
}
//Only one result, pass SQL and key two parameters
//String result= test.getresult (sql, "info_id");
//System.out.println ("The result is" +result);
}
}The results of the implementation are as follows:Description: The method used to write the GetResult () overload, because Java does not support the parameter assignment default value, so can only use overloading to implement such a function (the same execution method, I point to get query results, one want to get the query results in a specific field value), With regard to the use of Java overloading, I will write another essay in a few days to add to it ~
Java connection MySQL database and operation