MyEclipse Basic Introduction to MySQL database connection via JDBC

Source: Internet
Author: User
Tags getmessage

Reprinted from: http://www.jb51.net/article/31876.htm

1. If MyEclipse has been able to develop the Java project normally
2. Install MySQL
The individual is using the version is Mysql-5.0.22-win32.zip
Website: http://www.mysql.com/downloads/mysql/#downloads
3. Download the JDBC driver
Personal use of the mysql-connector-java-5.1.22.zip, what is needed is the Mysql-connector-java-5.1.22-bin.jar after decompression
Website: http://www.mysql.com/downloads/connector/j/

4. Code Testing  

1 Package Ts.jsj.lyh;2 3Import java.sql.*;4 5 /** */6 /**7 * Use JDBC to connect to the database MySQL process DATABASE:JSJ, table:student;8  * 9 * @author DuchangfengTen  */ One  Public classJdbctest { A  -      Public StaticConnection getconnection () throws SQLException, - java.lang.ClassNotFoundException { the         //first step: Load MySQL's JDBC driver -Class.forName ("Com.mysql.jdbc.Driver"); -  -         //get the URL of the connection, can access the MySQL database user name, password; JSJ: Database name +String URL ="Jdbc:mysql://localhost:3306/mydata"; -String username ="Root"; +String Password =""; A  at         //Step Two: Create an instance of the connection class with the MySQL database -Connection con =drivermanager.getconnection (URL, username, password); -         returncon; -     } -  -      Public Static voidMain (String args[]) { in         Try { -             //Step Three: Get the connection class instance con, create an instance of the statement object class with Con sql_statement toConnection con =getconnection (); +Statement sql_statement =con.createstatement (); -  the             /** */ *             /************ related operations to the database ************/ $             //if a database with the same name exists, deletePanax Notoginseng             //sql_statement.executeupdate ("drop table if exists student"); -             //An SQL statement was executed to generate a table named student the             //sql_statement.executeupdate ("CREATE TABLE student (ID int not NULL auto_increment, name varchar () = NOT NULL default ' Name ', math int not NULL default, primary key (ID)); "); +             //inserting data into a table A             //sql_statement.executeupdate ("Insert student values (1, ' liying ', 98)"); the             //sql_statement.executeupdate ("Insert student values (2, ' Jiangshan ',") "); +             //sql_statement.executeupdate ("Insert student values (3, ' Wangjiawu ',") "); -             //sql_statement.executeupdate ("Insert student values (4, ' Duchangfeng ',") "); $             //---above is not practical, but is listed as a reference--- $  -             //Fourth Step: Execute the query, with the object of the ResultSet class, return the results of the query -String query ="SELECT * FROM Student"; theResultSet result =sql_statement.executequery (query); -             /** */Wuyi             /************ related operations to the database ************/ the  -System. out. println ("the data in the student table is as follows:"); WuSystem. out. println ("------------------------"); -System. out. println ("School Number"+" "+"name"+" "+"Data Score"); AboutSystem. out. println ("------------------------"); $  -             //processes the resulting query results and operates on the object of the result class -              while(Result.next ()) { -                 intNumber = Result.getint ("Sno"); AString name = Result.getstring ("sname"); +String Mathscore = result.getstring ("Sgrade"); the                 //get data from the database -System. out. println (" "+ number +" "+ name +" "+mathscore); $             } the  the             //closing connections and claims the sql_statement.close (); the con.close (); -  in}Catch(java.lang.ClassNotFoundException e) { the             //a JDBC error was loaded and the driver used was not found theSystem.err.print ("classnotfoundexception"); About             //Other Errors the System.err.println (E.getmessage ()); the}Catch(SQLException ex) { the             //Display database connection errors or query errors +System.err.println ("SQLException:"+ex.getmessage ()); -         } the     }Bayi  the}

Add a few points that you personally think you need to be aware of:

1) about the storage location of the Mysql-connector-java-5.1.22-bin.jar. Create a new folder for the jar package (such as Lib) in MyEclipse specific Java project, copy the Mysql-connector-java-5.1.22-bin.jar into the folder, select the Jar package right-click--->build Path--->add to Build path.

If it appears 

Classnotfoundexceptioncom.mysql.jdbc.driver 

is the result of a missing import jar package.  

2) If you are already familiar with MySQL usage, you can ignore this one. When the individual is testing the connection, the exception prompt always appears:  

Sqlexception:communications Link failure< Span class= "Apple-converted-space" >&NBSP;
the last packet sent successfully to the server was 0 milliseconds ago. The driver has no received any packets from the server. &NBSP;

This is because the individual is not familiar with MySQL, a lot of attempts to MySQL, I do not know when inadvertently the MySQL service ( If there is no change when installing MySQL, the default service name is MySQL) shutdown, the workaround is to open the service. Control Panel---> Administrative Tools---> Services--->mysql---> select Enabled. &NBSP;

3) when using the above code test, the place to be changed is: //mysql database user name, password, database name &NBSP;

1 " JDBC:MYSQL://LOCALHOST:3306/JSJ "  2"root"3"111" 

and the name of the field in the underlying table that you want to query:

1 int number = Result.getint ("Sno"2 String name = Result.getstring ("sname"3 String mathscore = result.getstring ( " Sgrade "

MyEclipse Basic Introduction to MySQL database connection via JDBC

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.