First, download the Mysql-connector-java-x.x.x-bin.jar package
- Download Mysql-connector-java-x.x.x-bin.jar, currently the latest version of Mysql-connector-java-5.1.22-bin.jar. (
- In Eclipse, select the project---Properties->java build path, and in the Libraries tab, select Add external jar to locate the downloaded Mysql-connector-java-5.1.22-bin.jar file and add it to the project.
- Compile again, the program runs normally! (Can be tested using the above code)
Ii. Examples of JDBC application experiments in Java
1 Packagecom.baorant;2 3 Importjava.sql.Connection;4 ImportJava.sql.DriverManager;5 Importjava.sql.PreparedStatement;6 Importjava.sql.SQLException;7 8 Public classJavademo {9 Ten Public Static voidMain (string[] args)throwsException { OneString username = "root"; AString password = "li19941101"; -String url = "Jdbc:mysql://localhost:3306/mvc_user"; - Connection Conn; the Try { - //1. Load the database driver -Class.forName ("Com.mysql.jdbc.Driver"); -conn =drivermanager.getconnection (URL, username, password); +SYSTEM.OUT.PRINTLN ("Database connection succeeded! "); - + //The second step: Operation database; AString sql= "INSERT into User_info (User_id,user_name,password) VALUES (5, ' song ', ' SS ')"; atPreparedStatement pt=conn.preparestatement (SQL); - pt.executeupdate (); - //Step three: Close the database; - conn.close (); -SYSTEM.OUT.PRINTLN ("Database operation succeeded! "); -}Catch(SQLException e) { in //TODO auto-generated Catch block - e.printstacktrace (); to } + } -}
JDBC Application Sample Experiment in Java