import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class mysqlManager {
//增
public boolean insert(String username, String pwd) {
String sql = "insert into admin values(?,?)";
Connection conn = JDUtill.getConnection();
PreparedStatement pst = null;
ResultSet rs = null;
boolean number = true;
try {
pst = conn.prepareStatement(sql);
pst.setString(1, username);
pst.setString(2, pwd);
int i=pst.executeUpdate();
if(i!=0){
System.out.println("插入成功");
}else{
number = false;
}
} catch (Exception e) {
// TODO: handle exception
} finally {
JDUtill.closeAll(conn, pst, rs);
}
return number;
}
//删
public boolean delete(String username) {
String sql = "delete from admin where userName=?";
Connection conn = JDUtill.getConnection();
PreparedStatement pst = null;
ResultSet rs = null;
boolean number = true;
try {
pst = conn.prepareStatement(sql);
pst.setString(1, username);
int i=pst.executeUpdate();
if(i!=0){
System.out.println("删除成功");
}else{
number = false;
}
} catch (Exception e) {
// TODO: handle exception
} finally {
JDUtill.closeAll(conn, pst, rs);
}
return number;
}
//改
public boolean update(String username,String pwd){
String sql="update admin set pwd=? where userName=?";
Connection conn = JDUtill.getConnection();
PreparedStatement pst = null;
ResultSet rs = null;
boolean number = true;
try {
pst = conn.prepareStatement(sql);
pst.setString(1, pwd);
pst.setString(2, username);
int i=pst.executeUpdate();
if(i!=0){
System.out.println("修改成功");
}else{
number = false;
}
} catch (Exception e) {
// TODO: handle exception
} finally {
JDUtill.closeAll(conn, pst, rs);
}
return number;
}
//查
public void select(){
Connection conn = JDUtill.getConnection();
Statement st = null;
ResultSet rs = null;
String sql = "select * from student";
try {
st = conn.createStatement();
rs = st.executeQuery(sql);
while(rs.next()){
System.out.print(rs.getString(1) + "\t");
System.out.print(rs.getString(2) + "\t");
System.out.print(rs.getString(3) + "\t");
System.out.print(rs.getString(4) + "\t");
System.out.print(rs.getString(5) + "\t");
System.out.print(rs.getString(6) + "\t");
System.out.print(rs.getString(7) + "\t");
System.out.println(rs.getString(8) + "\t");
}
} catch (Exception e) {
e.printStackTrace();
}finally{
JDUtill.closeAll(conn, st, rs);
}
}
}
JDBC通过PreparedStatement 对数据库进行增删改查
最新推荐文章于 2022-11-05 01:49:35 发布