CodingForums.com

CodingForums.com (http://www.codingforums.com/index.php)
-   Java and JSP (http://www.codingforums.com/forumdisplay.php?f=54)
-   -   SQL Error (http://www.codingforums.com/showthread.php?t=247298)

sam71 12-28-2011 08:04 AM

SQL Error
 
I am new to Java and getting a run time error in sql statement in the java code.

Below is the test code. I want to select the row from CUSTOMER table where CUSTLOGINID = "abc"
Code:

String username = "abc";
String sqlText  = "select CustId, CustLoginId, CustPassword from javaDB.Customer where custLoginId = ";

stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery(sqlText + " '+username+' ");

Please advice what is wrong in above code.

Thanks

Fou-Lu 12-28-2011 01:22 PM

And what is the error you are receiving?

You may as well simplify this as well by making it a prepared statement. Prepared statements have many advantages over standard statements.

Code:

String username = "abc";
String sqlText  = "select CustId, CustLoginId, CustPassword from javaDB.Customer where custLoginId = ?";

PreparedStatement stmt = conn.prepareStatement(sqlText);
stmt.setString(1, username);
ResultSet rs = stmt.executeQuery();



All times are GMT +1. The time now is 11:22 PM.

Powered by vBulletin®
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.