简体   繁体   English

如何在 JDBC 中使用 PreparedStatement

[英]How to use PreparedStatement with JDBC

I'm new to JDBC and Java.我是 JDBC 和 Java 的新手。

I'm coming from Javascript and Typescript background.我来自 Javascript 和 Typescript 背景。 I decided to learn JDBC with Oracle by creating a small basic project.我决定通过创建一个小型基础项目来学习 JDBC 和 Oracle。

I'm using JDK 8 .我正在使用JDK 8 I'm following this study material: TutorialsPoint-PreparedStatement .我正在关注这个学习材料: TutorialsPoint-PreparedStatement I figured out that problem is with my DataService .我发现问题出在我的DataService 上

Here's my DataService class:这是我的DataService类:

import java.sql.*;
import java.util.logging.Level;
import java.util.logging.Logger;

public class DataService {
    Connection con;
    PreparedStatement pstmt;

    static String branch_name="";
    static LocalDate branch_created_on;
    static String branch_pulled_from="";

    DataService() {
        try {
            Class.forName("oracle.jdbc.driver.OracleDriver");
            con=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe","system","scott");
        }
        catch(Exception e){ 
            System.out.println(e);
        }
    }

    public void getValue() {
        branch_name=AddNewBranchRecord.branchNameTextField.getText();
        branch_created_on=AddNewBranchRecord.datePicker1.getValue();
        branch_pulled_from=(String) AddNewBranchRecord.combo_box_1.getValue();
    }

    public void putValue() {
        System.out.println("Branch name: "+branch_name);
        System.out.println("Branch created on: "+branch_created_on);
        System.out.println("Branch pulled from: "+branch_pulled_from);
    }

    public void insertRecord() {
        System.out.println("Adding a record...");
        getValue();
        try {
            String sql;
            sql = "insert into mybranches values (branch_name, branch_created_on, branch_pulled_from);";
            pstmt = con.prepareStatement(sql);
        } catch (SQLException ex) {
            Logger.getLogger(DataService.class.getName()).log(Level.SEVERE, null, ex);
        }
        pstmt .close();
    }
}

I'm sure there's something that I missed out.我确定我错过了一些东西。

I'm not getting any error or exception but no row is inserted in the database.我没有收到任何错误或异常,但没有在数据库中插入任何行。

I cross checked with select * from mybranches .我与select * from mybranches交叉检查。

However, the same code works perfectly if I use the normal Statement .但是,如果我使用普通的Statement ,则相同的代码可以完美运行。

You create the PreparedStatement but you don't use it.您创建了PreparedStatement但不使用它。

A prepared statement is there to eg insert different values into a table multiple times.准备好的语句用于例如将不同的值多次插入表中。

You create the PreparedStatement once like you would execute a normal Statement and include ?像执行普通语句一样创建PreparedStatement一次并包含? instead of the values that differ.而不是不同的值。

If you want to execute it, you have to set the values (the ? s will be replaced with them) by using the setXXX(int,Type) methods and then execute it with .execute() .如果你想执行它,你必须使用setXXX(int,Type)方法设置值( ? s 将被它们替换setXXX(int,Type) ,然后使用.execute()执行它。

As pointed out in the comments of the question, the SQL code is not valid.正如问题的评论中所指出的,SQL 代码无效。 The sql code prepared statement is just like the sql code of a regular statement but the values that change all the time are replaced by ? sql代码准备好的语句就像普通语句的sql代码一样,但是一直在变化的值被替换为? . .

The SQL code of the prepared statement would be something like that:准备好的语句的 SQL 代码类似于:

INSERT INTO mybranches VALUES (?,?,?)

If you want to use the PreparedStatement , you could set the values like that:如果要使用PreparedStatement ,可以设置如下值:

pstmt.setString(1,branch_name);
pstmt.setObject(2,branch_created_from);
pstmt.setString(3,branch_pulled_from);

Finally, execute it with最后,执行它

pstmt.execute();

Note that (as I already said) you should create the PreparedStatement once and not every time you execute the insertRecord() method and in that method, you should just call the setXXX methods and the execute() method.请注意(正如我已经说过的)您应该创建PreparedStatement一次,而不是每次执行insertRecord()方法时,在该方法中,您应该只调用setXXX方法和execute()方法。

The PreparedStatement (and the Connection object) should be closed when you don't need it anymore.当您不再需要PreparedStatement (和Connection对象)时,应将其关闭。

Also, as @ TT.另外,作为@TT。 suggests in the comments, you should specify the columns in an INSERT statement.在评论中建议,您应该在INSERT语句中指定列。 It would be something like它会是这样的

INSERT INTO mybranches (name,createdFrom,pulledFrom) VALUES (?,?,?)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM