package qsap;
public class qsap {
String id;
String name;
String place;
public qsap(String id,String name,String place)
{
this.id=id;
this.name=name;
this.place=place;
}
public qsap(String name,String place)
{
this.name=name;
this.place=place;
}
public void setId(String id) {
this.id=id;
}
public void setName(String name) {
this.name=name;
}
public void setPlace(String place) {
this.place=place;
}
public String getId() {
return id;
}
public String getName() {
return name;
}
public String getPlace() {
return place;
}
}
package Dao;
import qsap.qsap;
import util.DBUtil;
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 boolean add(qsap qsap) {
String sql = "insert into qsap(id,name,place) values('" + qsap.getId() + "','" + qsap.getName() + "','" + qsap.getPlace() + "')";
Connection conn = DBUtil.getConn();
Statement state = null;
boolean f = false;
int a = 0;
try {
state = conn.createStatement();
a = state.executeUpdate(sql);
} catch (Exception e) {
e.printStackTrace();
} finally {
DBUtil.close(state, conn);
}
if (a > 0) {
f = true;
}
return f;
}
public boolean leibie(String id) {
boolean flag = false;
if(id.length()>20)return true;
String sql = "select zhuti from qsap where id = '" + id + "'";
Connection conn = DBUtil.getConn();
Statement state = null;
ResultSet rs = null;
try {
state = conn.createStatement();
rs = state.executeQuery(sql);
while (rs.next()) {
flag = true;
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
DBUtil.close(rs, state, conn);
}
return flag;
}
public List<qsap> list() {
String sql = "select * from qsap";
List<qsap> list = new ArrayList<>();
Connection conn = DBUtil.getConn();
Statement state = null;
ResultSet rs = null;
try {
state = conn.createStatement();
rs = state.executeQuery(sql);
qsap bean = null;
while (rs.next()) {
String id = rs.getString("id");
String name = rs.getString("name");
String place = rs.getString("place");
bean = new qsap(id,name,place);
list.add(bean);
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
DBUtil.close(rs, state, conn);
}
return list;
}
public boolean update(qsap qsap,String id) {
String sql = " UPDATE qsap SET id='" + qsap.getId() + "', name ='" + qsap.getName()+ "' ;";
boolean f = false;
Connection conn = DBUtil.getConn();
Statement state = null;
int a = 0;
try {
state = conn.createStatement();
a= state.executeUpdate(sql);
} catch (SQLException e) {
e.printStackTrace();
} finally {
DBUtil.close(state, conn);
}
if (a > 0) {
f = true;
}
return f;
}
public qsap getByName(String id) {
String sql = "select * from qsap where id ='" + id + "'";
Connection conn = DBUtil.getConn();
Statement state = null;
ResultSet rs = null;
qsap qsap= null;
try {
state = conn.createStatement();
rs = state.executeQuery(sql);
while (rs.next()) {
String name = rs.getString("name");
String place = rs.getString("place");
qsap = new qsap(name,place);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
DBUtil.close(rs, state, conn);
}
return qsap;
}
public List<qsap> getdel(String id)
{ String sql = "select * from qsap where id ='" + id + "'";
List<qsap> list = new ArrayList<>();
Connection conn = DBUtil.getConn();
Statement state = null;
ResultSet rs = null;
qsap qsap= null;
try {
state = conn.createStatement();
rs = state.executeQuery(sql);
while (rs.next()) {
String name = rs.getString("name");
String place = rs.getString("place");
qsap = new qsap(id,name,place);
list.add(qsap);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
DBUtil.close(rs, state, conn);
}
return list;
}
}
今天完成了大部分的代码,目标是完成一个简单的寝室管理系统包括学生学号,学生姓名,学生寝室。在明天就应该能完成了,今天学习时间为两个小时。
标签:String,23,qsap,rs,state,2023,id,name From: https://www.cnblogs.com/y1126/p/17149599.html