日期:2014-05-18  浏览次数:20731 次

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();
                         &