在数据库中插入数据

时间:2015-01-07 04:27:00

标签: java sql jdbc

我有以下文件:

  1. Student.sql
  2. 的index.jsp
  3. sampleReport.jsp
  4. StudentServlet(com.ydp.controller)
  5. StudentDao(com.ydp.dao)
  6. StudentDaoImpl(com.ydp.dao.impl)
  7. 学生(com.ydp.domains)。
    我的数据库连接是 jdbc:derby:// localhost:1527 / sample [APP上的app]。
  8. StudentDaoImpl.java

    public class StudentDaoImpl implements StudentDao {
    private DataSource ds;
    private Connection con;
    private PreparedStatement preparedStatement;
    private ResultSet rs;
    public StudentDaoImpl() {
    }
    @Override
    public void create(Student student) throws SQLException, NamingException {
        long id = getID();
        ds = getStudentDS();
        con = ds.getConnection();
        preparedStatement = con.prepareStatement("INSERT INTO STUDENT(ID,STUDENTNAME,EMAIL,AGE,COUNTRY) VALUES(?,?,?,?,?)");
        preparedStatement.setLong(1, id);
        preparedStatement.setString(2, student.getStudentName());
        preparedStatement.setString(3, student.getEmail());
        preparedStatement.setInt(4, student.getAge());
        preparedStatement.setString(5, student.getCountry());
        preparedStatement.executeUpdate();
        con.close();
    }
    @Override
    public List<Student> findAll() throws NamingException, SQLException {
    
        List<Student> list = new ArrayList<>();
        ds = getStudentDS();
        con = ds.getConnection();
        preparedStatement = con.prepareStatement("SELECT * FROM STUDENT");
        rs = preparedStatement.executeQuery();
        while (rs.next()) {
            Student student = new Student();
            student.setId(rs.getLong("ID"));
            student.setStudentName(rs.getString("STUDENTNAME"));
            student.setEmail(rs.getString("EMAIL"));
            student.setAge(rs.getInt("AGE"));
            student.setCountry(rs.getString("COUNTRY"));
            list.add(student);
        }
        con.close();
        return list;
    }
    @Override
    public Student find(long id) throws NamingException, SQLException {
    
        Student student = null;
        ds = getStudentDS();
        con = ds.getConnection();
        preparedStatement = con.prepareStatement("SELECT * FROM STUDENT WHERE ID=?");
        preparedStatement.setLong(1, id);
        rs = preparedStatement.executeQuery();
        if (rs.next()) {
            student = new Student();
            student.setId(rs.getLong("ID"));
            student.setStudentName(rs.getString("STUDENTNAME"));
            student.setEmail(rs.getString("EMAIL"));
            student.setAge(rs.getInt("AGE"));
            student.setCountry(rs.getString("COUNTRY"));
        }
        con.close();
        return student;
    }
    @Override
    public void edit(Student student) throws NamingException, SQLException {
        ds = getStudentDS();
        con = ds.getConnection();
        preparedStatement = con.prepareStatement("UPDATE STUDENT SET STUDENTNAME=?, EMAIL=?, AGE=?, COUNTRY=? WHERE ID=?");
        preparedStatement.setString(1, student.getStudentName());
        preparedStatement.setString(2, student.getEmail());
        preparedStatement.setInt(3, student.getAge());
        preparedStatement.setString(4, student.getCountry());
        preparedStatement.setLong(5, student.getId());
        preparedStatement.executeUpdate();
        con.close();
    }
    @Override
    public void delete(long id) throws NamingException, SQLException {
        ds = getStudentDS();
        con = ds.getConnection();
        preparedStatement = con.prepareStatement("DELETE FROM STUDENT WHERE ID=?");
        preparedStatement.setLong(1, id);
        preparedStatement.executeUpdate();
        con.close();
    }
    private DataSource getStudentDS() throws NamingException {
        Context c = new InitialContext();
        return (DataSource) c.lookup("java:comp/env/StudentDS");
    }
    private long getID() throws NamingException, SQLException {
        long id = 0;
        ds = getStudentDS();
        con = ds.getConnection();
        preparedStatement = con.prepareStatement("SELECT MAX (ID) FROM STUDENT");
        rs = preparedStatement.executeQuery();
        if (rs.next()) {
            id = rs.getLong(1);
        }
        id++;
        con.close();
        return id;
    } }
    

    StudentDao.java

    public interface StudentDao {
     public void create(Student student) throws SQLException, NamingException;
     public List<Student> findAll() throws SQLException, NamingException;
     public Student find(long id) throws SQLException, NamingException;
     public void edit(Student student) throws SQLException, NamingException;
     public void delete(long id) throws SQLException, NamingException;
    }
    

    错误:我无法将数据保存到数据库中   我的朋友告诉(尝试使用try catch at public void create(学生)和getID。不要使用抛出异常)我使用try catch但它不起作用。

0 个答案:

没有答案