📄 accountdao.java~62~
字号:
package com.lx.test;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.*;
import java.util.List;
import java.util.ArrayList;
public class AccountDao {
public boolean inserAccount(String name, Integer age, String sex) {
boolean flag = false;
Connection conn = null;
PreparedStatement ps = null;
try {
conn = MyConnection.getConnection();
ps = conn.prepareStatement("insert into student values(?,?,?)");
ps.setString(1, name);
ps.setInt(2, age);
ps.setString(3, sex);
if (ps.executeUpdate() == 1) {
flag = true;
}
} catch (Exception ex) {
ex.printStackTrace();
} finally {
try {
ps.close();
conn.close();
} catch (Exception ex) {
ex.printStackTrace();
}
}
return flag;
}
public boolean genxin(Integer id, String name, Integer age, String sex) {
boolean flag = false;
Connection conn = null;
conn = MyConnection.getConnection();
PreparedStatement ps = null;
try {
ps = conn.prepareStatement(
"update student set name=?,age=?,sex=? where id=?");
ps.setString(1, name);
ps.setInt(2, age);
ps.setString(3, sex);
ps.setInt(4, id);
if (ps.executeUpdate() == 1) {
flag = true;
}
} catch (SQLException ex) {
} finally {
//关闭数据库连接
}
return flag;
}
public List selectByName(String name) {
List list = new ArrayList();
Connection conn = null;
PreparedStatement ps = null;
ResultSet rs = null;
try {
conn = MyConnection.getConnection();
ps = conn.prepareStatement(
"select * from student where name like ?");
ps.setString(1, name);
rs = ps.executeQuery();
Student stu=null;
while(rs.next()){
stu=new Student();
stu.setId(rs.getInt("id"));
stu.setName(rs.getString("name"));
stu.setAge(rs.getInt("age"));
stu.setSex(rs.getString("sex"));
list.add(stu);
}
} catch (Exception ex) {
ex.printStackTrace();
}
return list;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -