插入时的Java常规错误... ??? [英] Java General Error On Insert...???

查看:119
本文介绍了插入时的Java常规错误... ???的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在MS Access中的表上执行插入,更新和删除。对于 SELECT 语句,一切正常

I am trying to do an Insert, Update and Delete on a table in MS Access. Everything works fine

。但是当进行其他三个操作时,我似乎没有得到任何

for a SELECT statement. But when doing the other three operations, I don't seem to get any

错误,但这些操作没有反映到数据库中。请帮忙......

errors, but the actions are not reflected on to the DB. Please help...

INSERT 声明如下:

PreparedStatement ps = con.prepareStatement("INSERT INTO Student VALUES (?, ?, ?, ?, ?, ?, ?, ?)");    
  ps.setInt(1,1);    
  ps.setString(2,"ish");    
  ps.setInt(3,100);    
  ps.setInt(4,100);    
  ps.setInt(5,100);    
  ps.setInt(6,300);
  ps.setInt(7,100);
  ps.setString(8,"A");     
  ps.executeUpdate();

我也知道为什么使用PreparedStatement SELECT语句除外...

Also may I know why PreparedStatement is used except for SELECT statement...

我收到此错误:

Exception in thread "main" java.sql.SQLException: General error
        at sun.jdbc.odbc.JdbcOdbc.createSQLException(JdbcOdbc.java:6986)
        at sun.jdbc.odbc.JdbcOdbc.standardError(JdbcOdbc.java:7114)
        at sun.jdbc.odbc.JdbcOdbc.SQLExecute(JdbcOdbc.java:3149)
        at sun.jdbc.odbc.JdbcOdbcPreparedStatement.execute(JdbcOdbcPreparedState
ment.java:216)
        at sun.jdbc.odbc.JdbcOdbcPreparedStatement.executeUpdate(JdbcOdbcPrepare
dStatement.java:138)
        at Student.main(Student.java:19)

这是我的代码......

This is my code...

    import java.sql.*;
    import java.io.*;

    class Student {
        public static void main(String args[]) throws SQLException, IOException,    ClassNotFoundException {
            Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
            Connection con = DriverManager.getConnection("jdbc:odbc:Student","","");
            Statement st = con.createStatement();
            PreparedStatement ps = con.prepareStatement("INSERT INTO Student VALUES (?, ?, ?, ?, 
            ?, ?, ?, ?)");
            ps.setInt(1,1);
            ps.setString(2,"Girish");
            ps.setInt(3,100);
            ps.setInt(4,100);
            ps.setInt(5,100);
            ps.setInt(6,300);
            ps.setInt(7,100);
            ps.setString(8,"A"); 
            ps.executeUpdate();
            con.commit();
            con.close();
        }
    }


推荐答案

这当您没有时,可能会发生这种情况提交 / 关闭连接。确保在执行语句后提交连接并关闭 try <的 finally 块中的连接(以及语句和结果集) / code>阻止它们被获取并执行的地方。

This can happen when you don't commit/close the connection. Ensure that you're committing the connection after executing the statement and are closing the connection (and statement and resultset) in the finally block of the try block where they are been acquired and executed.

至于为什么使用 PreparedStatement ,这是避免 SQL注入的常用方法攻击并轻松设置值得满足的Java对象,如日期 InputStream SQL查询中的等,无需将它们转换为 String

As to why the PreparedStatement is used, it's the common approach to avoid SQL injection attacks and to ease setting fullworthy Java objects like Date, InputStream, etc in a SQL query without the need to convert them to String.

这篇关于插入时的Java常规错误... ???的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆