JDBC Connection MySQL code and connection failure resolution

Source: Internet
Author: User
Tags mysql code mysql tutorial stmt

Using JDBC to connect to the MySQL Tutorial database tutorial, the JDBC driver has been added to the project "build path" of Eclips tutorial E, which is supposed to be normal. Prompts "Unable to load driver" after running the project.

The final solution is to put the Mysql-connector-java-5.1.7-bin.jar into the "D:javajdk1.6.0_22jrelibext", and then compile and run the above program, it succeeds. As for why, I don't know yet.

Seemingly in the "build path" does not add a driver can also, as long as "%java_home%jrelibext" in the driver can be.


To establish a Connection object

String url= "Jdbc:mysql://localhost:3306/sample_db?user=root&password=your_password";

Connection con = drivermanager.getconnection (URL);

Create SQL Statement object (statement object)

Statement stmt = Con.createstatement ();

Execute SQL statement

ExecuteQuery ()

string query = "SELECT * from Test";

ResultSet rs=stmt.executequery (query);

Result set resultset

while (Rs.next ())

{rs.getstring (1); Rs.getint (2);}

Executeupdate ()

String upd= "INSERT into Test (id,name) VALUES (1001,xuzhaori)";

int con=stmt.executeupdate (UPD);

Execute ()


are not the same in Java programs and Web programs.
There is one of the simplest common methods:
Put the downloaded driver (my Mysql-connector-java-3.1.14-bin.jar) into
In the%java_home%jre/lib/ext directory, the jar package in the Ext directory is automatically loaded when the Java command is executed, so this is a once and for all way, and it works for any one of these operations.
The following is specific:
1. At the command line
Add JDBC-driven (such as Mysql-connector-java-3.1.14-bin.jar) packages to Classpath
2. Using the IDE
If Eclipse can import an external jar package and join the build path, this simple principle
3. Using in Web Programs
Simply put the JDBC driver Jar pack under/web-inf/lib, and of course there's a convenient way to
%tomcat_home%common/lib directory, so Tomcat will automatically load this jar every time it is started, but it is recommended to put it under/web-inf/lib

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.