JDBC Query. Retrieving records based on an input from console.an error called SQL statement not ended properly

时间:2015-10-06 08:28:44

标签: database jdbc

I am trying to get a record from database student based on a query performed on the databased using an sql connection and a input of the record number from the user.

package com.sun.java;

      import java.util.Scanner;
      import java.sql.Connection;
      import java.sql.Driver;
      import java.sql.DriverManager;
      import java.sql.SQLException;
      import java.sql.Statement;

      public class TableFetch {

      public static void main(String[] args) throws SQLException {
        // TODO Auto-generated method stub

        Driver drv = new oracle.jdbc.driver.OracleDriver();

        DriverManager.registerDriver(drv);



        Connection con =  DriverManager.getConnection ("url", "user", "password");

        System.out.println("Got the connection from DB" + con);

        System.out.println("kindly enter the sno of the record to fetch");

        Scanner n = new Scanner(System.in);

        int s9 = n.nextInt();

        String sql = "select * from student" + "WHERE sno =" + s9 + ";";
        Statement statement = con.prepareStatement(sql);
        statement.executeQuery(sql);

        n.close();
        }
        }

1 个答案:

答案 0 :(得分:0)

The following statement got error:

String sql = "select * from student" + "WHERE sno =" + s9 + ";";

Please have space between student and WHERE

P/S: Sorry for unable to comment because I dont have enough points.