Java SQLite database, insert entry giving nullPointerException

user3932611

I am trying to create a generic method that can add an entry into a SQLite database, using Eclipse and Java.

When the table name is hardcoded it works fine, but when I try to pass in the table name as a string it is giving me a nullPointerException.

below is the method that creates that table:

public static void Table()
      {
        Connection c = null;
        Statement stmt = null;
        try {
          Class.forName("org.sqlite.JDBC");
          c = DriverManager.getConnection("jdbc:sqlite:test.db");
          System.out.println("Opened database successfully");

          stmt = c.createStatement();
          String sql = "CREATE TABLE IF NOT EXISTS COMPANY " +
                       "(ID INT PRIMARY KEY     NOT NULL," +
                       " NAME           TEXT    NOT NULL, " + 
                       " AGE            INT     NOT NULL, " + 
                       " ADDRESS        TEXT, " + 
                       " SALARY         REAL)"; 
          stmt.executeUpdate(sql);
          stmt.close();
          c.close();
        } catch ( Exception e ) {
          System.err.println( e.getClass().getName() + ": " + e.getMessage() );
          System.exit(0);
        }
        System.out.println("Table created successfully");
      }

and here is the method that inserts an entry into the created table. I want to pass in the table name through the method rather than hard coding it:

 public static void Insert(String table, int id, String name, int age, String address, String salary)
      {
        Connection c = null;
        PreparedStatement pstmt = null;
        try {
          Class.forName("org.sqlite.JDBC");
          c = DriverManager.getConnection("jdbc:sqlite:test.db");
          c.setAutoCommit(false);
          System.out.println("Opened database successfully");

          String query="INSERT INTO "+table+" (ID,NAME,AGE,ADDRESS,SALARY) VALUES (?,?,?,?,?)";

          PreparedStatement stmt = c.prepareStatement(query);

            pstmt.setInt(1,id);
            pstmt.setString(2,name);
            pstmt.setInt(3, age);
            pstmt.setString(4, address);
            pstmt.setString(5, salary);

            pstmt.executeUpdate();

            pstmt.close();
          c.commit();
          c.close();
        } catch ( Exception e ) {
          System.err.println( e.getClass().getName() + ": " + e.getMessage() );
          System.exit(0);
        }
        System.out.println("Records created successfully");
      }
Chandan Rajput

You have small mistake just you have created two different PreparedStatement object So change

PreparedStatement stmt = c.prepareStatement(query);

to

pstmt = c.prepareStatement(query);

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

Insert String into JDBC SQLite database

分類Dev

How to insert image into sqlite3 database?

分類Dev

Insert list into single sqlite database column

分類Dev

Can't INSERT into Sqlite3 database

分類Dev

How to insert values into database in java

分類Dev

I am calling userDao.findById(Integer i) and is giving java.lang.NullPointerException i am giving the exact id that is in db

分類Dev

how do i search for an entry in mongodb database using java in netbeans?

分類Dev

Login through sqlite giving error

分類Dev

Database entry error

分類Dev

Unable to insert record into SQLite Database from Firebase Message Service when app is in background or closed state

分類Dev

How to parse a nested json data file iand insert it into sqlite database using python3

分類Dev

sqliteを使用したログインでのjava.lang.NullPointerException

分類Dev

SQLite INSERT OR REPLACE with condition

分類Dev

SQLite trigger before insert

分類Dev

MySQL INSERT INTO statement giving ERROR CODE 1062

分類Dev

multiple entry of products in the database - PHP

分類Dev

AES encrypted database entry size

分類Dev

Allow for null datetime entry into database?

分類Dev

Re-entry of Data in database

分類Dev

Sqlite database with null objects in the database

分類Dev

select list from database is giving null

分類Dev

Insert POINT into postgres database

分類Dev

PHP MySQL Insert database

分類Dev

Insert image into MySQL database

分類Dev

Mysql Insert date in a database

分類Dev

Export a writable sqlite database

分類Dev

Update values sqlite database

分類Dev

SQLite database partitioning

分類Dev

Java FXML:Nullpointerexception

Related 関連記事

ホットタグ

アーカイブ