使用Java将用户数据插入数据库时出现问题

使用Java将用户数据插入数据库时出现问题,java,sql,database,bufferedreader,Java,Sql,Database,Bufferedreader,我有一个包含电影表的数据库,我正试图编写一个java程序来接收用户的输入并将其插入数据库。我的代码如下。我已经包含了整个代码,因为我不知道为什么会出现错误 对于我正在接收的stmt.set…行,例如: 错误:找不到符号-stmt.setString(fname) 我不明白为什么我会得到这个,因为我已经收到了来自用户的输入,并在插入之前将其分配给这个变量 任何帮助都将不胜感激。 谢谢 乍一看,您的错误看起来像是在谈论setString等等,但实际上是关于传递给这些方法的变量 所有变量id\u in

我有一个包含电影表的数据库,我正试图编写一个java程序来接收用户的输入并将其插入数据库。我的代码如下。我已经包含了整个代码,因为我不知道为什么会出现错误

对于我正在接收的
stmt.set…
行,例如:

错误:找不到符号-stmt.setString(fname)

我不明白为什么我会得到这个,因为我已经收到了来自用户的输入,并在插入之前将其分配给这个变量

任何帮助都将不胜感激。 谢谢


乍一看,您的错误看起来像是在谈论
setString
等等,但实际上是关于传递给这些方法的变量

所有变量
id\u int
title
fname
lname
等都在不同的范围内声明:另一个
try


要修复它,请在
try
块之外声明所有这些变量(就像您在
conn
中所做的那样),以便它们将在所有
try
块的范围内。

乍一看,您的错误看起来像是在谈论
setString
等等,但实际上是关于传递给这些方法的变量

所有变量
id\u int
title
fname
lname
等都在不同的范围内声明:另一个
try


要修复它,请在
try
块之外声明所有这些变量(就像您使用
conn
时所做的那样),以便它们将在所有
try
块的范围内。

@shmosel用于每个stmt.set。。。它指向括号内的变量并给出“错误:找不到符号”。@shmosel代表每个stmt.set。。。它指向括号内的变量,并给出“错误:找不到符号”。多谢了,我真的应该意识到,哈哈。这是说我必须先草签,除了日期外,这没关系。你知道我应该如何初始化sql日期吗?与你对
conn
所做的类似,你可以通过将
null
0
分配给所有编译器来“欺骗”编译器。只要确保稍后使用
PreparedStatement
输入块时,检查它们是否仍然为
null
(以防块中出现
IOException
或设置这些变量值的内容)。多亏了heaps,我真的应该意识到这一点,哈哈。这是说我必须先草签,除了日期外,这没关系。你知道我应该如何初始化sql日期吗?与你对
conn
所做的类似,你可以通过将
null
0
分配给所有编译器来“欺骗”编译器。只要确保稍后使用
PreparedStatement
输入块时,检查它们是否仍然为
null
(以防块中出现
IOException
或设置这些变量值的内容)。
import java.sql.*;
import java.util.*;
import java.io.*;

public class insert {
    public static void main(String [] argv) throws Exception {
        Connection conn = null;
        try
        {
            Class.forName("org.postgresql.Driver");
            String url = "jdbc:postgresql://localhost//////";
            conn = DriverManager.getConnection(url, "/////", "///");
        }
        catch (ClassNotFoundException e)
        {
            e.printStackTrace();
            System.exit(1);
        }
        catch (SQLException e)
        {
            e.printStackTrace();
            System.exit(2);
        }
        BufferedReader bufRead = new BufferedReader(new InputStreamReader(System.in));
        try {
            System.out.println("Please enter the id for the new movie: ");
            String id = bufRead.readLine();
            int id_int = Integer.parseInt(id);
            System.out.println("Please enter the title for the new movie: ");
            String title = bufRead.readLine();
            System.out.println("Please enter the director's first name: ");
            String fname = bufRead.readLine();
            System.out.println("Please enter the director's last name: ");
            String lname = bufRead.readLine();
            System.out.println("Please enter the genre of the movie: ");
            String genre = bufRead.readLine();
            System.out.println("Please enter the media type: ");
            String media = bufRead.readLine();
            System.out.println("Please enter the movie's release date: ");
            String date = bufRead.readLine();
            java.sql.Date release_d = java.sql.Date.valueOf(date);
            System.out.println("Please enter the movie's studio: ");
            String studio = bufRead.readLine();
            System.out.println("Please enter the retail price of the movie: ");
            String price = bufRead.readLine();
            double price_d = Double.parseDouble(price);
            System.out.println("Please enter the number of copies in stock: ");
            String stock = bufRead.readLine();
            int stock_int = Integer.parseInt(stock);

        } catch (IOException err) {
            System.out.println(err);
        } catch (NumberFormatException err) {
            System.out.println(err);
        }

        System.out.println("\nWelcome to MovieDirect");
        System.out.println();

        try {
            PreparedStatement stmt = conn.prepareStatement("INSERT INTO movie " + "(movie_id, movie_title, director_first_name, director_last_name, genre, media_type, release_date, studio_name, retail_price, current_stock)" + "VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)");

            stmt.setInt(1, id_int);
            stmt.setString(2, title);
            stmt.setString(3, fname);
            stmt.setString(4, lname);
            stmt.setString(5, genre);
            stmt.setString(6, media);
            stmt.setDate(7, release_d);
            stmt.setString(8, studio);
            stmt.setDouble(9, price_d);
            stmt.setString(10, stock_int);

            stmt.executeUpdate();

        } catch (SQLException e) {
            System.out.println(e);
            conn.close();
            System.exit(1);
        }
        System.out.println("\nSuccess! ");
        conn.close();
    }
}