81,094
社区成员
发帖
与我相关
我的任务
分享
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
Connection conn = null;
PreparedStatement pstmt = null;
// Statement st=null;
ResultSet rs = null;
String getname = request.getParameter("userName");
String getpwd = request.getParameter("userPwd");
try {
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection("jdbc:mysql://localhost/db?"
+ "user=root&password=root");
pstmt = conn
.prepareStatement("SELECT * FROM USERINFO WHERE UNAME=? AND UPWD=?");
pstmt.setString(1, getname);
pstmt.setString(2, getpwd);
// st=conn.createStatement();
rs = pstmt.executeQuery();
// rs=st.executeQuery("SELECT * FROM USERINFO WHERE
// UNAME='"+getname+ "' AND UPWD='"+getpwd+"'");
if (rs.next()) {
HttpSession hs = request.getSession(true);
hs.setAttribute("loginName", getname);
response.sendRedirect("Welcome?uname=" + getname);
} else {
response.sendRedirect("Login");
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
if (rs != null) {
try {
rs.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if (pstmt != null) {
try {
pstmt.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if (conn != null) {
try {
conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}
try {
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection("jdbc:mysql://localhost/db?user=root&password=root&useUnicode=true&characterEncoding=gb2312");
pstmt = conn
.prepareStatement("SELECT * FROM USERINFO WHERE UNAME=? AND UPWD=?");
pstmt.setString(1, "数据库中的中文名");
pstmt.setString(2, "密码");
rs = pstmt.executeQuery();
System.out.println(rs);
if (rs.next()) {
HttpSession hs = request.getSession(true);
hs.setAttribute("loginName", getname);
System.out.println("xixi");
response.sendRedirect("Welcome");
} else {
response.sendRedirect("Login");
}
public class Conn {
public Conn(){}
String driverClass="org.gjt.mm.mysql.Driver";
String url = "jdbc:mysql://localhost/db?user=root&password=root&useUnicode=true&characterEncoding=gb2312";
Connection conn=null; //初始化
Statement stmt=null;
public Connection getConnection()
{
try {
Class.forName(driverClass); //加载驱动
// System.out.println("jiazaichenggong");
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
conn=DriverManager.getConnection(url); //建立连接conn
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return conn;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
Connection con=new Conn().getConnection();
try {
Statement stmt=con.createStatement();
ResultSet rs=stmt.executeQuery("select * from table where name='啊'");
if(rs.next())
System.out.println(rs.getString(1));
else
System.out.println("好像还是不行,我去继续提问");
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
try {
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection("jdbc:mysql://localhost/db?"
+ "user=root&password=root");
pstmt = conn
.prepareStatement("SELECT * FROM USERINFO WHERE UNAME=? AND UPWD=?");
pstmt.setString(1, getname);
pstmt.setString(2, getpwd);
// st=conn.createStatement();
rs = pstmt.executeQuery();
// rs=st.executeQuery("SELECT * FROM USERINFO WHERE
// UNAME='"+getname+ "' AND UPWD='"+getpwd+"'");
System.out.println(getname+getpwd);
System.out.println(rs);
if (rs.next()) {
HttpSession hs = request.getSession(true);
hs.setAttribute("loginName", getname);
System.out.println("xixi");
response.sendRedirect("Welcome");
} else {
response.sendRedirect("Login");
}