项目结构
adminbean
package pers.ant.oe.bean;
public class adminBean {
private Integer pid;
private String pwd;
public Integer getPid() {
return pid;
}
public void setPid(Integer pid) {
this.pid = pid;
}
public String getPwd() {
return pwd;
}
public void setPwd(String pwd) {
this.pwd = pwd;
}
@Override
public String toString() {
return "adminBean [pid=" + pid + ", pwd=" + pwd + "]";
}
public adminBean(Integer pid, String pwd) {
super();
this.pid = pid;
this.pwd = pwd;
}
}
admindao
package pers.ant.oe.dao;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import pers.ant.oe.bean.adminBean;
import pers.ant.oe.jdbc.jdbc;
public class adminDao {
public static adminBean check(int aid) {
adminBean bean = null;
String sql = "select * from admin where aid= '" + aid + "';";
Connection connection = jdbc.getConn();
Statement statement = null;
ResultSet resultSet = null;
try {
statement = connection.createStatement();
resultSet = statement.executeQuery(sql);
if (resultSet.next())
bean = new adminBean(aid,resultSet.getString(1));
} catch (Exception e) {
e.printStackTrace();
} finally {
jdbc.close(connection, statement, resultSet);
}
return bean;
}
public static adminBean check(int aid,String pwd) {
adminBean bean = null;
String sql = "select * from admin where aid= '" + aid + "' and pwd='"+ pwd+"';";
Connection connection = jdbc.getConn();
Statement statement = null;
ResultSet resultSet = null;
try {
statement = connection.createStatement();
resultSet = statement.executeQuery(sql);
if (resultSet.next())
bean = new adminBean(aid,resultSet.getString(2));
} catch (Exception e) {
e.printStackTrace();
} finally {
jdbc.close(connection, statement, resultSet);
}
return bean;
}
public static boolean add(int aid, String pwd) {
if(check(aid)!=null) {
System.out.println("该管理员编号已存在");
return false;
}
String sql = "insert into admin values('" + aid + "','" + pwd + "');";
Connection connection= jdbc.getConn();
Statement statement = null;
try {
statement= connection.createStatement();
statement.executeUpdate(sql);
} catch (Exception e) {
e.printStackTrace();
}finally {
jdbc.close(connection, statement, null);
}
return true;
}
public static void main(String []args) {
int aid=1001;
String pwdString="admin";
if(add(aid,pwdString)) {
System.out.println("添加成功");
}
add(aid, pwdString);
adminBean bean=check(1001,"admin");
if(bean==null) {
System.out.println("不存在该用户");
}else {
System.out.println(bean);
}
}
}