servler链接数据库问题求解答!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
//注册处理页面
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");
这个又是单引号又是双引号怎么算的,还是没太明白 数据库 MySQL servlet
[解决办法]
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
}
}