<%@ page contentType="text/html; charset=gb2312" language="java" import="java.sql.*,java.util.*,java.io.*" errorPage="" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>无标题文档</title>
</head>
<body>  
  <%
  String driverName="com.mysql.jdbc.Driver";
  String userName="root";
  String userPasswd="root";
  String dbName="register";
  String tableName="userinformation";
  String url = "jdbc:mysql://localhost:3306/register?user=root&password=root";
  Class.forName("com.mysql.jdbc.Driver").newInstance();
   Statement stmt=conn.createStatement();
  String username=request.getParameter("username"); 
  String password=request.getParameter("password");
   String password1=request.getParameter("password1");
  if(username.equals("")||password.equals("")||password1.equals("")||password!=password1)
  { out.print("输入有误,请重新注册!");
  response.sendRedirect("zhuce.html");}
  else{
    String sql="insert into txt (username,password) values ('"+username+"','"+password+"')";
       stmt.executeUpdate(sql);
       out.print("用户添加成功!!");
        response.sendRedirect("loginmain.html");
   }
  stmt.close();
  conn.close();
  %>
</body>
</html>
错误是conn cannot be resolved,求高手指导得怎么改?

解决方案 »

  1.   

    你的conn没有产生你就调用了。driverManage.getConnnection();
      

  2.   


    这句要改一下:Class.forName("com.mysql.jdbc.Driver").newInstance();
    改为:Class.forName(driverName) ;
    Connection conn = null ;
    conn = DriverManager.getConnection(url,userName,userPasswd) ;
    还有:
    这两句不要:String dbName="register";
    String tableName="userinformation";
    这两句还要改为下面的:String driverName="org.gjt.mm.mysql.Driver";
    String url = "jdbc:mysql://localhost:3306/register";
      

  3.   

    Class.forName("com.mysql.jdbc.Driver");这是利用Java的反射机制来加载驱动,获得连接需要用conn=DriverManager.getConnection(url,userName,password);
      

  4.   

    还是有问题啊,总是会出错
    org.apache.jasper.JasperException: An exception occurred processing JSP page /register.jsp at line 1613:   String url = "jdbc:mysql://localhost:3306/register";
    14:   Class.forName(driverName) ;
    15:   Connection conn = null ;
    16:   conn=DriverManager.getConnection(url,userName,userPasswd);
    17:   Statement stmt=conn.createStatement();
    18:   String username=request.getParameter("username");  
    19:   String password=request.getParameter("password");