java og sql - insert into
Prøver at indsætte data i min mysql database igennem en java applikation.her er min kode:
Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/bum","root","");
PreparedStatement insertStmt = con.prepareStatement("INSERT INTO users " + "VALUES (1, 'Ham', 'Gutten der', 'email@email.dk')");
ResultSet insertResult = insertStmt.executeQuery();
String id = "1";
PreparedStatement statement = con.prepareStatement("select * from users where id = '"+id+"'");
ResultSet result = statement.executeQuery();
while(result.next()) {
System.out.println(result.getString(1) + " " + result.getString(2) + " " + result.getString(3));
}
Hvilket giver fejl:
Exception in thread "main" java.sql.SQLException: Can not issue data manipulation statements with executeQuery().
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1074)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:988)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:974)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:919)
at com.mysql.jdbc.StatementImpl.checkForDml(StatementImpl.java:497)
at com.mysql.jdbc.PreparedStatement.executeQuery(PreparedStatement.java:2223)
at Main.main(Main.java:23)