首先建立一个数据库工具类,如下:
public class JDBCUtil {
private static final String driver = "com.mysql.cj.jdbc.Driver";
private static final String url = "jdbc:mysql://localhost:3306/数据库名?useSSL=false&serverTimezone=UTC&useUnicode=true&characterEncoding=utf8";
private static final String userName = "root";
//用户密码
private static final String password = "*****";
// 获取数据库连接
public static Connection getConnection() {
Connection con = null;
try {
Class.forName(driver);
con = DriverManager.getConnection(url, userName, password);
} catch (SQLException | ClassNotFoundException e) {
e.printStackTrace();
}
return con;
}
// 数据库查询,返回结果集
public static ResultSet query(Connection con, PreparedStatement st, ResultSet rs, String sql
, Object[] params) throws SQLException {
st = con.prepareStatement(sql, ResultSet.TYPE_SCROLL_SENSITIVE,
ResultSet.CONCUR_READ_ONLY);
if (params != null) {
for (int i = 0; i < params.length; i++) {
st.setObject(i + 1, params[i]);
}
}
rs = st.executeQuery();
return rs;
}
// 数据库增删改
public static int update(Connection con, String sql
, Object[] params, ResultSet rs, PreparedStatement st) throws SQLException {
st = con.prepareStatement(sql);
for (int i = 0; i < params.length; i++) {
st.setObject(i + 1, params[i]);
}
return st.executeUpdate();
}
// 关闭数据库连接
public static void release(Connection con, Statement st, ResultSet rs) {
boolean flag = true;
if (rs != null) {
try {
rs.close();
rs = null;
} catch (SQLException e) {
e.printStackTrace();
flag = false;
}
}
if (st != null) {
try {
st.close();
st = null;
} catch (SQLException e) {
e.printStackTrace();
flag = false;
}
}
if (con != null) {
try {
con.close();
con = null;
} catch (SQLException e) {
e.printStackTrace();
flag = false;
}
}
}
}
使用工具类进行数据库操作(操作表以book2为例)
(1)查询
public class Test {
public static void main(String[] args) {
Connection con=null;
// 创建statement类对象,用来执行SQL语句
PreparedStatement st=null;
ResultSet rs=null;
//获取数据库连接
con=JDBCUtil.getConnection();
//查询
String sql="SELECT * FROM book2 WHERE id=?";
try {
st=con.prepareStatement(sql);
//查询id=2的数据行
st.setInt(1,2);
// 通过SQL查询,把查询到的结果返回至Result的结果集
rs=st.executeQuery();
while (rs.next()) {
int id = rs.getInt("id");
String name = rs.getString("name");
double price = rs.getDouble("price");
System.out.println(id + " " + name + " " + price);
}
} catch (SQLException e) {
throw new RuntimeException(e);
}
JDBCUtil.release(con,st,rs);
}
}
运行结果如下
(2)增加
public class Test2 {
public static void main(String[] args) {
Connection con=null;
PreparedStatement st=null;
ResultSet rs=null;
//获取数据库连接
con=JDBCUtil.getConnection();
//插入name=XXX的数据
String sql="INSERT INTO book2 VALUES( 'XXX', 43.00,2,11)";
try {
st=con.prepareStatement(sql);
// 通过SQL更新数据库内容,把受影响的行数返回
int i=st.executeUpdate();
if (i!=1){
System.out.println("插入失败");
}else{
System.out.println("插入成功");
}
} catch (SQLException e) {
throw new RuntimeException(e);
}
JDBCUtil.release(con,st,rs);
}
}
插入后表如下所示:
(3)修改
与增加操作类似,将String sql=“delete from book2 where name=‘XXX’”;改为update book2 set price=10 where name=‘XXX’;即可将XXX的价格改为10。
也可使用工具类中的update方法。
String sql="update book2 set price=10 where name='XXX'";
(4)删除
同上所述,可做如下修改:
String sql="delete from book2 where name='XXX'";
将XXX的数据删除。