<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ page import="javax.sql.*" %>
<%@ page import="java.sql.*" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>Login_info</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="check,login">
<meta http-equiv="description" content="This is for checking your username and password">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
--> </head>
<body>
<%!
Connection conn=null;
Statement stmt=null;
ResultSet rs=null;
String sql=null;
%>
<%
try{
Class.forName("com.mysql.jdbc.Driver").newInstance();
conn=DriverManager.getConnection("jdbc:mysql://localhost/test","root","1219");
stmt=conn.createStatement();
sql = "select userinfo.username AS username," + "userinfo.password AS password" +" FROM userinfo"
+ " where userinfo.userid = 'admin'";
String username = null;
String password = null;
String name =new String(request.getParameter("username").getBytes("ISO-8859-1"),"UTF-8");
String pass = new String(request.getParameter("password").getBytes("ISO-8859-1"),"UTF-8");
rs=stmt.executeQuery(sql);
while(rs.next()){
username = rs.getString("username");
password = rs.getString("password");
}
//if((name.equals(username))&&(pass.equals(password)))
//{ // }
rs.close();
stmt.close();
conn.close();
}catch(Exception e) {
out.println("Error occurred obtaining connection");
}
%>
</body>
</html>
==============================================================
在输入正确的用户名与密码之后,总是显示:Error occurred obtaining connection
而且我用out.println("1");
检测后,发现 Class.forName("com.mysql.jdbc.Driver").newInstance();
这句就没通,但是我已经把mysql-connector-java-5.1.6-bin.jar放在WEB-INF的LIB下了。
<%@ page import="javax.sql.*" %>
<%@ page import="java.sql.*" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>Login_info</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="check,login">
<meta http-equiv="description" content="This is for checking your username and password">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
--> </head>
<body>
<%!
Connection conn=null;
Statement stmt=null;
ResultSet rs=null;
String sql=null;
%>
<%
try{
Class.forName("com.mysql.jdbc.Driver").newInstance();
conn=DriverManager.getConnection("jdbc:mysql://localhost/test","root","1219");
stmt=conn.createStatement();
sql = "select userinfo.username AS username," + "userinfo.password AS password" +" FROM userinfo"
+ " where userinfo.userid = 'admin'";
String username = null;
String password = null;
String name =new String(request.getParameter("username").getBytes("ISO-8859-1"),"UTF-8");
String pass = new String(request.getParameter("password").getBytes("ISO-8859-1"),"UTF-8");
rs=stmt.executeQuery(sql);
while(rs.next()){
username = rs.getString("username");
password = rs.getString("password");
}
//if((name.equals(username))&&(pass.equals(password)))
//{ // }
rs.close();
stmt.close();
conn.close();
}catch(Exception e) {
out.println("Error occurred obtaining connection");
}
%>
</body>
</html>
==============================================================
在输入正确的用户名与密码之后,总是显示:Error occurred obtaining connection
而且我用out.println("1");
检测后,发现 Class.forName("com.mysql.jdbc.Driver").newInstance();
这句就没通,但是我已经把mysql-connector-java-5.1.6-bin.jar放在WEB-INF的LIB下了。
Connection conn=null;
Statement stmt=null;
ResultSet rs=null;
String sql=null;
try{
Class.forName("com.mysql.jdbc.Driver");
conn=DriverManager.getConnection("jdbc:mysql://localhost/test","root","1219");
stmt=conn.createStatement();
String name =new String(request.getParameter("username").getBytes("ISO-8859-1"),"UTF-8");
String pass = new String(request.getParameter("password").getBytes("ISO-8859-1"),"UTF-8");
sql = "select username,password FROM userinfo where username='"+name+"'and password='"+pass+"'";
rs=stmt.executeQuery(sql);
if(rs.next()){
rs.getString("username");
rs.getString("password");
重定向成功页面!!
} else {
重定向失败页面!!
}
rs.close();
stmt.close();
conn.close();
}catch(Exception e) {
out.println("Error occurred obtaining connection");
}
%>
如果这样还有问题,那你的配置或数据库有问题!
执行catch后的代码,说明你的连接没有成功,
conn=DriverManager.getConnection("jdbc:mysql://localhost/test","root","1219");
你的端口号呢? 主要是上面的这句.
conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/test","root","1219");
这句后面的.newInstance()去掉。
conn=DriverManager.getConnection("jdbc:mysql://localhost/test","root","1219");
这句在写url的时候加上端口号。
还有一点是,在写sql语句的时候注意写空格。使用的是“+”号连接字符串,很容易漏掉空格,比如上面的语句中,
from附近就有特别注意,"...password"+"FROM userinfo..."很容易漏掉空格,连接起来就是
"...passwordFROM userinfo..."。
你先试试,看能不能进入数据库,如果不能,就是服务没有启动。 那就得启动服务 然后再试试 应该可以的