Results 1 to 6 of 6
  1. #1
    senidaljeet is offline Member
    Join Date
    May 2010
    Posts
    2
    Rep Power
    0

    Unhappy How to accept operator from user in jdbc ?

    Hi There,
    I am a newbie to java, But likes it a lot so always trying to do something new.
    Here is a piece of code which i am trying to run , but all in vain
    Java Code:
    import java.sql.*;
    import java.util.Scanner;
    
    public class SelectRecords{
      public static void main(String[] args) {
        Connection con = null;
    	Scanner sc=new Scanner(System.in);
        try{
          Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
          con = DriverManager.getConnection("jdbc:odbc:Myemp");
         String sql = "SELECT NAME,SALARY from MyEmp where SALARY ? ?";
    	PreparedStatement prest=con.prepareStatement(sql);
    
    	System.out.println("Enter symbol ");
    	String symbol =sc.next();
    	System.out.println("Enter Income ");
    	int IN =sc.nextInt();
    	prest.setString(1,symbol);  
    	prest.setInt(2,IN);
    	System.out.println("Records showing income "+symbol+IN);
    	System.out.println("NAME"+ "\t"+"INCOME");
            ResultSet rs = prest.executeQuery();
            while (rs.next())
    	{
             String name= rs.getString(1);
              int Income = rs.getInt(2);
              System.out.println(name + "\t" +Income);
            }
            prest.close();
            con.close();
          }
          catch (Exception e){
            System.out.println(e.getMessage());
          }
        }
    }
    What i want is that the Less than (<) or greater than(>) symbol should also be entered by the user along with the income amount , in the select statement,
    If i hardcode the (<) or (>) operator the code runs well but it doesn't run if i accept it from keyboard,it gives syntax error
    i would like to know which method should i use for accepting operator , is it a special symbol ??
    Can this be done??
    Thanks.

  2. #2
    Webuser is offline Senior Member
    Join Date
    Dec 2008
    Posts
    526
    Rep Power
    0

    Lightbulb

    Emm try to modify your code like a

    Java Code:
    ...
    String symbol =String.valueOf(sc.next());
    ...
    And find out your console encoding default settings...

    P.S. I dearly hope I understood the question :rolleyes:
    If my answer helped you. Please click my "REP" button and add a comment
    Have a Good Java Coding :)

  3. #3
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,361
    Blog Entries
    7
    Rep Power
    20

    Default

    Prepared statements can't handle that sort of fiddling; they can only inject values, not arbitrary symbols. You have to construct the SQl query yourself (using String fiddling) and execute that using an ordinary Statement. Be careful though because that approach is vulnerable to SQL injection.

    kind regards,

    Jos

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,820
    Rep Power
    19

    Default

    Yep.
    First check that the symbol entered is one of a small set ('>', '<', etc).
    Stick this in your query, and then use that in your prepared statement.
    Java Code:
    String sql = "Select * from something where value " + checkedSymbol + " ?";

  5. #5
    senidaljeet is offline Member
    Join Date
    May 2010
    Posts
    2
    Rep Power
    0

    Default

    Hi Tolls,
    I didn't knew it was so easy...
    Thanks for your reply.
    JosAH,Webuser thanks for your inputs too...

    Just one more thing could some one tell me a free java hosting i did a lot of googling but did not find one.

  6. #6
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,820
    Rep Power
    19

    Default

    They do seem to be rare.
    I have spotted the odd one in the UK, but I can't think who they were now.

Similar Threads

  1. Accept Connect Request and Read Broadcast
    By lockcompetition in forum Networking
    Replies: 2
    Last Post: 03-28-2010, 02:43 AM
  2. How to make a text field to accept numbers only?
    By Robert_85 in forum Advanced Java
    Replies: 15
    Last Post: 04-07-2009, 01:30 PM
  3. variable to accept a single object
    By Rgfirefly24 in forum New To Java
    Replies: 1
    Last Post: 08-06-2007, 04:41 AM
  4. Accept 2 values ....
    By danny000 in forum New To Java
    Replies: 1
    Last Post: 07-31-2007, 02:12 PM
  5. freezing when use socket.accept() inside of a thread
    By tamayo in forum Advanced Java
    Replies: 0
    Last Post: 07-23-2007, 11:48 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •