package BBS.Ceate;
import java.io.IOException;
import java.sql.*;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class BBSlogin extends HttpServlet
{
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException ,IOException//如果用Post调用get post也可以用
{
doGet(req,resp);
}
protected void doGet(HttpServletRequest req, HttpServletResponse resp)throws ServletException, IOException //传入post和get
{
req.setCharacterEncoding("UTF-8");//转编码表
String UserName=req.getParameter("Name");//获取Jsp中的数据
String UserPwd=req.getParameter("Pwd");
try {
Class.forName("oracle.jdbc.driver.OracleDriver");//加载驱动
String url="jdbc:oracle:thin:@localhost:1521:orcl";//获取连接对象的字符串
String user="BBS1";//获取数据库用户名
String pwd="123";//用户名密码
Connection con =DriverManager.getConnection(url,user,pwd);//建立Connection对象
String sql="select * from userinfo where username= ? and userpwd= ?";//写sql语句
PreparedStatement pstm=con.prepareStatement(sql);//构建参数的命令对象
pstm.setString(1,UserName);//设置里面的值
pstm.setString(2,UserPwd);
ResultSet i=pstm.executeQuery();//ResultSet 对数据库里面的数据取出并成一张Set表不重复 execute...返回单个表单里面的对象
boolean falg=false;
int count=0;
while(i.next())//next 如果i里面有多个返回值 就一直会循环到结束 返回count=循环次数
{
count++;
System.out.println("Name"+i.getString("UserName"));
falg=true;
}
System.out.println(count);
if(falg==false)
{
resp.sendRedirect("sb.jsp");//跳转到Sb页面 reposonse响应请求
System.out.print("登陆失败");
}
else
{
resp.sendRedirect("cg.jsp");
System.out.print("登陆成功");
}
con.close();
pstm.close();
}
catch (SQLException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
catch (ClassNotFoundException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
import java.io.IOException;
import java.sql.*;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class BBSlogin extends HttpServlet
{
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException ,IOException//如果用Post调用get post也可以用
{
doGet(req,resp);
}
protected void doGet(HttpServletRequest req, HttpServletResponse resp)throws ServletException, IOException //传入post和get
{
req.setCharacterEncoding("UTF-8");//转编码表
String UserName=req.getParameter("Name");//获取Jsp中的数据
String UserPwd=req.getParameter("Pwd");
try {
Class.forName("oracle.jdbc.driver.OracleDriver");//加载驱动
String url="jdbc:oracle:thin:@localhost:1521:orcl";//获取连接对象的字符串
String user="BBS1";//获取数据库用户名
String pwd="123";//用户名密码
Connection con =DriverManager.getConnection(url,user,pwd);//建立Connection对象
String sql="select * from userinfo where username= ? and userpwd= ?";//写sql语句
PreparedStatement pstm=con.prepareStatement(sql);//构建参数的命令对象
pstm.setString(1,UserName);//设置里面的值
pstm.setString(2,UserPwd);
ResultSet i=pstm.executeQuery();//ResultSet 对数据库里面的数据取出并成一张Set表不重复 execute...返回单个表单里面的对象
boolean falg=false;
int count=0;
while(i.next())//next 如果i里面有多个返回值 就一直会循环到结束 返回count=循环次数
{
count++;
System.out.println("Name"+i.getString("UserName"));
falg=true;
}
System.out.println(count);
if(falg==false)
{
resp.sendRedirect("sb.jsp");//跳转到Sb页面 reposonse响应请求
System.out.print("登陆失败");
}
else
{
resp.sendRedirect("cg.jsp");
System.out.print("登陆成功");
}
con.close();
pstm.close();
}
catch (SQLException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
catch (ClassNotFoundException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}