Results 1 to 6 of 6
  1. #1
    monkeynote is offline Member
    Join Date
    Jan 2012
    Posts
    4
    Rep Power
    0

    Default JDBC named parameters instead of ?, ?, ?

    hello guys!

    I'm new to java and I would like to ask if there is a parameterized way to include / place a name in java? i am using mysql.

    example: in java,

    SELECT * FROM TABLES WHERE field1 = ? AND field2 = ?
    TO
    SELECT * FROM TABLES WHERE field1 = ?field1value AND field2 = ?field2value

    thanks in advance :)

  2. #2
    quad64bit's Avatar
    quad64bit is offline Moderator
    Join Date
    Jul 2009
    Location
    VA
    Posts
    1,323
    Rep Power
    7

    Default Re: JDBC named parameters instead of ?, ?, ?

    Wait what? Your example isn't java, it is SQL. Are you just trying to include a variable in an SQL string?

  3. #3
    monkeynote is offline Member
    Join Date
    Jan 2012
    Posts
    4
    Rep Power
    0

    Default Re: JDBC named parameters instead of ?, ?, ?

    my apologies about my post :)

    here's a sample code using JDBC parameters
    Java Code:
    		// if connection was made, time to execute some statements
    		try{
    			statement = connection.createStatement();
    			//using parameters 
    			[B]String sqlStatement = "SELECT username, fullname FROM users WHERE username = ? AND password = ? ";[/B]
    			PreparedStatement prepStmt = connection.prepareStatement(sqlStatement);
    			prepStmt.setString(1, "username");
    			prepStmt.setString(2, "password");
    			rs = prepStmt.executeQuery();
    			while (rs.next()) {
    				System.out.println(rs.getString("username") + " " + rs.getString("password"));
    			}
    			rs.close();
    			statement.close();
    			connection.close();
    		}
    i want the bold statement to transform into this:

    String sqlStatement = "SELECT username, fullname FROM users WHERE username = ?username AND password = ?password ";

    i hope that you can help me with regards to my inquiry :)

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,173
    Rep Power
    20

    Default Re: JDBC named parameters instead of ?, ?, ?

    What happened when you tried?
    Certain frameworks (both Spring and Hibernate, for example) provide this ability, but straight forward JDBC doesn't (as seen in the setXXX() methods, none of which provide for a parameter name).

  5. #5
    jwalant is offline Member
    Join Date
    Jan 2012
    Posts
    1
    Rep Power
    0

    Default Re: JDBC named parameters instead of ?, ?, ?

    I am not 100% sure what you are asking but I guess you are looking for SQL statement that contains parameter values instead of passing it later.

    If that is the case, use this statement in your case:

    String sqlStatement = "SELECT username, fullname FROM users WHERE username = ' "+username+" ' AND password = ' "+password+" ' ";

    Also, you need not to use preparedStatement. Simple object of Statement class will work.

  6. #6
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default Re: JDBC named parameters instead of ?, ?, ?

    Quote Originally Posted by jwalant View Post
    I am not 100% sure what you are asking but I guess you are looking for SQL statement that contains parameter values instead of passing it later.

    If that is the case, use this statement in your case:

    String sqlStatement = "SELECT username, fullname FROM users WHERE username = ' "+username+" ' AND password = ' "+password+" ' ";

    Also, you need not to use preparedStatement. Simple object of Statement class will work.
    And, this way, you will introduce a wonderful source of SQL syntax errors, and SQL Injection Attack capabilites.

Similar Threads

  1. String named variables
    By sgthale in forum New To Java
    Replies: 3
    Last Post: 05-06-2011, 05:06 AM
  2. Replies: 5
    Last Post: 11-21-2010, 03:08 PM
  3. Named Variables and Exponents
    By kathekas in forum New To Java
    Replies: 5
    Last Post: 09-14-2009, 07:58 PM
  4. MappingException: Named query not known :
    By abhibh_1 in forum Web Frameworks
    Replies: 0
    Last Post: 08-14-2007, 01:59 PM
  5. named pipes
    By samson in forum Networking
    Replies: 1
    Last Post: 04-04-2007, 07:40 AM

Posting Permissions

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