我的dbhelp
package db;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class dbhelp {
String DRIVER="com.microsoft.sqlserver.jdbc.SQLServerDriver";
String url="jdbc:sqlserver://localhost:1433;database=mydb;user=sa;password=123456";
Connection con=null;
public Connection getConnection(){
try{
Class.forName(DRIVER);
con=DriverManager.getConnection(url);
}catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
return con;
}
public int executesql(String sql,Object...args)
{
Connection con=getConnection();
PreparedStatement sta=null;
int rows=0;
try{
sta=con.prepareStatement(sql);
for(int i=0;i<args.length;i++)
{
sta.setObject(i+1, args[i]);
}
rows=sta.executeUpdate();
if(rows>0)
{
System.out.println("operate successfully");}
else{System.out.println("error");
}
}
catch(SQLException e)
{
e.printStackTrace();
}
finally
{
this.close(sta,con);
}
return rows;
}
public void close(ResultSet rs, PreparedStatement sta, Connection con) {
try {
if(rs != null) {
rs.close();
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
try {
if(sta != null) {
sta.close();
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
try {
if(con != null) {
con.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
public void close(PreparedStatement sta,Connection con)
{
this.close(null,sta,con);
}
}
我的stu
package db;
public class stu{
private String sno; //用户名
private String smm;//密码
//getter...setter...
public String getsno() {
return sno;
}
public void setsno(String sno) {
this.sno = sno;
}
public String getsmm() {
return smm;
}
public void setsmm(String smm) {
this.smm = smm;
}
}
我的studao
package db;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class studao {
private static dbhelp db = new dbhelp();
public int insert(stu s) {
String sql = "insert user(sno,smm) values(?,?)";
return db.executesql(sql, s.getsno(),s.getsmm());
}
public static boolean logoin(stu s){
Connection con = db.getConnection();
PreparedStatement sta = null;
ResultSet rs = null;
boolean i = false;
try{
String sql = "select * from user where sno=? and smm=?";
sta = con.prepareStatement(sql);
sta.setString(1, s.getsno());
sta.setString(2, s.getsmm());
rs=sta.executeQuery();
if (rs.next()) {
i = true;
} else {
i = false;
}
}
catch(SQLException e) {
e.printStackTrace();
}finally {
db.close(rs,sta, con);
}
return i;
}
public static stu retrievebysno(String sno) {
Connection con = db.getConnection();
PreparedStatement sta = null;
ResultSet rs = null;
stu s = null;
try {
String sql = "select sno,smm from user where sno=?";
sta = con.prepareStatement(sql);
sta.setString(1, sno);
rs = sta.executeQuery();
if(rs.next()) {
s = new stu();
s.setsno(rs.getString("sno"));
s.setsmm(rs.getString("smm"));
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
db.close(rs,sta, con);
}
return s;
}
}
我的UserServlet
package yzm;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import db.stu;
import db.studao;
public class UserServlet extends HttpServlet {
public void destroy() {
super.destroy(); // Just puts "destroy" string in log
// Put your code here
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.setCharacterEncoding("utf-8");
response.setContentType("text/html");
String sno = new String(request.getParameter("uname").getBytes(
"ISO8859_1"), "GBK");
String smm = new String(request.getParameter("upwd").getBytes(
"ISO8859_1"), "UTF-8");
stu s = new stu();
s.setsno(sno);
s.setsmm(smm);
boolean isLogin;
isLogin = studao.logoin(s);
if (isLogin) {
response.sendRedirect("xuesheng.jsp");
} else {
response.sendRedirect("index.jsp");
}
}
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doPost(request, response);
}
public void init() throws ServletException {
}
}
登录界面(错应该不在这里)
<%@ page language="java" import="java.util.*" pageEncoding="gbk"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<body>
<table><jsp:include page="top.jsp"/></table>
<table style=“width:100%”>
<tr><td style="width: 530px; "><img src="images/login_pic.png" style="width: 540px; height: 209px"></td>
<td>
<form action="servletuser" method="post">
<table align="right" style="width: 421px; height: 255px">
<tr><td>用户名:</td><td><input type="text" name="uname" style="width: 283px; "></td></tr>
<tr><td>密 码:</td><td style="width: 284px; "><input type="password" name="upwd" style="width: 286px; "></td></tr>
<tr><td>验证码:</td><td style="height: 21px; "><input type="text" name="uyzm" style="height: 29px; width: 116px">
<img alt="更换验证码" src="servletyzm" onclick="this.src='servletyzm?a='+Math.random()+100"/>点击更换
<tr><td><input id="RadioButtonList1_1" type="radio" name="RadioButtonList1" value="教师" tabindex="4" /><label for="RadioButtonList1_1">教师</label></td><td><input id="RadioButtonList1_2" type="radio" name="RadioButtonList1" value="学生" checked="checked" tabindex="4" /><label for="RadioButtonList1_2">学生</label></td></tr>
<tr><td align="center"><input type="submit" value="登录" style="background-color: Yellow; width: 124px"></td>
<td align="center"><input type="reset" value="取消" style="width: 86px; background-color: Yellow"></td>
</tr>
</table>
</form>
</td>
</tr>
</table>
</body>
<table align="center"><jsp:include page="bottom.jsp"/></table>
</html>
这个是web.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd">
<web-app id="WebApp_ID">
<display-name>xuesheng</display-name>
<servlet>
<servlet-name>ImageServlet</servlet-name>
<servlet-class>yzm.ImageServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>ImageServlet</servlet-name>
<url-pattern>/servletyzm</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>UserServlet</servlet-name>
<servlet-class>yzm.UserServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>UserServlet</servlet-name>
<url-pattern>/servletuser</url-pattern>
</servlet-mapping>
</web-app>
------解决思路----------------------
莫名觉得你好萌。。