Java 在数据库中插入数据

Java 在数据库中插入数据,java,sql,jdbc,Java,Sql,Jdbc,我有以下文件: Student.sql index.jsp sampleport.jsp StudentServlet(com.ydp.controller) StudentDao(com.ydp.dao) StudentDaoImpl(com.ydp.dao.impl) 学生(com.ydp.domains) 我的数据库连接是jdbc:derby://localhost:1527/sample[应用程序对应用程序] StudentDaoImpl.java: public class Stude

我有以下文件:

  • Student.sql
  • index.jsp
  • sampleport.jsp
  • StudentServlet(com.ydp.controller)
  • StudentDao(com.ydp.dao)
  • StudentDaoImpl(com.ydp.dao.impl)
  • 学生(com.ydp.domains)
    我的数据库连接是jdbc:derby://localhost:1527/sample[应用程序对应用程序]
  • 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;
    } }
    
    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;
    }
    
    公共类StudentDaoImpl实现StudentDao{
    私有数据源ds;
    私人连接;
    私人编制的报表;
    私人结果集;
    公立学生{
    }
    @凌驾
    公共void create(Student-Student)抛出SQLException、NamingException{
    long id=getID();
    ds=getStudentDS();
    con=ds.getConnection();
    preparedStatement=con.prepareStatement(“插入学生(ID、学生姓名、电子邮件、年龄、国家)值(?,,,?,?)”;
    编制的报表。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();
    }
    @凌驾
    public List findAll()引发NamingException,SQLException{
    列表=新的ArrayList();
    ds=getStudentDS();
    con=ds.getConnection();
    preparedStatement=con.prepareStatement(“从学生中选择*);
    rs=preparedStatement.executeQuery();
    while(rs.next()){
    学生=新生();
    学生身份证(rs.getLong(“身份证”);
    student.setStudentName(rs.getString(“STUDENTNAME”);
    student.setEmail(rs.getString(“EMAIL”);
    学生设置(rs.getInt(“年龄”);
    student.setCountry(rs.getString(“国家”);
    列表。添加(学生);
    }
    con.close();
    退货清单;
    }
    @凌驾
    公共学生查找(长id)抛出NamingException、SQLException{
    Student=null;
    ds=getStudentDS();
    con=ds.getConnection();
    preparedStatement=con.prepareStatement(“从ID=?”的学生中选择*”;
    编制的报表。setLong(1,id);
    rs=preparedStatement.executeQuery();
    如果(rs.next()){
    学生=新学生();
    学生身份证(rs.getLong(“身份证”);
    student.setStudentName(rs.getString(“STUDENTNAME”);
    student.setEmail(rs.getString(“EMAIL”);
    学生设置(rs.getInt(“年龄”);
    student.setCountry(rs.getString(“国家”);
    }
    con.close();
    留学生;
    }
    @凌驾
    公共无效编辑(学生)引发NamingException、SQLException{
    ds=getStudentDS();
    con=ds.getConnection();
    preparedStatement=con.prepareStatement(“更新学生集学生名=?,电子邮件=?,年龄=?,国家=?其中ID=?”;
    preparedStatement.setString(1,student.getStudentName());
    preparedStatement.setString(2,student.getEmail());
    preparedStatement.setInt(3,student.getAge());
    preparedStatement.setString(4,student.getCountry());
    setLong(5,student.getId());
    preparedStatement.executeUpdate();
    con.close();
    }
    @凌驾
    公共void delete(长id)引发NamingException、SQLException{
    ds=getStudentDS();
    con=ds.getConnection();
    preparedStatement=con.prepareStatement(“从学生中删除,其中ID=?”);
    编制的报表。setLong(1,id);
    preparedStatement.executeUpdate();
    con.close();
    }
    私有数据源getStudentDS()引发NamingException{
    Context c=新的InitialContext();
    返回(数据源)c.lookup(“java:comp/env/StudentDS”);
    }
    private long getID()引发NamingException,SQLException{
    长id=0;
    ds=getStudentDS();
    con=ds.getConnection();
    preparedStatement=con.prepareStatement(“从学生中选择最大ID”);
    rs=preparedStatement.executeQuery();
    如果(rs.next()){
    id=rs.getLong(1);
    }
    id++;
    con.close();
    返回id;
    } }
    
    StudentDao.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;
    } }
    
    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;
    }
    
    公共接口StudentDao{
    公共void create(Student-Student)抛出SQLException、NamingException;
    public List findAll()抛出SQLException,NamingException;
    公共学生查找(长id)抛出SQLException、NamingException;
    公共无效编辑(学生)抛出SQLException、NamingException;
    public void delete(长id)抛出SQLException、NamingException;
    }
    
    错误:我无法将数据保存到数据库中

    我的朋友告诉我(在public void create(Student-Student)和getID中使用try-catch。不要使用抛出异常)我使用try-catch,但它不起作用。

    显示错误日志和连接创建代码在哪里?检查在create方法中的ds和con中得到的值。尝试先检查是否能够连接到数据库。在关闭连接之前是否尝试调用连接#提交?错误“我无法将数据保存到数据库”不是正常的错误消息。请描述您是否遇到异常(如果是,请发布stacktrace),或者更清楚地描述您的问题。顺便说一下,您的DAO实现既不是线程安全的,也不是资源泄漏安全的。尝试寻找一个更合适的DAO教程