Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Accept characters like '

Status
Not open for further replies.

RicardoPereira

Programmer
Jun 3, 2003
255
PT
i have a bean with a method that accept the client parameter. The problem is when i search a client with that character ' in the middle, like o'conner. How can i use this characters?

code:
public ArrayList getClients(String Client) throws SQLException {
ArrayList res=new ArrayList();
Connection con = getConnection();
PreparedStatement pstmt=
con.prepareStatement("exec Clients '" + Client + "'");

ResultSet rslt=pstmt.executeQuery();
while (rslt.next()) {
res.add(new TblClients (rslt.getString(1),rslt.getString(2),rslt.getString(3)) );
}
rslt.close();
pstmt.close();
con.close();
return res;
}
 
Here is an example of how to do this :
Code:
PreparedStatement pstmt = con.prepareStatement("select * from bla where client = '?'");
pstmt.setString(1, Client);
ResultSet rslt = pstmt.executeQuery();

--------------------------------------------------
Free Database Connection Pooling Software
 
In that case why are you using PreparedStatement ? You should be using CallableStatement.

--------------------------------------------------
Free Database Connection Pooling Software
 
use double '

for each string that you use in query search for ' and put one more ' .
for example
Code:
String query = "select ... from palyers where col_name = 'o[b]''[/b]conner'"
 
fuadhamidov :

This really is not a good way of doing it because it is prone to SQL injection attacks.

Depending on how JDBC compliant your driver is, the syntax should be :
Code:
CallableStatement cstmt = con.prepareCall("{ Clients(?) }");
cstmt.setString(1, Client);
ResultSet rslt = cstmt.executeQuery();
[code]


--------------------------------------------------
Free Database Connection Pooling Software
[URL unfurl="true"]http://www.primrose.org.uk[/URL]
 
What is the main difference between CallableStatement and PreparedStatement ?
 
sedj:

With your i got the error
Microsoft][SQLServer 2000 Driver for JDBC]Invalid parameter binding(s).
 
Have you tried :

Code:
CallableStatement cstmt = con.prepareCall("{ call Clients[?]}");

or 

CallableStatement cstmt = con.prepareCall("{ exec Clients[?]}");

or 

CallableStatement cstmt = con.prepareCall("{ call Clients(?)}");

or 

CallableStatement cstmt = con.prepareCall("{ exec Clients(?)}");

Did you read the docs on it (the link I posted earlier) ? What about your SQLServer documentation on the driver ?

--------------------------------------------------
Free Database Connection Pooling Software
 
yes but i got everytime the same error:
Microsoft][SQLServer 2000 Driver for JDBC]Invalid parameter binding(s).
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top