我想从JAVA里面取出数据库里面的数据怎么取出来呢? 求各位写写代码!
麻烦了!
麻烦了!
解决方案 »
- 关于web.xml中过滤器的配置
- JSP ASP 谁会是未来,谁将主宰
- 100分求登录页面的随机验证码源程序。
- 进来看看!!!!急!sql sever jdbc 加载不到classpath 中
- 求助!!!急!!!
- applet在appletviewer里运行正常,在网页里却报错NoSuchMethodError
- 哪位仁兄见过个这样的问题
- 用java报表工具实现同期比
- 请问request的getSession(false)是什么意思?
- 一个简单的问题,但是实现非常困难,解决者再给300分!!!!!!!
- 请各位高手帮忙看一个错误,采用的是spring3.0mvc
- 火狐下display的问题!求教!
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
public class StudentDaoImp implements StudentDao {
private Connection con =null;
private PreparedStatement ps=null;
private ResultSet rs=null;
private List list=null;
private String addSql="insert into Student"+
"(studentNumber,name ,password,sex ,birthday ,address ,polity ,brief)"+
" values(?,?,?,?,?,?,?,?)";
private String delSql="delete from Student where id=?";
private String selAll="select * from Student";
private String selId="select * from Student where id=?";
private String selName="select * from Student where name=?";
private String login="insert into Student(name,password) values(?,?)";
private String edit="update Student set password=? where studentNumber=?";
public boolean add(Student stu) {
try {
if(con==null){
con=ConnectionFactory.getConnection();
}
ps=con.prepareStatement(addSql);
ps.setString(1, stu.getStudentNumber());
ps.setString(2, stu.getName());
ps.setString(3, stu.getPassword());
ps.setInt(4,stu.getSex());
ps.setDate(5,stu.getBirthday());
ps.setString(6, stu.getAddress());
ps.setInt(7, stu.getPolity());
ps.setString(8,stu.getBrief());
int cout=ps.executeUpdate();
if(cout>0){
return true;
}
else{
return false;
}
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
ConnectionFactory.closeAll(con, ps, null);
}
return false;
}
public boolean delete(Student stu) {
try {
if(con==null){
con=ConnectionFactory.getConnection();
}
ps=con.prepareStatement(delSql);
ps.setLong(1, stu.getId());
int cout=ps.executeUpdate();
if(cout>0){
return true;
}
else{
return false;
}
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return false;
} public boolean edit(Student stu) {
try {
if(con==null){
con=ConnectionFactory.getConnection();
}
ps=con.prepareStatement(edit);
ps.setString(1,stu.getPassword());
ps.setString(2,stu.getStudentNumber());
int cout=ps.executeUpdate();
if(cout>0){
return true;
}
else{
return false;
}
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
ConnectionFactory.closeAll(con, ps, null);
}
return false;
} public List findAll() {
list=new ArrayList();
try {
if(con==null){
con=ConnectionFactory.getConnection();
}
ps=con.prepareStatement(selAll);
rs=ps.executeQuery();
while(rs.next()){
Student stu=new Student();
stu.setId(rs.getLong("id"));
stu.setStudentNumber(rs.getString("studentNumber"));
stu.setName(rs.getString("name"));
stu.setPassword(rs.getString("password"));
stu.setSex(rs.getInt("sex"));
stu.setBirthday(rs.getDate("birthday"));
stu.setAddress(rs.getString("address"));
stu.setPolity(rs.getInt("polity"));
stu.setBrief(rs.getString("brief"));
list.add(stu);
}
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
ConnectionFactory.closeAll(con, ps, rs);
}
return list;
} public Student findById(int id) {
Student stu=null;
try {
if(con==null){
con=ConnectionFactory.getConnection();
}
ps=con.prepareStatement(selId);
ps.setInt(1,id);
rs=ps.executeQuery();
while(rs.next()){
stu=new Student();
stu.setId(rs.getLong("id"));
stu.setStudentNumber(rs.getString("studentNumber"));
stu.setName(rs.getString("name"));
stu.setPassword(rs.getString("password"));
stu.setSex(rs.getInt("sex"));
stu.setBirthday(rs.getDate("birthday"));
stu.setAddress(rs.getString("address"));
stu.setPolity(rs.getInt("polity"));
stu.setBrief(rs.getString("brief"));
}
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
ConnectionFactory.closeAll(con, ps, rs);
}
return stu;
} public Student findByName(String name) {
Student stu=null;
try {
if(con==null){
con=ConnectionFactory.getConnection();
}
ps=con.prepareStatement(selName);
ps.setString(1, name);
rs=ps.executeQuery();
while(rs.next()){
stu=new Student();
stu.setId(rs.getLong("id"));
stu.setStudentNumber(rs.getString("studentNumber"));
stu.setName(rs.getString("name"));
stu.setPassword(rs.getString("password"));
stu.setSex(rs.getInt("sex"));
stu.setBirthday(rs.getDate("birthday"));
stu.setAddress(rs.getString("address"));
stu.setPolity(rs.getInt("polity"));
stu.setBrief(rs.getString("brief"));
}
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
ConnectionFactory.closeAll(con, ps, rs);
}
return stu;
} public boolean login(String name, String pwd) {
try {
if(con==null){
con=ConnectionFactory.getConnection();
}
ps=con.prepareStatement(login);
ps.setString(1,name);
ps.setString(2, pwd);
int cout=ps.executeUpdate();
if(cout>0){
return true;
}
else{
return false;
}
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
ConnectionFactory.closeAll(con, ps, null);
}
return false;
}}
不知道楼主使用的是jdbc查询还是使用框架呢?
求各位大大行行好吧 急啊 做个示范都OK 有些代码 我看不懂 麻烦 您们谢谢注释谢谢!