Manoj Rawat Manoj Rawat - 3 years ago 137
Java Question

java.sql.SQLSyntaxErrorException: ORA-00928: missing SELECT keyword

I get this error when i was inserting some rows to database.

Code

public class InsertRowData {
public static void main(String[] args)throws ClassNotFoundException, SQLException{

Connection con = (Connection) DriverManager
.getConnection("jdbc:oracle:thin:@localhost:1521:ORCL" , "system" , "system");

Statement statement = con.createStatement();
String dmlinsert = "insert into student(111,manoj,rawatrulz09)";

int rowseffected = statement.executeUpdate(dmlinsert);
System.out.println("no of rows effected" + rowseffected);

}
}


After searching in google I found the solution to remove single quotes from the column but still getting the same error.
p.s-I am a newbie

Answer Source

This is a bad approach to insert any data into the Table.

String dmlinsert="insert into student(111,manoj,rawatrulz09)"

first of all, your query missing a syntax Values.

when columns are not mentioned while inserting, values must contain same amount of data and respectively.

But when inserting a data the best practice is to mention columns, so that if in later time you make any change to the table in database, your insert query stays stable. for e.g: insert into student(id, name, email) values(111,manoj,rawatrulz09).

Now, if in future you will add a column to this table, your insert query will still work as it has mentioned columns, but if you won't mention any columns then your code will start giving error when executed no of values don't match given to the table etc... Good luck! anyway, answer for your insert query:

insert into student values(111,manoj,rawatrulz09)

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download