--sql文件
if exists (select * from sys.objects where name='demodb')
drop database demodb
go
create database demodb
go
use demodb
go
create table t_user
(
name nvarchar(20) ,
[password] nvarchar(20),
email nvarchar(30)
)
go
select * from t_user
// java 代码
package com.student.Bean.util;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
public class Regis {
public Connection conn = null;
public Statement stm = null;
public ResultSet rs = null;
public String check;
public void getConn() {
try {
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
conn = DriverManager.getConnection(
"jdbc:sqlserver://localhost:1433;database=demodb", "sa",
"123");
// 测试数据库是否连接成功
if(conn!=null){
System.out.println("ok!");
}
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
}
public boolean register(String name, String pwd, String email) {
boolean bol = false;
this.getConn();
String sql = "insert into t_user values('"+name+"','"+pwd+"','"+email+"')";
String sqlSelect="select name from t_user where name='"+name+"'";
try {
stm=conn.createStatement();
rs=stm.executeQuery(sqlSelect);
//如果rs有值代表此账号已经存在 则现在需要无法注册
if(rs.next()){
check="亲,此用户名已经注册过了哦,亲你要换一个哦!";
}else{
if(name!=""){
if(pwd!=""){
if(email!=""){
//邮箱不允许重复注册
String sqlSelectEmail="select email from t_user where email='"+email+"'";
stm=conn.createStatement();
rs=stm.executeQuery(sqlSelectEmail);
if(rs.next()){
check="亲,此邮箱账号已经被注册过了哦,亲你再换一个邮箱吧!";
}else{
if(email.contains("@") && (email.endsWith(".cn") || email.endsWith(".com") )){
//PreparedStatement pstm=null;
stm= conn.createStatement();//创建语句对象
stm.execute(sql);
//pstm=conn.prepareStatement(sql);
//pstm.execute();
bol = true;
}else{
check="亲,请填写正确的邮箱格式!";
}
}
}else{
check="亲,请填写邮箱噢!";
}
}else{
check="亲,请填写密码哦!";
}
}else{
check="亲,请填写用户名哦!";
}
}
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
return bol;
}
// public static void main(String[] args){
// Regis r=new Regis();
// r.getConn();
// }
}
//doPost()命令
package com.student.web.util;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import com.student.Bean.util.Regis;
public class RegisServlet extends HttpServlet {
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=gbk");
PrintWriter out = response.getWriter();
String name = request.getParameter("userName");
String pwd = request.getParameter("userPwd");
String email = request.getParameter("userEmail");
Regis re = new Regis();
name=new String(name.getBytes("ISO-8859-1"),"UTF-8");
if (re.register(name,pwd,email)==true) {
HttpSession sess = request.getSession();
sess.setAttribute("regisName", name);
request.getRequestDispatcher("success.jsp").forward(request,
response);
} else {
//out.println("sorry!未能成功添加!");
HttpSession sess = request.getSession();
sess.setAttribute("check", re.check);
request.getRequestDispatcher("error.jsp").forward(request,
response);
}
out.flush();
out.close();
}
}