MyEclipse uses Java to connect to the MySQL database through JDBC-MySQL

Source: Internet
Author: User
MyEclipse uses Java to connect to the MySQL database through JDBC basic test bitsCN.com

MyEclipse uses Java to connect to the MySQL database through JDBC.

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

1 package ts. jsj. lyh; 2 3 import java. SQL. *; 4 5/**** // ** 6 * connection to MySQL using JDBC 7 * DataBase: JSJ, table: student; 8 * @ author DuChangfeng 2008 09 18 9 */10 public class JDBCTest {11 12 public static Connection getConnection () throws SQLException, 13 java. lang. classNotFoundException 14 {15 // Step 1: load the MySQL JDBC driver 16 Class. forName ("com. mysql. jdbc. driver "); 17 18 // get the connection url, the user name and password that can access the MySQL database; jsj: database name 19 String url =" jdbc: mysql: // localhost: 3306/jsj "; 20 String username =" root "; 21 String password =" 111 "; 22 23 // Step 2: create an instance of the Connection class with the MySQL database 24 Connection con = DriverManager. getConnection (url, username, password); 25 return con; 26} 27 28 29 public static void main (String args []) {30 try31 {32 // Step 3: obtain the Connection class instance con and use con to create the Statement object class instance SQL _statement33 Connection con = getConnection (); 34 Statement SQL _statement = con. createStatement (); 35 36/**** // *************** perform operations on the database *************/37/ /if a database with the same name exists, delete 38 // SQL _statement.executeUpdate ("drop table if exists student "); 39 // execute an SQL statement to generate a table named student 40 // 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 ));"); 41 // insert data to the table 42 // SQL _statement.executeUpdate ("insert student values (1, 'liing', 98)"); 43 // SQL _statement.executeUpdate ("insert student values (2, 'jiangshanc', 88) "); 44 // SQL _statement.executeUpdate (" insert student values (3, 'wangjiawu ', 78 )"); 45 // SQL _statement.executeUpdate ("insert student values (4, 'duchangfeng', 100)"); 46 // --- the preceding operations are not practical, but it is listed as a reference --- 47 48 // Step 4: execute the query and use the ResultSet class object to return the query result 49 String query = "select * from student "; 50 ResultSet result = SQL _statement.executeQuery (query ); 51/*** // *************** perform operations on the database *************/52 53 System. out. println ("The data in the Student table is as follows:"); 54 System. out. println ("------------------------"); 55 System. out. println ("student ID" + "" + "name" + "" + "data score"); 56 System. out. println ("------------------------"); 57 58 // process the obtained query results and operate 59 while (Result. next () 60 {61 int number = result. getInt ("sno"); 62 String name = result. getString ("sname"); 63 String mathScore = result. getString ("sgrade"); 64 // Get the data in the database 65 System. out. println ("" + number + "" + name + "" + mathScore); 66} 67 68 // close the connection and declare 69 SQL _statement.close (); 70 con. close (); 71 72} catch (java. lang. classNotFoundException e) {73 // JDBC loading error. the 74 System is not found for the driver to be used. err. print ("ClassNotFoundException"); 75 // Other errors 76 System. err. println (e. getMessage (); 77} catch (SQLException ex) {78 // Display database connection error or query error 79 System. err. println ("SQLException:" + ex. getMessage (); 80} 81} 82 83}

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
19 String url = "jdbc: mysql: // localhost: 3306/jsj ";
20 String username = "root ";
21 String password = "111 ";
And the field names to be queried in the basic table:

61 int number = result. getInt ("sno ");
62 String name = result. getString ("sname ");
63 String mathScore = result. getString ("sgrade ");

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

BitsCN.com

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.