Java General Error On Insert…???

前端 未结 4 1024
误落风尘
误落风尘 2020-12-22 06:29

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 o

相关标签:
4条回答
  • 2020-12-22 07:13

    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.

    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.

    0 讨论(0)
  • 2020-12-22 07:22

    The main reason for using a PreparedStatement is security. Generating a SQL query by concating strings is unsafe as the variable parts may contain SQL statements entered by a user. This would allow to execute statements like DROP TABLE * to the user (see SQL Injection). Theres is is a good idea only to use PreparedStatemnts if the SQL query is not static (doe snot contain variable parts). Therefore it would be better also to use PreparedStatement for SELECT statements.

    0 讨论(0)
  • 2020-12-22 07:25

    Edit :

    You try to Insert your Student Primary Key, if it's an Identity column, it will not work.

    You need to prepare your statement like this :

    PreparedStatement ps = con.prepareStatement("INSERT INTO Student(Field1,Field2,Field3,Field4,Field5,Field6,Field7) VALUES (?, ?, ?, ?, ?, ?, ?)");
    

    Without your Primary Key set, the DB will do it for you.

    .

    .

    .

    Original post :

    There is a kind of similar question on StackOverflow.

    You won't see any result from INSERT queries with Access until you close your Connection properly.

    Your code doesn't close any resources, which will surely bring you grief. Call the close methods (in reverse order if there are more than one) in a finally block.

    Here is a class DataBaseUtils to help you if needed.

    public class DatabaseUtils
    {
        public static Connection createConnection(String driver, String url, String username, String password) 
            throws ClassNotFoundException, SQLException
        {
            Class.forName(driver);
    
            return DriverManager.getConnection(url, username, password);
        }
    
        public static void close(Connection connection)
        {
            try
            {
                if (connection != null)
                {
                    connection.close();
                }
            }
            catch (SQLException e)
            {
                e.printStackTrace(e);
            }
        }
    
        public static void close(Statement statement)
        {
            try
            {
                if (statement != null)
                {
                    statement.close();
                }
            }
            catch (SQLException e)
            {
                e.printStackTrace(e);
            }
        }
    
        public static void close(ResultSet rs)
        {
            try
            {
                if (rs != null)
                {
                    rs.close();
                }
            }
            catch (SQLException e)
            {
                e.printStackTrace(e);
            }
        }
    }
    
    0 讨论(0)
  • 2020-12-22 07:31

    I believe your prepared statement is of the wrong format. The documentation for INSERT INTO (available here: http://msdn.microsoft.com/en-us/library/bb208861(v=office.12).aspx) gives this format:

    Single-record append query:

    INSERT INTO target [(field1[, field2[, …]])]     VALUES (value1[, value2[, …])
    

    You give the format:

    INSERT INTO target VALUES (value1[, value2[, …])
    

    edit: To be more clear I believe you want something like:

    PreparedStatement ps = con.prepareStatement("INSERT INTO Student (Year, Name, field3 ...) VALUES (?, ?, ?, ?, ?, ?, ?, ?)");
    

    Where Year, Name, field3 ... are the names of the fields you are trying to insert into.

    0 讨论(0)
提交回复
热议问题