Basic Introduction of MyEclipse connecting to MySQL database through JDBC

Source: Internet
Author: User

1. The premise is that MyEclipse can normally develop Java Projects
2. Install MySQL
Personally, the version is mysql-5.0.22-win32.zip.
Web: http://www.mysql.com/downloads/mysql/#downloads
3. Download The JDBC driver
I personally use mysql-connector-java-5.1.22.zip, all I need is the mysql-connector-java-5.1.22-bin.jar after decompression
Web: http://www.mysql.com/downloads/connector/j/
4. Code Testing
Copy codeThe Code is as follows:
Package ts. jsj. lyh;

Import java. SQL .*;

/***//**
* Connection to MySQL using JDBC
* DataBase: JSJ, table: student;
* @ Author DuChangfeng 2008 09 18
*/
Public class JDBCTest {

Public static Connection getConnection () throws SQLException,
Java. lang. ClassNotFoundException
{
// Step 1: load the JDBC driver of MySQL
Class. forName ("com. mysql. jdbc. Driver ");

// Obtain the connection url, the user name and password that can access the MySQL database; jsj: Database Name
String url = "jdbc: mysql: // localhost: 3306/jsj ";
String username = "root ";
String password = "111 ";

// Step 2: create a connection instance to the MySQL database
Connection con = DriverManager. getConnection (url, username, password );
Return con;
}


Public static void main (String args []) {
Try
{
// Step 3: Obtain the connection class instance con and use con to create the Statement object class instance SQL _statement
Connection con = getConnection ();
Statement SQL _statement = con. createStatement ();

/***** // ************* Perform operations on the database ************/
// Delete a database with the same name
// SQL _statement.executeUpdate ("drop table if exists student ");
// Execute an SQL statement to generate a table named student.
// 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, 'liing', 98 )");
// SQL _statement.executeUpdate ("insert student values (2, 'jiangshance', 88 )");
// SQL _statement.executeUpdate ("insert student values (3, 'wangjiawu ', 78 )");
// SQL _statement.executeUpdate ("insert student values (4, 'duchangfeng', 100 )");
// --- The preceding operations are not practical, but are listed as a reference ---

// Step 4: Execute the query. Use the ResultSet class object to return the query result.
String query = "select * from student ";
ResultSet result = SQL _statement.executeQuery (query );
/***** // ************* Perform operations on the database ************/

System. out. println ("the data in the Student table is as follows :");
System. out. println ("------------------------");
System. out. println ("student ID" + "" + "name" + "+" Data score ");
System. out. println ("------------------------");

// Process the obtained query results and perform operations on the objects in the Result class
While (result. next ())
{
Int number = result. getInt ("sno ");
String name = result. getString ("sname ");
String mathScore = result. getString ("sgrade ");
// Obtain data in the database
System. out. println ("" + number + "" + name + "" + mathScore );
}

// Close the connection and declare
SQL _statement.close ();
Con. close ();

} Catch (java. lang. ClassNotFoundException e ){
// JDBC loading error. The driver used cannot be found
System. err. print ("ClassNotFoundException ");
// Other errors
System. err. println (e. getMessage ());
} Catch (SQLException ex ){
// Display database connection errors or query errors
System. err. println ("SQLException:" + ex. getMessage ());
}
}

}

Most of the above content is organized on the Internet. Thanks to the selfless dedication of the ape ~~ The specific steps and powerful Internet are easy to query. I will not repeat them here. I will add some points that I think should be noted about:

1) about the storage location of the mysql-connector-java-5.1.22-bin.jar. In the specific java project of MyEclipse, create a folder for storing jar packages (such as lib), copy the mysql-connector-java-5.1.22-bin.jar To the folder, right-click the jar package and choose ---> Build Path ---> Add To Build Path, you can.

If

ClassNotFoundExceptioncom. mysql. jdbc. Driver

Is caused by the lack of imported jar packages.

2) If you are familiar with MySQL, ignore this line. When I test the connection, the following error occurs:

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

This is precisely because I am not familiar with MySQL and have made a lot of trial operations on MySQL. I do not know when to use the MySQL Service (if it is not changed during MySQL installation, the default service name is MySQL. Control Panel ---> Management Tools ---> services ---> MySQL ---> select enable.

3) when using the above Code for testing, the following must be changed:
// MySQL database username, password, and database name
Copy codeThe Code is as follows:
String url = "jdbc: mysql: // localhost: 3306/jsj ";
String username = "root ";
String password = "111 ";

And the field names to be queried in the basic table:
Copy codeThe Code is as follows:
Int number = result. getInt ("sno ");
String name = result. getString ("sname ");
String mathScore = result. getString ("sgrade ");

Share more information. If you have any questions, please contact us ~~

Related Article

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.