本文将介绍java实现数据库增删改查的操作方法定义的代码,包括statment和preparestatment两种模式,两种的区别可以参考别的文章,按需选用。
例:getdata是statment的查询的方法,pgetdata是preparestatment的查询的方法
需要自己修改用户名、密码、数据库url
添加方法需要每个表单独定义方法(要改哪里一看就会)
package com.example.helloworld.Utils;
import java.sql.*;
public class JDBCUtils {
public static Connection getConn(){
try {
Class.forName("com.mysql.cj.jdbc.Driver");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
Connection conn = null;
//mysql用户名
String userName = "root";
//mysql密码
String password = "password";
//数据库URL
String url = "jdbc:mysql://localhost:3306/<数据库名>?useSSL=false";
try {
conn = DriverManager.getConnection(url, userName, password);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return conn;
}
//查询
public static String GetData(String table,String column,String name,String col){
Connection conn = getConn();
String a = null;
Statement stmt;
try {
stmt = conn.createStatement();
String sql;
sql = "SELECT * FROM "+table + " WHERE " + column + "='" + name+"'";
ResultSet rs = stmt.executeQuery(sql);
while (rs.next()) {
a=rs.getString(col);
}
rs.close();
stmt.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return a;
}
public static String pGetData(String table, String column, String name, String col) {
Connection conn = getConn();
String a = null;
PreparedStatement ps = null;
try {
//创建prepareStatement对象
String sql = "SELECT * FROM "+table+" WHERE "+column+" = ?";
ps = conn.prepareStatement(sql);
ps.setString(1, name);
ps.execute();//执行
System.out.println(ps.execute());//执行表输出返回的结果,结果为false,因为没有返回的结果集
//处理结果集
ResultSet rs=ps.executeQuery();
while(rs.next()){
a=rs.getString(col);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
//关闭资源
try {
if (ps != null) ps.close();
} catch (SQLException e) {
e.printStackTrace();
}
try {
if (conn != null) conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
return a;
}
//更新
public static void UpdateData(String table, String column, String name, String col , String newdata) {
Connection conn = getConn();
Statement stmt;
try {
stmt = conn.createStatement();
String sql;
sql = "UPDATE "+ table + " SET " + col + " = '"+newdata + "' WHERE " + column + " = '"+name+"'";
stmt.executeUpdate(sql);
stmt.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void pUpdateData(String table, String column, String name, String col , String newdata) {
Connection conn = getConn();
PreparedStatement ps = null;
try {
//创建prepareStatement对象
String sql = "UPDATE "+table+" SET "+col+" = ? WHERE "+column+" = ?";
ps = conn.prepareStatement(sql);
//执行sql语句
//ps.setString(1, table);
//ps.setString(2, col);
ps.setString(1, newdata);
//ps.setString(4, column);
ps.setString(2, name);
//java.util.Date utilDate = new java.util.Date();//进行类型转换,由util类型的date转化为sql类型的
ps.execute();//执行
System.out.println(ps.execute());//执行表输出返回的结果,结果为false,因为没有返回的结果集
//处理结果集
} catch (Exception e) {
e.printStackTrace();
} finally {
//关闭资源
try {
if (ps != null) ps.close();
} catch (SQLException e) {
e.printStackTrace();
}
try {
if (conn != null) conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
//删除
public static void DeleteData(String table,String col,String name){
Connection conn = getConn();
Statement stmt;
try {
stmt = conn.createStatement();
String sql;
sql = "DELETE FROM "+table + " WHERE " + col + "='" + name+"'";
stmt.executeUpdate(sql);
stmt.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void pDeleteData(String table,String col,String name){
Connection conn = getConn();
PreparedStatement ps = null;
try {
//创建prepareStatement对象
String sql = "DELETE FROM "+table + " WHERE " + col + "= ?";
ps = conn.prepareStatement(sql);
//执行sql语句
ps.setString(1, name);
//java.util.Date utilDate = new java.util.Date();//进行类型转换,由util类型的date转化为sql类型的
ps.execute();//执行
System.out.println(ps.execute());//执行表输出返回的结果,结果为false,因为没有返回的结果集
//处理结果集
} catch (Exception e) {
e.printStackTrace();
} finally {
//关闭资源
try {
if (ps != null) ps.close();
} catch (SQLException e) {
e.printStackTrace();
}
try {
if (conn != null) conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
//添加
public static void AddDatastu(String name1,String password1,String qqaccount1){
Connection conn = getConn();
Statement stmt;
try {
stmt = conn.createStatement();
String sql;
sql = "insert into stu(stuname,password,qq) values ('"+name1+"','"+password1+"','"+qqaccount1+"');";
stmt.executeUpdate(sql);
stmt.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void pAddDatastu(String name1,String password1,String qqaccount1){
Connection conn = getConn();
PreparedStatement ps = null;
try {
//创建prepareStatement对象
String sql = "insert into stu(stuname,password,qq) values (?,?,?)";
ps = conn.prepareStatement(sql);
//执行sql语句
ps.setString(1, name1);
ps.setString(2,password1);
ps.setString(3,qqaccount1);
//java.util.Date utilDate = new java.util.Date();//进行类型转换,由util类型的date转化为sql类型的
//ps.execute();
// 执行
System.out.println(ps.execute());//执行表输出返回的结果,结果为false,因为没有返回的结果集
//处理结果集
} catch (Exception e) {
e.printStackTrace();
} finally {
//关闭资源
try {
if (ps != null) ps.close();
} catch (SQLException e) {
e.printStackTrace();
}
try {
if (conn != null) conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}