JDBC:就是用JAVA操作数据库;
以下是代码:
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
public class Myjdbc {
private static final String URL = “jdbc:mysql://localhost:3306/user_cmx”;
private static final String USERNAME = “root”;
private static final String PWD = “password”;
public static void updata() {
Statement stmt = null;
Connection connection = null;
try {
Class.forName("com.mysql.jdbc.Driver");
System.out.println("Success loading Mysql Driver!");
connection = DriverManager.getConnection(URL, USERNAME, PWD);
System.out.println("Success connect Mysql server!");
stmt = connection.createStatement();
String sql = "insert into user(name,age) values ('eclipse','123');";
int count = stmt.executeUpdate(sql);
if (count > 0) {
System.out.println("操作成功");
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch(SQLException e){
e.printStackTrace();
}catch(Exception e){
e.printStackTrace();
}
finally {
try{
if(stmt!=null) stmt.close();
if(connection!=null)connection.close();
}catch (Exception e) {
e.printStackTrace();
}
}
}
public static void main(String[] args) {
updata();
}
}
有三种工具操作数据库,NAVICAT,CMD,JAVA,
JAVA就是在eclipse上用JDBC操作,Driver Manager管理JDBC驱动,Connection建立链接,Statement增删改查,CallableStatement调用数据库中的函数,Result返回结果集。
1导入驱动Class.forName(“com.mysql.jdbc.Driver”);去掉.class,