私は、カラムが id
,teacher_id
およびname
のコーステーブルを用意しています。PreparedStatementで間違った出力を取得しています
これはIDでコースを取得するために使用しているメソッドです。
私はこの方法を実行すると、私は出力IDを取得public static Course getById(int id) throws SQLException {
String query = "SELECT * FROM courses WHERE id = ?" ;
Course course = new Course();
Connection connection = null;
PreparedStatement statement = null;
ResultSet resultSet = null;
try{
DriverManager.registerDriver(new com.mysql.jdbc.Driver());
connection = (Connection) DriverManager.getConnection(ConnectDb.CONN_STRING, ConnectDb.USERNAME, ConnectDb.PASSWORD);
statement = (PreparedStatement) connection.prepareStatement(query, ResultSet.TYPE_SCROLL_INSENSITIVE,
ResultSet.CONCUR_READ_ONLY);
statement.setInt(1, id);
resultSet = statement.executeQuery(query);
while (resultSet.next()) {
course.setId(resultSet.getInt("id"));
course.setName(resultSet.getString("name"));
course.setTeacherId(resultSet.getInt("teacher_id"));
}
}catch (SQLException e) {
System.err.println(e);
}finally{
if (resultSet != null) resultSet.close();;
if (statement != null) statement.close();
if(connection != null) connection.close();
}
return course;
}// end of method
:0、teacher_id:0
サーバー・ログには、私はバグがここにあるのSQLException
com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '?' at line 1
コードをデバッグしましたか?結果がまったくない場合は、戻り値が返されます...代わりにその時点で例外をスローする必要がありますか? –
または、行が一致していない。私はドーナツにあなたにドルを賭けるでしょう。「新しいコース」は、あなたにそれらのフィールドのゼロを持つコースを与えます。ステップ1は**常に**デバッグ:[*小さなプログラムをデバッグする*](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/) –
データベース内のその「id」のデータそして、あなたが見ている結果は、int型のデフォルト値です。 – Sachin