|
-
Sep 9th, 2004, 01:29 PM
#1
Thread Starter
Fanatic Member
Replacing Apostophes for Insert into Database?
This is really stumping me....I have a form within a JSP, and within the form I ahve a text area for comments...
If the comments have an apostrophe in it ...like That's not right....then it causes an error when I try to insert the data into my Access database...it says that there is a missing operator in the syntax...pointing to the comments...
I have been looking for a simple way to replace the apostrophes within the text area before I send it to the database...
Here is the code I have for the database INSERT:
Code:
<%
String name = request.getParameter("Name");
String phn = request.getParameter("Phone");
String loc = request.getParameter("Location");
String rep = request.getParameter("Rep");
String typ = request.getParameter("Type");
String dte = request.getParameter("DateR");
String acct = request.getParameter("acct");
String prod = request.getParameter("Products");
String cust = request.getParameter("custlname");
String resn = request.getParameter("Reasons");
String comm = request.getParameter("comments");
String parsedReqParam = comm.replace('\'','"');
String today = request.getParameter("today");
ddpe.sqltools.SQLTool q= new ddpe.sqltools.SQLTool("sun.jdbc.odbc.JdbcOdbcDriver","CEPweb",true);
q.doSelectQuery("INSERT INTO CEP VALUES('"+name+"', '"+phn+"', '"+loc+"', '"+rep+"', '"+typ+"', '"+dte+"', '"+acct+"', '"+prod+"', '"+cust+"', '"+resn+"', '"+comm+"', '"+today+"')");
out.println(q.getError());
//out.println(today);
q.close();
%>
I sure hope that some of you JAVA gurus are out there to assist a newbie like me???
Thank you.
-
Sep 10th, 2004, 04:53 AM
#2
Fanatic Member
dunno if this helps
Code:
import java.sql.*;
public class sample{
public static void main(String[] args){
String s="This has an aphostrophe ('apostrophe')";
try{
Class.forName("com.mysql.jdbc.Driver");
Connection cn=DriverManager.getConnection("jdbc:mysql:///temp");
Statement sql=cn.createStatement();
s=s.replaceAll("'","''");
sql.executeUpdate("insert into table1 values('"+s+"')");
}
catch(Exception ex){
ex.printStackTrace();
}
}
}
-
Sep 10th, 2004, 04:54 AM
#3
Fanatic Member
btw, you need to put '' for saving an apostrophe (') on your db.
edit: i mean two apostrophes
Last edited by brown monkey; Sep 10th, 2004 at 05:00 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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|