标签:编译 drive strong ack size http str rac mysql
IEDA的基本六步操作
案例
package cn.kgc;
import Utile.Test;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
public class Lianxi {
public static void main(String[] args) throws Exception {
/*连接数据库*/
Connection connection = Test.getConnection();
/*编写带有?sql语句*/
String sql = "INSERT INTO used (id,NAME,age) VALUES (?,?,?)";
/*预编译*/
PreparedStatement pstm = connection.prepareStatement(sql);
/*填充占位符*/
pstm.setObject(1,4);
pstm.setObject(2,"天");
pstm.setObject(3,67);
//5.执行
pstm.executeUpdate();
/* 6.关闭流*/
Test.close(pstm,connection);
}
}
为了方便使用可以把连接数据库和关闭驱动写出一个方法,需要使用直接调用即可
package Utile;
import java.sql.*;
public class Test {
public static Connection getConnection(){
Connection connection=null;
try {
Class.forName("com.mysql.jdbc.Driver");
connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/kh87", "root", "123789");
} catch (Exception e) {
e.printStackTrace();
}
//连接数据库
return connection;
}
public static void close(PreparedStatement pstm, Connection connection){
try {
pstm.close();
} catch (SQLException e) {
e.printStackTrace();
}
try {
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
public static void close2(ResultSet rs,PreparedStatement pstm, Connection connection){
try {
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
try {
pstm.close();
} catch (SQLException e) {
e.printStackTrace();
}
try {
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
执行结果:
标签:编译 drive strong ack size http str rac mysql
原文地址:https://www.cnblogs.com/li-ding-yong/p/13197519.html