求助用户登陆的程序(连接数据库) 我在写一个学生成绩管理系统~但是登陆时始终不能和数据库联上~` 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 /** * 返回唯一实例.如果是第一次调用此方法,则创建实例 * @return DBConnBean 唯一实例 */ static synchronized public DBConnBean getInstance() { if (instance == null) { instance = new DBConnBean(); } clients++; return instance; } /** * 获得一个可用的(空闲的)连接.如果没有可用连接,且已有连接数小于最大连接数 * 限制,则创建并返回新连接 * * @param name 在属性文件中定义的连接池名字 * @return Connection 可用连接或null */ public Connection getConnection(String name) { DBConnectionPool pool = (DBConnectionPool) pools.get(name); if (pool != null) { return pool.getConnection(); } return null; } ObjDBConnBean=getInstance(); Connection Conn=ObjDBConnBean.getConnection("goa"); if (Conn==null) { response.sendRedirect("../index.jsp?MessageType=98"); return; }else{ ResultSet rs = null; Statement stmt = null; try { stmt = Conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE); strSql = "select PASSWORD,DEPTNAME,DEPTID,EMPLOYEE,EMPLOYEENAME,SUPERUSER"; strSql = strSql + " from T_USERMAIN "; rs = stmt.executeQuery(strSql); 登陆页面<html><head><title>主页面</title><meta http-equiv="Content-Type" content="text/html; charset=gb2312"></head><body><br><br><center><img src="images/tree.jpg" width="500" height="300"></center> <form name="myform" method="post" action="chklogin.jsp"> <table width="23%" border="0" align="center" bgcolor="efefef"> <tr> <td><br></td> </tr> <tr> <td width="37%" align="center">用户:</td> <td width="63%" align="center"> <input type="text" name="username" size=14> </td> </tr> <tr> <td width="37%" align="center">密码:</td> <td width="63%" align="center"> <input type="password" name="password" size=14> </td> </tr> <tr> <td colspan=2 align="center"><br><a href="javascript:myform.submit();">登陆</a>      <a href="register.jsp">新用户注册</a> </td> </tr> <tr> <td><br></td> </tr> </table> </form></body></html>登陆验证<html><head><title>登陆验证</title></head><%@page import="java.util.*, java.sql.*" contentType="text/html;charset=GB2312" %><jsp:useBean id="pool" scope="application" class="connection.ConnPool"/><body><% boolean success=false; //是否能够登陆 boolean valid=true; //是否重复登陆 String username=new String(request.getParameter("username").getBytes("ISO8859_1"),"GBK"); String password=new String(request.getParameter("password").getBytes("ISO8859_1"),"GBK"); int rankid=0; //加载数据库驱动并用连接池连结数据库 String sql=""; Connection con = null; try{ if (pool.getDriver()==null){ pool.setDriver("sun.jdbc.odbc.JdbcOdbcDriver"); pool.setURL("jdbc:odbc:MyForum"); pool.setSize(5); pool.initializePool(); } con = pool.getConnection(); Statement statement = con.createStatement(); sql="select * from Users where UserName='"+username+"' and Password='"+password+"'"; ResultSet rs =statement.executeQuery(sql); if (rs.next()){ success=true; rankid=rs.getInt("Rank"); }else{ success=false; } //释放数据库连结 pool.releaseConnection(con); }catch (Exception e){ out.println(e.getMessage()); } //根据登陆结果判断 Vector UserList=(Vector)session.getAttribute("userlist"); if(UserList==null){ UserList=new Vector(); } if (success){ session.setAttribute("username",username); UserList.addElement(username); //判断同一用户名是否重复登陆 for(int i=0;i<UserList.size()-1;i++) { if(username.equals(UserList.elementAt(i).toString())) { UserList.removeElement(username); valid=false; %><h3>用户 <%=username%> 已经登陆,您不要再尝试用此用户名登陆了!</h3> <%} } if(valid) { session.setAttribute("userlist",UserList); session.setAttribute("username",username); response.sendRedirect("listboard.jsp?rankid="+String.valueOf(rankid)); } }else{%> <h3>用户名或密码有误,请重新登录!</h3> <%}%><a href="javascript:history.back();">返回</a></body></html>还需要一个连接bean,可以自己写了 Jxl关于CellView的用法。。。高人进 Java 如何通过TCP传输文件 关于流的问题 菜鸟问题,大家快来帮我看看! 初学者关于数组的问题。 急切求助!!httpclient中文参数的问题!在线等待. 哪儿有支持多列的listview?(内详) 写一个循环语句时碰到了问题,求解 帮我看看这几个英文java的单词是什么意思啊 请教*.shtml如何生成?谢谢! 从邮件服务器接收邮件,怎样同时将其从邮件服务器删除? 求解输出一个字符串的所有组合的问题?
* 返回唯一实例.如果是第一次调用此方法,则创建实例
* @return DBConnBean 唯一实例
*/
static synchronized public DBConnBean getInstance() {
if (instance == null) {
instance = new DBConnBean();
}
clients++;
return instance;
} /**
* 获得一个可用的(空闲的)连接.如果没有可用连接,且已有连接数小于最大连接数
* 限制,则创建并返回新连接
*
* @param name 在属性文件中定义的连接池名字
* @return Connection 可用连接或null
*/
public Connection getConnection(String name) {
DBConnectionPool pool = (DBConnectionPool) pools.get(name);
if (pool != null) {
return pool.getConnection();
}
return null;
}
ObjDBConnBean=getInstance();
Connection Conn=ObjDBConnBean.getConnection("goa");
if (Conn==null) {
response.sendRedirect("../index.jsp?MessageType=98");
return;
}else{
ResultSet rs = null;
Statement stmt = null;
try {
stmt = Conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
strSql = "select PASSWORD,DEPTNAME,DEPTID,EMPLOYEE,EMPLOYEENAME,SUPERUSER";
strSql = strSql + " from T_USERMAIN "; rs = stmt.executeQuery(strSql);
<html>
<head>
<title>主页面</title>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
</head>
<body>
<br><br>
<center><img src="images/tree.jpg" width="500" height="300"></center>
<form name="myform" method="post" action="chklogin.jsp">
<table width="23%" border="0" align="center" bgcolor="efefef">
<tr>
<td><br></td>
</tr>
<tr>
<td width="37%" align="center">用户:</td>
<td width="63%" align="center">
<input type="text" name="username" size=14>
</td>
</tr>
<tr>
<td width="37%" align="center">密码:</td>
<td width="63%" align="center">
<input type="password" name="password" size=14>
</td>
</tr>
<tr>
<td colspan=2 align="center"><br><a href="javascript:myform.submit();">登陆</a>
     <a href="register.jsp">新用户注册</a>
</td>
</tr>
<tr>
<td><br></td>
</tr>
</table>
</form>
</body>
</html>登陆验证<html>
<head>
<title>登陆验证</title>
</head>
<%@page import="java.util.*, java.sql.*" contentType="text/html;charset=GB2312" %>
<jsp:useBean id="pool" scope="application" class="connection.ConnPool"/>
<body>
<%
boolean success=false; //是否能够登陆
boolean valid=true; //是否重复登陆
String username=new String(request.getParameter("username").getBytes("ISO8859_1"),"GBK");
String password=new String(request.getParameter("password").getBytes("ISO8859_1"),"GBK");
int rankid=0;
//加载数据库驱动并用连接池连结数据库
String sql="";
Connection con = null;
try{
if (pool.getDriver()==null){
pool.setDriver("sun.jdbc.odbc.JdbcOdbcDriver");
pool.setURL("jdbc:odbc:MyForum");
pool.setSize(5);
pool.initializePool();
}
con = pool.getConnection();
Statement statement = con.createStatement();
sql="select * from Users where UserName='"+username+"' and Password='"+password+"'";
ResultSet rs =statement.executeQuery(sql);
if (rs.next()){
success=true;
rankid=rs.getInt("Rank");
}else{
success=false;
}
//释放数据库连结
pool.releaseConnection(con);
}catch (Exception e){
out.println(e.getMessage());
}
//根据登陆结果判断
Vector UserList=(Vector)session.getAttribute("userlist");
if(UserList==null){
UserList=new Vector();
}
if (success){
session.setAttribute("username",username);
UserList.addElement(username);
//判断同一用户名是否重复登陆
for(int i=0;i<UserList.size()-1;i++)
{
if(username.equals(UserList.elementAt(i).toString()))
{
UserList.removeElement(username);
valid=false; %><h3>用户 <%=username%> 已经登陆,您不要再尝试用此用户名登陆了!</h3>
<%}
}
if(valid)
{
session.setAttribute("userlist",UserList);
session.setAttribute("username",username);
response.sendRedirect("listboard.jsp?rankid="+String.valueOf(rankid));
}
}else{%>
<h3>用户名或密码有误,请重新登录!</h3>
<%}%>
<a href="javascript:history.back();">返回</a>
</body>
</html>
还需要一个连接bean,可以自己写了