-4

I need to insert value which is stored in "name" variable into the database. Till Now, i have tried below code ,but its not working.

String name="Aswini";

String query="INSERT INTO STUDENT"+"VALUES (1239,'name', 'IT')";
statement.executeUpdate(quer);

Can any one help me in this,thanks in advance.

Siddhartha Gupta
  • 507
  • 3
  • 11
teja reddy
  • 157
  • 1
  • 1
  • 4

3 Answers3

0

Modify your string and then try

String query="INSERT INTO STUDENT"+"VALUES (1239,'"+name+"', 'IT')";
AJ.
  • 4,526
  • 5
  • 29
  • 41
  • While this code snippet may solve the question, [including an explanation](http://meta.stackexchange.com/questions/114762/explaining-entirely-code-based-answers) really helps to improve the quality of your post. Remember that you are answering the question for readers in the future, and those people might not know the reasons for your code suggestion. – Bono Mar 16 '15 at 12:58
0

This is not a good way of use db query. You should go with prepared statements.

Still you can correct this as follows.

String query="INSERT INTO STUDENT"+"VALUES (1239,'"+name+"', 'IT')";

But best way is

    String name="Aswini";
    Connection conn = null;
    String query="INSERT INTO STUDENT VALUES (1239,?, 'IT')";
    PreparedStatement preparedStatement = null;
    String messageType = "trading";
    try {
        conn = // initialize connection
    } catch (SQLException e) {
        e.printStackTrace();
    }
    preparedStatement = conn.prepareStatement(query);
    preparedStatement.setString(1,name);
Ruchira Gayan Ranaweera
  • 34,993
  • 17
  • 75
  • 115
  • Hai Ruchira i used same way as said ,but still its gettion below error. Exception in thread "main" java.lang.NullPointerExceptionat sampleExa .main (sampleExa. java:62) – teja reddy Mar 16 '15 at 13:04
  • @tejareddy make sure your `conn` is not `null` here. – Ruchira Gayan Ranaweera Mar 17 '15 at 03:42
  • try { // conn = // initialize connection Class.forName("oracle.jdbc.driver.OracleDriver"); Connection connection = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe","usename","pswrd"); i have given like this,please help me still i'm not able to get it.Thanks in advance. – teja reddy Mar 17 '15 at 04:55
  • @user:1869846:String query="INSERT INTO STUDENT"+"VALUES (?,?,?)"; Class.forName("oracle.jdbc.driver.OracleDriver"); Connection connection = DriverManager .getConnection ("jdbc:oracle :thin:@localhost:1521:xe","Tejswini","Tejswini"); PreparedStatement updateemp =connection.prepareStatement(query); updateemp.setInt(1,23); updateemp.setString(2,"Roshan"); updateemp.setString(3, "CEO"); updateemp.executeUpdate();please help me i tried dis way but still getting error,java.sql.SQLSyntaxErrorException: ORA-00928: missing SELECT keyword – teja reddy Mar 17 '15 at 06:29
  • @tejareddy please follow these instructions.. http://www.mkyong.com/jdbc/jdbc-statement-example-insert-a-record/ . If you are still facing an issue contact me via skype. you can find my contact info in my profile. – Ruchira Gayan Ranaweera Mar 17 '15 at 07:42
0

You can insert using Table like that

String query="INSERT INTO STUDENT"+"VALUES (1239,'"+name+"', 'IT')";
Benjamin
  • 2,257
  • 1
  • 15
  • 24