public List findAll() {
PreparedStatement preparedStatement = null;
ResultSet resultSet = null;
List users = new ArrayList<>();
Connection connection = null;
try {
connection = DBUtil.getConnection(DBUtil.qudong, DBUtil.url);
//3.写sql
String sql = “select * from tb_user”;
//4.得到statement对象
preparedStatement = connection.prepareStatement(sql);
//5.执行sql
resultSet = preparedStatement.executeQuery();
//6.处理结果集
while (resultSet.next()) {
TbUser user = new TbUser();
user.setId(resultSet.getInt(1));
user.setUsername(resultSet.getString(2));
user.setPassword(resultSet.getString(3));
users.add(user);
}
//7.关闭资源
} catch (Exception e) {
e.printStackTrace();
} finally {
DBUtil.CloseAll(resultSet, preparedStatement, connection);
}
return users;
}
public boolean doLogin(String username,String password){
PreparedStatement preparedStatement = null;
ResultSet resultSet = null;
Connection connection = null;
int id;
try {
connection = DBUtil.getConnection(DBUtil.qudong, DBUtil.url);
//3.写sql
String sql = "select * from tb_user where username=? and password=?";
//4.得到statement对象
preparedStatement = connection.prepareStatement(sql);
preparedStatement.setString(1,username);
preparedStatement.setString(2,password);
//5.执行sql
resultSet = preparedStatement.executeQuery();
//6.处理结果集
if(resultSet.next()){
return true;
}
}catch (Exception e){
e.printStackTrace();
}
return false;
}
public static void insert(String username1,String password1){
PreparedStatement preparedStatement=null;
int resultSet;
Connection connection=null;
try {
connection=DBUtil.getConnection(DBUtil.qudong,DBUtil.url);
String sql="insert into tb_user(username,password) values ('"+username1+"','"+password1+"')";
preparedStatement=connection.prepareStatement(sql);
//5.执 行sql
resultSet=preparedStatement.executeUpdate();
//6.处理结果集
//7.关闭资源
} catch (Exception e) {
e.printStackTrace();
}finally {
DBUtil.CloseAll(null,preparedStatement,connection);
}
}
public static void delete(int id){ //delete(int id1){
PreparedStatement preparedStatement=null;
Connection connection=null;
int resultSet;
try {
connection=DBUtil.getConnection(DBUtil.qudong,DBUtil.url);
String sql="delete from tb_user where id=?"; //where id="+id1;
preparedStatement=connection.prepareStatement(sql);
//5.执行sql
preparedStatement.setInt(1,id);
resultSet=preparedStatement.executeUpdate();
//7.关闭资源
} catch (Exception e) {
e.printStackTrace();
}finally {
DBUtil.CloseAll(null,preparedStatement,connection);
}
}
public static void modify(int id,String username,String password){
PreparedStatement preparedStatement=null;
int resultSet;
Connection connection=null;
try {
connection=DBUtil.getConnection(DBUtil.qudong,DBUtil.url);
String sql="update tb_user set username=? , password=? where id=?";
preparedStatement=connection.prepareStatement(sql);
//5.执行sql
preparedStatement.setString(1,username);
preparedStatement.setString(2,password);
preparedStatement.setInt(3,id);
resultSet=preparedStatement.executeUpdate();
//6.处理结果集
//7.关闭资源
} catch (Exception e) {
e.printStackTrace();
}finally {
DBUtil.CloseAll(null,preparedStatement,connection);
}
}