SQLite JDBC Update

I am new to jdbc sqlite. I would like to know how to upgrade. For example, I have a table with the name of people and an activity inside. Should I use PreparedStatement?

PreparedStatement change = conn.prepareStatement("Update people set name = ? ");
change.setString(1, "John");

ResultSet rs = stat.executeQuery("select * from people where name = 'Gandhi';");
while (rs.next()) {
    System.out.println("name = " + rs.getString("name"));
    System.out.println("job = " + rs.getString("occupation"));
}
rs.close();
conn.close();

I would like to ask the right way. Thanks you.

+3
source share
2 answers

You are not updating. You need to call change.executeUpdate()to send the UPDATE statement to the engine.

(And as ZeroPage pointed out: delete ;in SQL string)

+2
source

You might want to check Java and SQLite.

Example from the message:

package com.rungeek.sqlite;

import java.sql.Connection;
import java.sql.DriverManager; 
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;

public class Test {
 public static void main(String[] args) throws Exception {
    Class.forName("org.sqlite.JDBC");
    Connection conn = DriverManager.getConnection("jdbc:sqlite:test.db");
    Statement stat = conn.createStatement();
    stat.executeUpdate("drop table if exists people;");
    stat.executeUpdate("create table people (name, occupation);");
    PreparedStatement prep = conn.prepareStatement(
        "insert into people values (?, ?);");

    prep.setString(1, "Gandhi");
    prep.setString(2, "politics");
    prep.addBatch();
    prep.setString(1, "Turing");
    prep.setString(2, "computers");
    prep.addBatch();
    prep.setString(1, "Wittgenstein");
    prep.setString(2, "smartypants");
    prep.addBatch();

    conn.setAutoCommit(false);
    prep.executeBatch();
    conn.setAutoCommit(true);

    ResultSet rs = stat.executeQuery("select * from people;");
    while (rs.next()) {
        System.out.println("name = " + rs.getString("name"));
        System.out.println("job = " + rs.getString("occupation"));
    }
    rs.close();
    conn.close();
}
}

Thank,

+1
source

All Articles