java.sql.SQLException:ORA-00911:无效字符

时间:2016-03-24 18:56:41

标签: java sql oracle web-services jsp

下面的代码显示了java.sql.SQLException。任何人都可以帮我修复这个

Class.forName("oracle.jdbc.driver.OracleDriver");

String lname=request.getParameter("lastname");
String uname=request.getParameter("username");
String fname=request.getParameter("firstname");
String cemail=request.getParameter("email");
String pswd=request.getParameter("password");
String cpswd=request.getParameter("conformpassword");
String sex=request.getParameter("gender");
long phon=Long.parseLong(request.getParameter("mobile"));

Connection con = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:XE", "system", "admin");
Statement st = con.createStatement();
ResultSet rs = st.executeQuery("insert into users values('" + uname + "','" + fname + "','"  + lname + "','" + cpswd + "',"+ phon + ",'" + cemail + "','" + sex + "');");

1 个答案:

答案 0 :(得分:0)

而不是:

Statement st = con.createStatement();
ResultSet rs = st.executeQuery("insert into users values('" + uname + "','" + fname + "','"  + lname + "','" + cpswd + "',"+ phon + ",'" + cemail + "','" + sex + "')");

我建议做以下事情:

PreparedStatement st = con.prepareeStatement("insert into users values(?,?,?,?,?,?,?);");
st.setString(1, uname);
st.setString(2, fname); 
st.setString(3, lname); 
st.setString(4, cpswd); 
st.setLong(5, phon); 
st.setString(6, cemail); 
st.setString(7, sex);
ResultSet rs = st.executeQuery();

您的问题并不明显,但我使用PreparedStatement可以防止您将遇到的未来问题。我希望有所帮助。