List:MySQL and Java« Previous MessageNext Message »
From:<mmatthew Date:May 4 1999 4:50pm
Subject:Re: Prepared statement problem
View as plain text  
On Tue, 4 May 1999, Robert H. Thompson wrote:

> Hello all, 
> 
> I am a java jdk1.2 (blackdown's port) running on Red Hat Linux 5.2
> (kernel 2.0.36) , mysql 3.22.21 and mm.mysql.jdbc1.2a jdbc driver for
> mysql. I am having a problem executing sql statements. After watching
> some of the posts for the last few days I decided to try using prepared
> statement in the following context:
> 
>     String usrnme = txt_username.getText();
>     try 
>       {
>         System.out.println("loading driver");
>         Class.forName("org.gjt.mm.mysql.Driver");              //loads
> the driver
>         String url     = "jdbc:mysql://localhost:3306/dev_db";
>         Connection con = (Connection) DriverManager.getConnection(url,
> null, null);
> 		                          con.prepareStatement("SELECT f_name, l_name,
> " +
>    				                               "username, password " +
>                                                                "FROM
> valid_users" +
> 				                               "WHERE username = ?");
>         System.out.println("setting up prepared sql statement");
> 		System.out.println("passing "+usrnme+" as input to prepared
> statement");
>         pstmt.setString (1,usrnme);
> 	System.out.println("executing prepared sql statement");
> 	ResultSet rs = (ResultSet) pstmt.executeQuery();
> 	}
> 	catch (Exception ex) 
>         {
>         System.out.println("Exception: " + ex);
> 	System.out.println(" ");
>         }
> 
> When I run the applet that has this code I get the following runtime
> error. java.sql.Exception error during query:
> Syntax or access violation: parse error near '= 'admin'' at line 1.

The quotes are part of the error message. Make sure that you are adding
spaces between your concatenated strings. As your code is now, the
PreparedStatement has the following query:

SELECT f_name, username,password FROM valid_usersWHERE
username = ?

Notice the missing space between valid_users and WHERE. That is your SQL
syntax error.

	-Mark (author of mm.mysql drivers)
--
Mark Matthews <mmatthew@stripped>
http://www.ccm.ecn.purdue.edu/~mmatthew/
"Computers in the future may weigh no more than 2 tons." -Pop.Mech., 1947

Thread
Prepared statement problem Robert H. Thompson4 May
  • Re: Prepared statement problem mmatthew4 May
  • Re: Prepared statement problem Oana Radulescu5 May
    • Re: Prepared statement problemChristian Mack5 May