MyEclipse connects to MySQL database via JDBC basic introduction

  • 2020-05-13 03:42:14
  • OfStack

1. The premise is that MyEclipse can normally develop the Java project
2. Install MySQL
The version for personal use is mysql-5.0.22-win32.zip
Address: http: / / www. mysql. com/downloads/mysql / # downloads
3. Download the JDBC driver
Personal use is mysql-connector-java-5.1.22.zip, and all that is needed is the unzipped mysql-connector-java-5.1.22-bin.jar
Address: http: / / www. mysql. com/downloads/connector j /
4. Code testing
 
package ts.jsj.lyh; 

import java.sql.*; 

/** *//** 
*  use JDBC Connect to database MySQL The process of  
* DataBase : JSJ .  table : student ;  
* @author DuChangfeng 2008 09 18 
*/ 
public class JDBCTest { 

public static Connection getConnection() throws SQLException, 
java.lang.ClassNotFoundException 
{ 
// The first 1 Load step: MySQL the JDBC The driver  
Class.forName("com.mysql.jdbc.Driver"); 

// Get connected url, Access to the MySQL The user name of the database , Password; jsj : database name  
String url = "jdbc:mysql://localhost:3306/jsj"; 
String username = "root"; 
String password = "111"; 

// The first 2 Step: create with MySQL An instance of the connection class for the database  
Connection con = DriverManager.getConnection(url, username, password); 
return con; 
} 


public static void main(String args[]) { 
try 
{ 
// The first 3 Step: get the connection class instance con with con create Statement Object class instance  sql_statement 
Connection con = getConnection(); 
Statement sql_statement = con.createStatement(); 

/** *//************  Operate on the database  ************/ 
// Delete if the database with the same name exists  
//sql_statement.executeUpdate("drop table if exists student"); 
// To perform the 1 a sql Statement generated 1 called student The table of  
//sql_statement.executeUpdate("create table student (id int not null auto_increment, name varchar(20) not null default 'name', math int not null default 60, primary key (id) ); "); 
// Insert data into the table  
//sql_statement.executeUpdate("insert student values(1, 'liying', 98)"); 
//sql_statement.executeUpdate("insert student values(2, 'jiangshan', 88)"); 
//sql_statement.executeUpdate("insert student values(3, 'wangjiawu', 78)"); 
//sql_statement.executeUpdate("insert student values(4, 'duchangfeng', 100)"); 
//--- The above is not practical, but listed as a reference --- 

// The first 4 Step: execute the query with ResultSet Class, which returns the result of the query  
String query = "select * from student"; 
ResultSet result = sql_statement.executeQuery(query); 
/** *//************  Operate on the database  ************/ 

System.out.println("Student The data in the table is as follows :"); 
System.out.println("------------------------"); 
System.out.println(" Student id " + " " + " The name " + " " + " The data result  "); 
System.out.println("------------------------"); 

// Process the obtained query results, yes Result Object of the class to manipulate  
while (result.next()) 
{ 
int number = result.getInt("sno"); 
String name = result.getString("sname"); 
String mathScore = result.getString("sgrade"); 
// Get the data in the database  
System.out.println(" " + number + " " + name + " " + mathScore); 
} 

// Close the connection and declaration  
sql_statement.close(); 
con.close(); 

} catch(java.lang.ClassNotFoundException e) { 
// loading JDBC error , The desired driver was not found  
System.err.print("ClassNotFoundException"); 
// Other errors  
System.err.println(e.getMessage()); 
} catch (SQLException ex) { 
// Displays a database connection error or query error  
System.err.println("SQLException: " + ex.getMessage()); 
} 
} 

} 

Thanks to the great efforts of the great apes, the detailed steps can be easily found on the powerful Internet. Now, I would like to add a few points that I think should be paid attention to:

1) about the storage location of mysql-connector-java-5.1.22-bin.jar. In the MyEclipse specific java project, create a new folder for jar packages (e.g. lib), copy mysql-connector-java-5.1.22-bin.jar to the folder, and select jar packages and right-click -- > Build Path--- > Add To Build Path.

If appear

ClassNotFoundExceptioncom.mysql.jdbc.Driver

Is caused by the lack of importing the jar package.

2) if you are already familiar with the use of MySQL, you can ignore this. When individuals test connections, they often get this exception:

SQLException: Communications link failure
The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.

This is because I am not familiar with the use of MySQL, so I have tried many operations on MySQL. I do not know when I accidentally shut down the service of MySQL (if it is not changed during the installation of MySQL, the default service name is MySQL). The solution is to turn on this service. Control panel -- > Management tools -- > Service - > MySQL--- > Select enable.

3) when using the above code for testing, the following should be changed:
// user name, password and database name of MySQL database
 
String url = "jdbc:mysql://localhost:3306/jsj"; 
String username = "root"; 
String password = "111"; 

And the field name to be queried in the specific base table:
 
int number = result.getInt("sno"); 
String name = result.getString("sname"); 
String mathScore = result.getString("sgrade"); 

Share more. If you have any questions, please let us know

Related articles: