JDBC connection to MySQL code and connection failure Solution

Source: Internet
Author: User
Tags mysql code mysql tutorial

Using jdbc to connect to the mysql tutorial database tutorial, The jdbc driver has been added to the "build path" of eclips tutorial e. It is reasonable to say that everything is normal. After running the project, the system prompts "driver cannot be loaded ".

The final solution is to put the mysql-connector-java-5.1.7-bin.jar in "d: javajdk1.6.0 _ 22jrelibext", and then compile and run the above program, it is successful. I still don't know why.

 

It seems that the driver is not added to the "build path", as long as there is a driver in "% java_home % jrelibext.


Create a connection object

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

Connection con = drivermanager. getconnection (url );

Create an SQL statement object)

Statement stmt = con. createstatement ();

Execute SQL statements

Executequery ()

String query = "select * from test ";

Resultset rs1_stmt.exe cutequery (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 conw.stmt.exe cuteupdate (upd );

Execute ()


Java and web programs are not the same.
There is a simple general method:
Put the downloaded Driver (I am using a mysql-connector-java-3.1.14-bin.jar)
In the % java_home % jre/lib/ext directory, the jar package under the ext directory is automatically loaded when the java command is executed, so this is a permanent solution, it works for any of the above running methods.
The following details:
1. In the command line
Add a jdbc Driver (such as a mysql-connector-java-3.1.14-bin.jar) package to classpath
2. Use ide
If it is eclipse, You can import external jar packages and add them to the build path. This is simple and the principle is the same as above.
3. Use in web programs
Directly put the jdbc driver jar package under/web-inf/lib. Of course, there is also a convenient way to put
% Tomcat_home % common/lib directory, so tomcat will automatically load this jar at each startup, 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.