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