2016-05-02 8 views
0

ユーザーがidを入力した場合、driverIDにユーザー入力を使用し、onJobをtrueに変更する必要があります。ユーザは、ドライバIDとして1を入力した場合JDBCでの変数の使用

そこで、例えば、ID番号1のドライバはここで1

にonJob変数の変化が現在自分のコードである必要があります。

public class TaxiDriver { 

    //String driverLocation = DRIVERFIRSTLOCATION; 
    //String destinationintoAPI; 

    //JDBC driver name and database URL 
    static final String JDBC_DRIVER = "com.mysql.jdbc.Driver"; 
    static final String DB_URL = "jdbc:mysql://localhost/DRIVER"; 

    //Database credentials 

    static final String USER = "user"; 
    static final String PASS = "password"; 
    String driverId; 
    static Scanner reader = new Scanner(System.in); 

    public static void main(String[] args) { 
     Connection conn = null; 
     Statement stmt = null; 
     try { 
      //STEP 2: Register JDBC driver 
      Class.forName("com.mysql.jdbc.Driver"); 

      //STEP 3: Open a connection 
      System.out.println("Connecting to a selected database..."); 
      conn = DriverManager.getConnection(DB_URL, USER, PASS); 
      System.out.println("Connected database successfully..."); 

      System.out.println("Assign a driver to a jo..."); 
      reader.nextInt(); 

      //STEP 4: Execute a query 
      System.out.println("Creating statement..."); 
      stmt = conn.createStatement(); 
      String sql = "UPDATE Drivers " + 
       "SET OnJob = 1 WHERE id = (driverId)"; 
      stmt.executeUpdate(sql); 

      // Now you can extract all the records 
      // to see the updated records 
      sql = "SELECT id, license, first, last, OnJob, Email, Telephone, Address, Postcode, Veichle FROM Drivers"; 
      ResultSet rs = stmt.executeQuery(sql); 

      while (rs.next()) { 
       //retrieve by column name 

       int id = rs.getInt("id"); 
       int license = rs.getInt("license"); 
       String first = rs.getString("first"); 
       String last = rs.getString("last"); 
       int OnJob = rs.getInt("OnJob"); 
       String Email = rs.getString("Email"); 
       String Telephone = rs.getString("Telephone"); 
       String Address = rs.getString("Address"); 
       String Postcode = rs.getString("Postcode"); 
       String Veichle = rs.getString("Veichle"); 
       //Display 
       System.out.print("ID: " + id); 
       System.out.print(", license: " + license); 
       System.out.print(", First: " + first); 
       System.out.print(", Last: " + last); 
       System.out.print(", Email; " + Email); 
       System.out.print(", Telephone; " + Telephone); 
       System.out.print(", Address; " + Address); 
       System.out.print(", Postcode; " + Postcode); 
       System.out.print(", Veichle;" + Veichle); 
       if (OnJob == 0) { 
        System.out.println(": Driver is aviliable for pickup"); 
       } else { 
        System.out.println(": Driver is not aviliable for pickup"); 
       } 
      } 
      rs.close(); 
     } catch (SQLException se) { 
      //Handle errors for JDBC 
      se.printStackTrace(); 
     } catch (Exception e) { 
      //Handle errors for Class.forName 
      e.printStackTrace(); 
     } finally { 
      //finally block used to close resources 
      try { 
       if (stmt != null) 
        conn.close(); 
      } catch (SQLException se) { 
      }// do nothing 
      try { 
       if (conn != null) 
        conn.close(); 
      } catch (SQLException se) { 
       se.printStackTrace(); 
      }//end finally try 
     }//end try 
     System.out.println("Goodbye!"); 
    }//end main 
}//end JDBCExample 

現在、これらのエラーが発生しています。

Connecting to a selected database... 
Tue May 03 00:18:28 BST 2016 WARN: Establishing SSL connection without server's identity verification is not recommended. 
According to MySQL 5.5.45+, 5.6.26+ and 5.7.6+ requirements SSL connection must be established by default if explicit option isn't set. 
For compliance with existing applications not using SSL the verifyServerCertificate property is set to 'false'. 
You need either to explicitly disable SSL by setting useSSL=false, or set useSSL=true and provide truststore for server certificate verification. 
Connected database successfully... 
Assign a driver to a jo... 
2 
Creating statement... 
com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Unknown column 'driverId' in 'where clause' 
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) 
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source) 
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source) 
    at java.lang.reflect.Constructor.newInstance(Unknown Source) 
    at com.mysql.jdbc.Util.handleNewInstance(Util.java:404) 
    at com.mysql.jdbc.Util.getInstance(Util.java:387) 
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:939) 
    at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3878) 
    at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3814) 
    at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2478) 
    at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2625) 
    at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2547) 
    at com.mysql.jdbc.StatementImpl.executeUpdateInternal(StatementImpl.java:1541) 
    at com.mysql.jdbc.StatementImpl.executeLargeUpdate(StatementImpl.java:2605) 
    at com.mysql.jdbc.StatementImpl.executeUpdate(StatementImpl.java:1469) 
    at TaxiDriver.main(TaxiDriver.java:71) 
Goodbye! 

事前に感謝します。

答えて

4

まず、実際にはdriverIdの値をどこかに読み込む必要があります。

それ以外の問題は、SQL文にあります。あなたは持っている:

String sql = "UPDATE Drivers " + 
      "SET OnJob = 1 WHERE id = (driverId)"; 

あなたは、レコードid列の値はそのdriverId列の値と等しいレコードを探すためにデータベースを語っています。

driverId変数の値を、実際の文字「driverId」をSQLに入れるのではなく、SQLに変換する必要があります。ここで(見てのとおり)列の名前として解釈されます。あなたがする必要がある、あなたのアプローチを使用して

String sql = "UPDATE drivers SET OnJob=1 WHERE id=" + driverId; 

しかし、動的SQLは危険であり、あなたがPreparedStatementを使用したほうが良いです。これにより、ユーザーの入力が消され、SQLインジェクション攻撃が防止されます。

String sql = "UPDATE drivers SET OnJob=1 WHERE id=?"; 
PreparedStatement ps = connection.createPreparedStatement(sql); 
ps.setInt(1, Integer.parseInt(driverId)); 
ResultSet rs = ps.executeQuery(); 
+0

ありがとう、私は値を読み取ることを忘れていたとは思えません。 –

0

あなたは、ユーザ入力からdriverIdを設定していないようです。 スクリプトを実行する前に、sqlステートメントを有効なid値で更新してください。

すなわち

int driverId = 123;// This is user input, am assuming its type int. 
String sql = "UPDATE Drivers SET OnJob = 1 WHERE id =" + driverId; 
//now execute your sql 

String driverId = "123";//This is user input, am assuming its type String. 
String sql = "UPDATE Drivers SET OnJob = 1 WHERE id =" + "'" + driverId + "'"; 
//now execute your sql 

編集:私は非常にそのはるかに安全と異なったタイプのすべての取り扱いを隠すようPreparedStatementを使用してのQuantumMechanicのソリューションをお勧めします。

+0

これはうまくいきません。 – QuantumMechanic

+0

@QuantumMechanic、指摘に感謝します。あなたが提案したように、そのPreparedStatementを使う方が良い、その確かにもっと安全です。 – MSameer

関連する問題