package user;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import javax.sql.*;
public class uregcl extends HttpServlet{
public void doPost(HttpServletRequest req,HttpServletResponse res){
res.doGet(req,res);
}
public void doGet(HttpServletRequest req,HttpServletResponse res){
Connection ct=null;
Statement st=null;
ResultSet rs=null;
try{
res.setContentType("text/html;charset=gbk");
PrintWriter pw=res.getWriter();
//用户名
String userId=(String)req.getParameterNames("userId");
//姓名
String userName=(String)req.getParameterNames("userName");
//性别
String userSex=(String)req.getParameterNames("userSex");
//年龄
int userAge=(int)req.getParameterNames("userAge");
//问题
String userQuestion=(String)req.getParameterNames("userQuestion");
//答案
String userAnswer=(String)req.getParameterNames("userAnswer");
//密码
String userPassword=(String)req.getParameterNames("userPassword");
//邮箱
String userEmail=(String)req.getParameterNames("userEmail");
//加载驱动
Class.forName("com.mysql.jdbc.Driver");
String str="insert into user(userID,userPassword,userName,userSex,userAge,userEmail,userQuestion,userAnswer) values('"+userID+"','"+userPassword+"','"+userName+"','"+userSex+"','"+userAge+"','"+userEmail+"','"+userQuestion+"','"+userAnswer+"')";
Connection ct=DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306","root","123456");
Statement st=ct.createStatement();
ResultSet rs=st.executeUpdate(str);
}catch(Exception ex){
ex.printStackTrace();
}final{
//不管是否抛出异常都会执行
try{
//分别判断是否为空
//不为空时关闭资源
if(rs!=null){
rs.close();
}
if(st!=null){
st.close();
}
if(ct!=null){
ct.close();
}
}catch(Exception ex){
ex.printStackTrace();
}
}
}
特别是这句:
String str="insert into user(userID,userPassword,userName,userSex,userAge,userEmail,userQuestion,userAnswer) values('"+userID+"','"+userPassword+"','"+userName+"','"+userSex+"','"+userAge+"','"+userEmail+"','"+userQuestion+"','"+userAnswer+"')";
Connection ct=DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306","root","123456");
这个又是单引号又是双引号怎么算的,还是没太明白
17 个解决方案
#1
('"+userID+"','"+userPassword+"','"+userName+"','"+userSex+"','"+userAge+"','"+userEmail+"','"+userQuestion+"','"+userAnswer+"'这句什么时候单引号,生时候双引号
#2
这是报错提示
#3
还有人吗????????????????
#4
'"+userPassword+"'这叫传参数
#5
用
String.format("insert into user(userID,userPassword,userName,userSex,userAge,userEmail,userQuestion,userAnswer) values(%d,'%s','%s','%s',%d,%d,'%s','%s','%s')",userID,userPassword,userName,userSex,userAge,userEmail,userQuestion,userAnswercode]);
#6
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class Urcgcl extends HttpServlet {
/**
* Constructor of the object.
*/
public Urcgcl() {
super();
}
/**
* Destruction of the servlet. <br>
*/
public void destroy() {
super.destroy(); // Just puts "destroy" string in log
// Put your code here
}
/**
* The doGet method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to get.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out
.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
out.println("<HTML>");
out.println(" <HEAD><TITLE>A Servlet</TITLE></HEAD>");
out.println(" <BODY>");
out.print(" This is ");
out.print(this.getClass());
out.println(", using the GET method");
out.println(" </BODY>");
out.println("</HTML>");
out.flush();
out.close();
Connection ct=null;
Statement st=null;
ResultSet rs=null;
try{
//用户名
String userId=(String)request.getParameter("userId");
//姓名
String userName=(String)request.getParameter("userName");
//性别
String userSex=(String)request.getParameter("userSex");
//年龄
String userAge= request.getParameter("userAge");
//问题
String userQuestion=(String)request.getParameter("userQuestion");
//答案
String userAnswer=(String)request.getParameter("userAnswer");
//密码
String userPassword=(String)request.getParameter("userPassword");
//邮箱
String userEmail=(String)request.getParameter("userEmail");
//加载驱动
Class.forName("com.mysql.jdbc.Driver");
String str="insert into user(userID,userPassword,userName,userSex,userAge,userEmail,userQuestion,userAnswer) values('"+userId+"','"+userPassword+"','"+userName+"','"+userSex+"','"+userAge+"','"+userEmail+"','"+userQuestion+"','"+userAnswer+"')";
ct=DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306","root","123456");
st=ct.createStatement();
int i = st.executeUpdate(str);
}catch(Exception ex){
ex.printStackTrace();
}finally{
//不管是否抛出异常都会执行
try{
//分别判断是否为空
//不为空时关闭资源
if(rs!=null){
rs.close();
}
if(st!=null){
st.close();
}
if(ct!=null){
ct.close();
}
}catch(Exception ex){
ex.printStackTrace();
}
}
}
/**
* The doPost method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to post.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
this.doGet(request,response);
}
/**
* Initialization of the servlet. <br>
*
* @throws ServletException if an error occurs
*/
public void init() throws ServletException {
// Put your code here
}
}
一堆的错误。
#7
你还不如用PreparedStatement来进行操作。看着这么多的格式就纠结啦
#8
单引就是数据库存储字符串必须的修饰符,双引表示字符串相加。最后生成出来是这样
('2344','密码','用户名','男',)
('2344','密码','用户名','男',)
#9
我也是才学servlet,不太会操作数据库头疼!
#10
是的,一大推错误,头疼啊!
#11
我看到有些不加引号,像单独的写语句一样也行
就是比如
insert into user('a','b','c') values('s1','s2','s3');这样。
#12
我觉得你先要了解一下sql知识,然后了解一下servlet知识,再了解一下jdbc只是就可以了!
你写的意思就是插入数据,你可能要组装你的sql,所以要传你的参数值!呵呵,没事慢慢来,你可以用preparedstatment,就不会那么纠结!
你写的意思就是插入数据,你可能要组装你的sql,所以要传你的参数值!呵呵,没事慢慢来,你可以用preparedstatment,就不会那么纠结!
#13
其实就是拼字符串
"insert into user(userID,userPassword,userName,userSex,userAge,userEmail,userQuestion,userAnswer) values('"+userID+"','"+userPassword+"','"+userName+"','"+userSex+"','"+userAge+"','"+userEmail+"','"+userQuestion+"','"+userAnswer+"')"
可以把这个字符串用+分开
第一个“”里面就是个普通的字符串 然后+userId userId是一个值 比如是1
结果就是
insert into user(userID,userPassword,userName,userSex,userAge,userEmail,userQuestion,userAnswer) values(' 1
后面的类似
"insert into user(userID,userPassword,userName,userSex,userAge,userEmail,userQuestion,userAnswer) values('"+userID+"','"+userPassword+"','"+userName+"','"+userSex+"','"+userAge+"','"+userEmail+"','"+userQuestion+"','"+userAnswer+"')"
可以把这个字符串用+分开
第一个“”里面就是个普通的字符串 然后+userId userId是一个值 比如是1
结果就是
insert into user(userID,userPassword,userName,userSex,userAge,userEmail,userQuestion,userAnswer) values(' 1
后面的类似
#14
我也感觉preparedstatment可能会好一点,首先sql语句的格式就很清晰,然后是效率问题,预编译的,如果要多次执行这类语句的话,效率也会好一些,然后还可以防止sql注入。
#15
其实你把你拼成的字符串打印出来,然后控制台里面显示的与你在命令行模式下执行的语句一样就行。
就像是单引号 ' 。你在命令行敲的时候肯定要敲上去吧。那么你在拼字符串的时候,就要把那个'(单引号)拼到那个sql中。
#16
解决问题了,原因是很多上下文代码大小写问题还有就是那个引包的问题,我想问个问题就是
package user;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import javax.sql.*;
这是我引的
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;这是6楼大神的,为啥我用.*不能代替呢?
package user;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import javax.sql.*;
这是我引的
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;这是6楼大神的,为啥我用.*不能代替呢?
#17
我错了是import java.sql.*;
我写成javax.sql.*;
了,我是用jcreator写的,那个jar包在那里下载,然后放在那个目录下
我写成javax.sql.*;
了,我是用jcreator写的,那个jar包在那里下载,然后放在那个目录下
#1
('"+userID+"','"+userPassword+"','"+userName+"','"+userSex+"','"+userAge+"','"+userEmail+"','"+userQuestion+"','"+userAnswer+"'这句什么时候单引号,生时候双引号
#2
这是报错提示
#3
还有人吗????????????????
#4
'"+userPassword+"'这叫传参数
#5
用
String.format("insert into user(userID,userPassword,userName,userSex,userAge,userEmail,userQuestion,userAnswer) values(%d,'%s','%s','%s',%d,%d,'%s','%s','%s')",userID,userPassword,userName,userSex,userAge,userEmail,userQuestion,userAnswercode]);
#6
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class Urcgcl extends HttpServlet {
/**
* Constructor of the object.
*/
public Urcgcl() {
super();
}
/**
* Destruction of the servlet. <br>
*/
public void destroy() {
super.destroy(); // Just puts "destroy" string in log
// Put your code here
}
/**
* The doGet method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to get.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out
.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
out.println("<HTML>");
out.println(" <HEAD><TITLE>A Servlet</TITLE></HEAD>");
out.println(" <BODY>");
out.print(" This is ");
out.print(this.getClass());
out.println(", using the GET method");
out.println(" </BODY>");
out.println("</HTML>");
out.flush();
out.close();
Connection ct=null;
Statement st=null;
ResultSet rs=null;
try{
//用户名
String userId=(String)request.getParameter("userId");
//姓名
String userName=(String)request.getParameter("userName");
//性别
String userSex=(String)request.getParameter("userSex");
//年龄
String userAge= request.getParameter("userAge");
//问题
String userQuestion=(String)request.getParameter("userQuestion");
//答案
String userAnswer=(String)request.getParameter("userAnswer");
//密码
String userPassword=(String)request.getParameter("userPassword");
//邮箱
String userEmail=(String)request.getParameter("userEmail");
//加载驱动
Class.forName("com.mysql.jdbc.Driver");
String str="insert into user(userID,userPassword,userName,userSex,userAge,userEmail,userQuestion,userAnswer) values('"+userId+"','"+userPassword+"','"+userName+"','"+userSex+"','"+userAge+"','"+userEmail+"','"+userQuestion+"','"+userAnswer+"')";
ct=DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306","root","123456");
st=ct.createStatement();
int i = st.executeUpdate(str);
}catch(Exception ex){
ex.printStackTrace();
}finally{
//不管是否抛出异常都会执行
try{
//分别判断是否为空
//不为空时关闭资源
if(rs!=null){
rs.close();
}
if(st!=null){
st.close();
}
if(ct!=null){
ct.close();
}
}catch(Exception ex){
ex.printStackTrace();
}
}
}
/**
* The doPost method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to post.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
this.doGet(request,response);
}
/**
* Initialization of the servlet. <br>
*
* @throws ServletException if an error occurs
*/
public void init() throws ServletException {
// Put your code here
}
}
一堆的错误。
#7
你还不如用PreparedStatement来进行操作。看着这么多的格式就纠结啦
#8
单引就是数据库存储字符串必须的修饰符,双引表示字符串相加。最后生成出来是这样
('2344','密码','用户名','男',)
('2344','密码','用户名','男',)
#9
我也是才学servlet,不太会操作数据库头疼!
#10
是的,一大推错误,头疼啊!
#11
我看到有些不加引号,像单独的写语句一样也行
就是比如
insert into user('a','b','c') values('s1','s2','s3');这样。
#12
我觉得你先要了解一下sql知识,然后了解一下servlet知识,再了解一下jdbc只是就可以了!
你写的意思就是插入数据,你可能要组装你的sql,所以要传你的参数值!呵呵,没事慢慢来,你可以用preparedstatment,就不会那么纠结!
你写的意思就是插入数据,你可能要组装你的sql,所以要传你的参数值!呵呵,没事慢慢来,你可以用preparedstatment,就不会那么纠结!
#13
其实就是拼字符串
"insert into user(userID,userPassword,userName,userSex,userAge,userEmail,userQuestion,userAnswer) values('"+userID+"','"+userPassword+"','"+userName+"','"+userSex+"','"+userAge+"','"+userEmail+"','"+userQuestion+"','"+userAnswer+"')"
可以把这个字符串用+分开
第一个“”里面就是个普通的字符串 然后+userId userId是一个值 比如是1
结果就是
insert into user(userID,userPassword,userName,userSex,userAge,userEmail,userQuestion,userAnswer) values(' 1
后面的类似
"insert into user(userID,userPassword,userName,userSex,userAge,userEmail,userQuestion,userAnswer) values('"+userID+"','"+userPassword+"','"+userName+"','"+userSex+"','"+userAge+"','"+userEmail+"','"+userQuestion+"','"+userAnswer+"')"
可以把这个字符串用+分开
第一个“”里面就是个普通的字符串 然后+userId userId是一个值 比如是1
结果就是
insert into user(userID,userPassword,userName,userSex,userAge,userEmail,userQuestion,userAnswer) values(' 1
后面的类似
#14
我也感觉preparedstatment可能会好一点,首先sql语句的格式就很清晰,然后是效率问题,预编译的,如果要多次执行这类语句的话,效率也会好一些,然后还可以防止sql注入。
#15
其实你把你拼成的字符串打印出来,然后控制台里面显示的与你在命令行模式下执行的语句一样就行。
就像是单引号 ' 。你在命令行敲的时候肯定要敲上去吧。那么你在拼字符串的时候,就要把那个'(单引号)拼到那个sql中。
#16
解决问题了,原因是很多上下文代码大小写问题还有就是那个引包的问题,我想问个问题就是
package user;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import javax.sql.*;
这是我引的
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;这是6楼大神的,为啥我用.*不能代替呢?
package user;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import javax.sql.*;
这是我引的
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;这是6楼大神的,为啥我用.*不能代替呢?
#17
我错了是import java.sql.*;
我写成javax.sql.*;
了,我是用jcreator写的,那个jar包在那里下载,然后放在那个目录下
我写成javax.sql.*;
了,我是用jcreator写的,那个jar包在那里下载,然后放在那个目录下