2016-05-17 5 views
1

私はムービーテーブルを含むデータベースを持っています。ユーザーから入力を受け取り、データベースに挿入するJavaプログラムを作成しようとしています。私のコードは以下の通りです。なぜ私はエラーが発生しているのか分かりませんので、コード全体が含まれています。Javaを使用してユーザーからデータベースにデータを挿入する際の問題

error: cannot find symbol - stmt.setString(fname);

私は、ユーザからの入力を受けて、それを割り当てられてきたように、私はこれを取得すると思います理由を把握することはできません。たとえば、私は受け付けておりstmt.set...ラインのそれぞれについて

、挿入する前にこの変数に追加します。

ご協力いただければ幸いです。 ありがとう!一見

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

@shmoselすべてのstmt.set ...行では、カッコ内の変数を指し、 "エラー:シンボルを見つけることができません"と表示されます。 – user6276841

答えて

4

、それはおよそsetStringを話しているようなあなたの誤差が見えるというように、それはあなたがそれらのメソッドに渡している変数について、実際のです。

変数id_inttitlefnamelnameなどのすべては、すべて異なるスコープで宣言された:他のtryブロック。

彼らはtryブロックのすべてのために、適用範囲になるように、(ちょうどあなたがconnでやったように)tryブロックの外でこれらの変数のすべてを宣言し、それを修正します。

+0

ヒープありがとう、私は本当にそのことを理解していたはずです。 私は最初にそれらを初期化しなければならないと言っていますが、これは日付を除いては問題ありません。私はどのように私はSQLの日付を任意に初期化するはずです知っていますか? – user6276841

+0

'conn'でやったのと同じように、コンパイラは' null'や '0'を全て割り当ててコンパイラを"トリック "することができます。後で 'PreparedStatement'を使ってブロックを入力したときに' null'でないことを後で確認するようにしてください(IOExceptionやブロックに値を設定するものがあった場合に備えて) )。 – 4castle

関連する問題