1、form表单代码
<form id="logform" action="ManagerServlet" method="post">
<table><tr>
<td>登录</td>
<td>注册</td>
</tr>
<tr>
<td>用户名</td>
<td><input type="text" name="name" />
</td>
</tr>
<tr>
<td>密码</td>
<td><input type="text" name="pwd" />
</td>
</tr>
<tr>
<td><input type="reset" value="清空" ></td>
<td><input type="submit" value="提交" />
</td>
</tr>
</table>
</form>
2、servlet 代码
//接受jsp页面参数
String name=request.getParameter("name");
String pwd=request.getParameter("pwd");
//创建实体类对象
Manager me = new Manager();
//赋值
me.setName(name);
me.setPwd(pwd);
//调用方法
ManagerImpl mi=new ManagerImpl();
//执行实现类中的登录查询,并接收集合
List list=mi.find(name, pwd);
//判断是否有此用户
if(list.size()<=0){
//如果没有查出则插入数据
boolean tf=mi.save(me);
//接收是否插入成功
if(tf==true){
response.sendRedirect("main.html");
}
}else
{
response.sendRedirect("index.jsp");
}
3、实现类中的方法
//加载驱动程序
public static final String Driver="oracle.jdbc.driver.OracleDriver";//驱动
public static final String url="jdbc:oracle:thin:@localhost :1521:ORCL";//url地址
public static final String name="system";//数据库登录名
public static final String pwd="orcl";//数据库登录密码
Connection conn=null;
Statement state=null;
ResultSet rs=null;
//执行添加用户方法
public boolean save(Manager me) {
int a = 0;
try {
Class.forName(Driver);
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
System.out.println("驱动加载错误");
}
try {
conn=DriverManager.getConnection(url,name, pwd);
} catch (SQLException e) {
// TODO Auto-generated catch block
System.out.println("数据库连接出错");
e.printStackTrace();
}
//把实体类中的值付给变量
String name=me.getName();
String pwd=me.getPwd();
String sql="insert into manager values(mid_seq.nextval,'"+name+"','"+pwd+"')";
try {
state=conn.createStatement();
a=state.executeUpdate(sql);
} catch (SQLException e) {
// TODO Auto-generated catch block
System.out.println("执行sql出错");
e.printStackTrace();
}
//最后关闭Connection 、Statement、ResultSet。
finally{
try {
rs.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
state.close();
} catch (SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
try {
conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
//判断是否添加成功
if(a==1){
return true;
}else {
return false;
}
}
//执行查询登录用户名和密码
public List<Manager> find(String nname, String npwd) {
List<Manager> list= new ArrayList<Manager>();
//数据库连接
try {
Class.forName(Driver);
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
System.out.println("驱动加载错误");
}
try {
conn=DriverManager.getConnection(url,name, pwd);
} catch (SQLException e) {
// TODO Auto-generated catch block
System.out.println("数据库连接出错");
e.printStackTrace();
}
//sql语句的执行
String sql =" select id,name,pwd from manager where name='"+nname+"' and pwd='"+npwd+"'";
try {
state=conn.createStatement();
rs=state.executeQuery(sql);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//判断是否查处数据。即是否有此用户
try {
while(rs.next()){
Manager ma=new Manager();
ma.SetId(Integer.valueOf(rs.getString("id")));
ma.setName(rs.getString("name"));
ma.setPwd(rs.getString("pwd"));
list.add(ma);
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//关闭Connection 、Statement、ResultSet
finally{
try {
rs.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
state.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
//返回集合
return list;
}