import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
public class DAO {
//添加咖啡信息
public void add(Login xiugai) {
String sql = "insert into denglu1 values("
+ xiugai.getSupid() + ",'"
+ xiugai.getSupname() + "','"
+ xiugai.getMima() + "')";
try (Connection con = SQL.getConnection();
Statement stem = con.createStatement();) {
int result = stem.executeUpdate(sql);
if (result > 0) {
System.out.println("添加成功");
} else {
System.out.println("添加失败");
}
} catch (SQLException ex) {
ex.printStackTrace();
}
}
//修改已有信息
public void update(Login xiugai) {
String sql1 = "update denglu1 set id="
+ xiugai.getSupid()
//+ ",where name='" + xiugai.getSupname()
+ ",password='" + xiugai.getMima()
+ "',where name='" + xiugai.getSupname()
+ "'";
try (Connection con = SQL.getConnection();
Statement stem = con.createStatement();) {
int result = stem.executeUpdate(sql1);
if (result > 0) {
System.out.println("修改成功");
} else {
System.out.println("修改失败");
}
} catch (SQLException ex) {
ex.printStackTrace();
}
}
//删除指定信息
public void remove(String xiugaiName) {
String sql2 = "delete from denglu1 where name='" + xiugaiName + "'";
try (Connection con = SQL.getConnection();
Statement stem = con.createStatement();) {
int result = stem.executeUpdate(sql2);
if (result > 0) {
System.out.println("删除成功");
} else {
System.out.println("删除失败");
}
} catch (SQLException ex) {
ex.printStackTrace();
}
}
//查看所有信息
public List<Login> findAll() {
List<Login> denglu1 = new ArrayList<>();
String sql3 = "select * from denglu1";
try (Connection con = SQL.getConnection();
Statement stem = con.createStatement();
ResultSet re = stem.executeQuery(sql3)) {
while (re.next()) {
Login xiu = new Login();
xiu.setSupid(re.getInt("id"));
xiu.setSupname(re.getString("name"));
xiu.setMima(re.getString("password"));
denglu1.add(xiu);
}
} catch (SQLException ex) {
}
return denglu1;
}
//按照名称查询指定信息
public Login find(String xiugaiName) {
String sql4 = "select id,name,password from denglu1 where name='" + xiugaiName + "'";
Login xiu = null;
try (Connection con = SQL.getConnection();
Statement stem = con.createStatement();) {
ResultSet re = stem.executeQuery(sql4);
while (re.next()) {
xiu = new Login();
xiu.setSupname(re.getString("name"));
xiu.setSupid(re.getInt("id"));
xiu.setMima(re.getString("password"));
}
} catch (SQLException ex) {
}
return xiu;
}
}