This article mainly introduces the connection between Java and MySQL .
1. Install JDK slightly ~ There are many in the garden
2. Install MySQL slightly ~
3. Download and install JDBC, download http://dev.mysql.com/downloads/connector/j/#current-tab via MySQL website
4. Install JDBC, run directly, after installation is complete: mine is the file with JDBC installed under C drive C:\Program Files\mysql\mysql Connector J
5. Open Eclipse, on the project that needs to introduce JDBC: right- click build path --
in your drive JAR where the bag is stored choose your JAR Package - Open
This is where you're going to see you. JAR The package already exists the last choice OK just fine. This way your database driver has been successfully introduced.
5. Testing
Import java.sql.*;
public class Jdbctest {
public static Connection getconnection () throws Exception {
String drivername= "Com.mysql.jdbc.Driver"; Loading the JDBC Driver
String dburl= "Jdbc:mysql://localhost:3306/test";
String username= "SA";
String password= "123456";
Class.forName (drivername);
Return drivermanager.getconnection (Dburl,username,password);
}
public static void Main (string[] Aras) {
try {
String sql= "INSERT into mytest (name,content) VALUES (' 111 ', ' xxx ')";
PreparedStatement ps=getconnection (). preparestatement (SQL);
int num = Ps.executeupdate ();
System.out.println ("OK" +num);
} catch (Exception e) {
Todo:handle exception
}
System.out.println ("successing~~~");
}
}
Building a Java and MySQL development environment based on eclipse