给你一个我刚学jsp的时候写的哈,关于连接数据库的都在jsp页面上
login.jsp
----------------------------------------------
------------------
chklogin.jsp
--------------------------------------------
<%
String url="jdbc:mysql://localhost:3306/d?useUnicode=true&chatacterEncoding=utf8";
String username="root";
String password="123456";
String name=request.getParameter("uname");
String pw=request.getParameter("pw");
Connection con=null;
PreparedStatement sql=null;
ResultSet rs=null;
try{
Class.forName("org.gjt.mm.mysql.Driver");
con=DriverManager.getConnection(url,username,password);
sql=con.prepareStatement("select * from user where username=? and password=?");
sql.setString(1,name);
sql.setString(2,pw);
rs=sql.executeQuery();
if(rs!=null){
if(rs.next()==true)
{%>
<%
session.setAttribute("uname",name);
Vector visitors=(Vector)application.getAttribute("namelist");
if(visitors==null)
{
visitors=new Vector();
application.setAttribute("namelist",visitors);
}
boolean flag=true;
for(int i=0;i {
if(((String)visitors.get(i)).equals(name))
{
flag=false;
break;
}
if(flag){visitors.add(name);}
}
out.print("恭喜登录成功,3秒后将自动跳转到主页面");
%>
<% response.setHeader("refresh","3;url=zhuye.jsp");
}
else{%>
alert("用户名或密码错误");
<%}
}
}
catch(Exception e)
{
e.printStackTrace();
out.print("网站遇到问题,请稍后再试");
}
finally{
try{
if(rs!=null) {rs.close();}
}
catch(Exception e)
{}
try{
if(sql!=null) {sql.close();}
}
catch(Exception e)
{}
try{
if(con!=null) {con.close();}
}
catch(Exception e)
{}
}
%>
你先看看,哪里不懂再说,不知道 你是准备用啥技术做,我这个是最原始的,嘿嘿 .......