[Reprinted] connection between eclipse and MySQL

Source: Internet
Author: User

Good start: Of course, first download MySQL and eclipse, and MySQL JDBC Driver (my driver is mysql-connector-java-5.1.13)

First, install MySQL (I suggest you install the installed version without any configuration. You can use it After decompressing eclipse. I will not explain it here ):
For general developers, they always click Next during installation to go to "Please select the default set (set the default Character Set) and select the last item (that is: manual selected default Character Set/collation), character set (character set) is set to GBK, click Next, select include bin directory in Windows path (including the root directory in Windows path ), click Next, set the password, and click execute to complete the MySQL configuration.
After completing the configuration of MySQL, MySQL JDBC processing (in fact, very simple single): extract out, copy the mysql-connector-java-5.1.13-bin.jar file inside to eclipse inside (copy to JDK \ bin can also, in fact, it can also not be copied, just to make it easier );

Next, connect eclipse and MYSQL: Open eclipse, file ---> New ----> Project ---> JAVA project ----> library. On this interface, select "add External library ", locate the mysql-connector-java-5.1.13-bin.jar file and click Finish.

Finally, let's see if eclipse can be connected to MySQL. Here is my test code:

Import java. SQL .*;
Public class user {

Public static void main (string [] ARGs ){

Try {
Class. forname ("com. MySQL. JDBC. Driver");/* ------ load the MySQL JDBC driver ----------*/
System. Out. println ("driver loaded successfully! ");
}
Catch (exception e ){
System. Out. Print ("failed to load the driver! ");
E. printstacktrace ();
}

Try {
Connection connect = drivermanager. getconnection (
"JDBC: mysql: // localhost: 3306/MySQL", "root", "Qingmin ");

/* ------------------------ Here, I will explain the meaning of the parameters in getconnection (): the first parameter is the Address URL, the second parameter is the user name, and the third parameter is the password. For the parameter "JDBC: mysql: // localhost: 3306/MySQL", 'mysql' is the original database of the server. If you have created other databases in MySQL, such as the 'Student 'database, if you want to query the student database, replace MySQL here
Student, the following will not change unless you change your login account ------------------------------*/

System. Out. println ("database connection successful! ");

Statement stmt = connect. createstatement ();/* ----- create a statement object to send an SQL statement to the database -----*/
Resultset rs = stmt.exe cutequery ("select * from student");/* ----- query statement. student is a table created in the database ---------*/

While (Rs. Next ()){
System. Out. println (Rs. getstring ("name"); // 'name' is the property to be displayed.
}
}
Catch (exception e ){
System. Out. Print ("An error occurred while obtaining the information! ");
E. printstacktrace ();
}
}
}


By now, I hope it will be helpful to those who have just started.

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.