El Classico
El Classico

Reputation: 29

Java General Error On Insert...???

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...

THe INSERT statement is as follows:

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();

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

I get this error:

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();
        }
    }

Upvotes: 0

Views: 9027

Answers (4)

LaGrandMere
LaGrandMere

Reputation: 10359

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);
        }
    }
}

Upvotes: 0

James Greenhalgh
James Greenhalgh

Reputation: 2491

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.

Upvotes: 1

Robert
Robert

Reputation: 42575

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.

Upvotes: 0

BalusC
BalusC

Reputation: 1108537

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.

Upvotes: 3

Related Questions