首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > Eclipse开发 >

学的不精,找不出异常了。求大神看看

2013-07-09 
学的不精,找不出错误了。求大神看看这是做的一个简单的信息管理系统。import java.sql.Connectionimport ja

学的不精,找不出错误了。求大神看看
这是做的一个简单的信息管理系统。

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;

import model.Depart;   ------这两句出现问题了,model是一个包?但是建立这个包后又出现一大堆错误
import model.Student;

public class Student_Dao {

public boolean addInfo(Student student){      -------Student这里显示出错了,为啥
boolean flag;
Connection con = DbConnection.getConnection();
PreparedStatement ps;
String sql = "";
//ResultSet rs;
try {
sql = "insert into student values(?,?,?,?,?,?)"; //用问号代替

ps = con.prepareStatement(sql);
ps.setInt(1, student.getId());
ps.setString(2, student.getName());
ps.setString(3, student.getSex());
ps.setString(4, student.getBorn_date());
ps.setString(5, student.getDepart_id());
ps.setString(6, student.getClass_name());
ps.execute();
System.out.println("添加了一条记录");
flag = true;
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
flag =  false;
}finally{
DbConnection.closeConnection(con);
}
return flag;
}




public ArrayList<Student> findAll(){                         -----各种Student显示出错
ArrayList<Student> students = new ArrayList<Student>();
Connection con = DbConnection.getConnection();
Statement sta;
ResultSet rs;
try {
sta = con.createStatement();
rs = sta.executeQuery("SELECT * FROM student");  //从表depart中查询所有数据
while(rs.next()){
Student student = new Student();
student.setId(rs.getInt(1));
student.setName(rs.getString(2));
student.setSex(rs.getString(3));
student.setBorn_date(rs.getString(4));
student.setDepart_id(rs.getString(5));
student.setClass_name(rs.getString(6));

System.out.println(student.getName());
students.add(student);
}
rs.close();
sta.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
DbConnection.closeConnection(con);
}
return students;


}

public Student findOne(int id){
Student student = new Student();
Connection con = DbConnection.getConnection();
PreparedStatement ps;
String sql = "";
ResultSet rs;
try {
sql = "SELECT * FROM student where id=?";
ps = con.prepareStatement(sql);
ps.setInt(1, id);
rs = ps.executeQuery();  //从表depart中查询所有数据
if(rs.next()){
student.setId(rs.getInt("id"));  //注意数据类型要对应
student.setName(rs.getString("Name"));
student.setSex(rs.getString("Sex"));
student.setBorn_date(rs.getString("birthday"));
student.setDepart_id(rs.getString("Depart"));
student.setClass_name(rs.getString("Class"));
System.out.println("id:"+student.getId()+"  name:"+student.getName());
}else{
System.out.println("没有数据");
}
rs.close();
ps.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
DbConnection.closeConnection(con);
}
return student;
}

public boolean deleteInfo(Student student){
boolean flag;
Connection con = DbConnection.getConnection();
PreparedStatement ps;
String sql = "";
try {
sql = "delete from student where  id=?"; //用问号代替

ps = con.prepareStatement(sql);
ps.setInt(1, student.getId());
ps.execute();
System.out.println("成功删除了一条记录");
flag = true;
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
flag =  false;
}finally{
DbConnection.closeConnection(con);
}
return flag;
}
}

其他部分也有错误,可能看这一点代码根本不知道说的是什么。大神留下你的联系方式吧。 java
[解决办法]
这是个什么东东哦,可以加qq:1040888918

热点排行