我的数据库用的是mysql,名字是db_01,账号和密码都是root
1.Register.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ page import="java.io.*,java.util.*,java.sql.*"%>
<%@ page import="javax.servlet.http.*,javax.servlet.*" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
添加用户
<%@include file="title.jsp" %>
注册界面
<%@include file="footer.jsp" %>
2.RegisterCheck.jsp
<%@ page language="java" import="java.util.*,java.sql.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
用户注册页面
<%
String user = new String(request.getParameter("username").getBytes("ISO-8859-1"),"UTF-8");
String pwd = request.getParameter("password");
String driverClass = "com.mysql.jdbc.Driver";
String url = "jdbc:mysql://localhost:3306/db_01";
String username = "root";
String password = "root";
Class.forName(driverClass);//加载驱动
Connection conn = DriverManager.getConnection(url,username,password);//得到连接
PreparedStatement pStmt = conn.prepareStatement("select * from user where usr = '" + user + "'");
ResultSet rs = pStmt.executeQuery();
if(rs.next()){
out.println("");
}else{
PreparedStatement tmt = conn.prepareStatement("Insert into user values('" + user + "','" + pwd + "',null,null,null,null,null,null,null,null,null,null,null)");
int rst = tmt.executeUpdate();
if (rst != 0){
out.println("");
}else{
out.println("");
}
}
%>
查看后续代码请看这里后续代码