展开全部
首先你得有一个jdbc的架包
然后e68a8462616964757a686964616f31333361323566import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
public class Util {
public static Connection getConnection(){
Connection con = null;
String user = "root";//数据库登陆用户名
String password = "root";//数据库登陆密码
String url = "jdbc:mysql://localhost:3306/password";//数据库库名
String driver = "com.mysql.jdbc.Driver";
try{
Class.forName(driver);
con = DriverManager.getConnection(url,user,password);
}catch(Exception e){
}
return con;
}
public static void main(String[] args) {
Connection con = getConnection();
String sql = "insert into stu values(null,\'hello\')";
PreparedStatement p = null;
try{
p = con.prepareStatement(sql);
p.executeUpdate();
}catch(SQLException e){
e.printStackTrace();
}finally{
try{
p.close();
con.close();
}catch(SQLException e){
e.printStackTrace();
}
}
}
}