老师布置的作业,用jsp连接mysql数据库。我准备了3个文件,index。jsp用于从表单中获取数据(用户名和密码),JDBConnection.java是一个javabean。check。jsp用于检验用户输入的数据(先获取传递过来的用户名和密码,然后检索数据库,做比较,进行简单的判断)。操蛋的j2ee老师,你让我们这些没学java的人情何以堪啊。。
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
代码:
index。jsp<%@ 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>
<head>
<base href="<%=basePath%>">
<title>My JSP 'index.jsp' starting page</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="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page"> </head>
<body>
<form method="get" action="check.jsp">
<table>
<tr><td>用户名:<input type="text" name="username" value=""></td></tr>
<tr><td>密码:<input type="password" name="password" value=""></td></tr>
<tr><td><input type="submit" value="提交" ></td>
<td valign="top"><br></td><td valign="top"><br></td><td valign="top"><br></td><td valign="top"><br></td><td><input type="reset" value="取消" ></td></tr>
</table>
</form>
</body>
</html>
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
JDBConnection.java(在包yours下)的源码:
package yours;import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;public class JDBConnection{ private final String dbDriver="com.mysql.jdbc.Driver";
private final String url="jdbc:mysql://localhost:3306/char1_2";
private final String userName="root";
private final String password="1991016";
private Connection con;
public JDBConnection()
{
try{
Class.forName(dbDriver).newInstance();
}catch (Exception e)
{
System.out.println("未能成功加载驱动!");
}
}
public boolean createConnection()
{
try{
con=DriverManager.getConnection(url,userName,password);
con.setAutoCommit(true);
}catch(SQLException e)
{
return false;
}
return true;
}
public ResultSet executeQuery(String sql)
{
ResultSet rs;
try
{
if(con==null)
createConnection();
Statement stmt =con.createStatement();
rs=stmt.executeQuery(sql);
}catch(Exception e)
{
return null;
}
return rs;
}
public void closeConnection()
{
if(con!=null)
{
try{
con.close();
}catch(SQLException e)
{
}
}
}
}+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++<%@ 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>
<head>
<base href="<%=basePath%>">
<title>My JSP 'check.jsp' starting page</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="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
--> </head>
<body>
<%@ page import="java.sql.*" %>
<jsp:useBean id="mysql" scope="page" class="yours.JDBConnection"/>
<%
String username=new String(request.getParameter("username"));//接收传递过来的用户名
String password=new String(request.getParameter("password"));//接收传递过来的密码
String sql="select * from table1";
ResultSet rs=null;
mysql.createConnection();
rs=mysql.executeQuery(sql);
String usr1=null;//获取数据库中的用户名
String passwd1=null;//获取用户名中的密码
boolean flag=false;
while(rs.next()){
usr1=rs.getString("username");
passwd1=rs.getString("password");
if(usr1==username&&passwd1==password)
{
out.println("欢迎你,用户:"+usr1.toString());
flag=true;
break;//找到记录就跳出循环//
}
}
if(flag)
out.println("没找到你的用户名,"+username.toString()+"没有注册!");
rs.close();
mysql.closeConnection();
%>
</body>
</html>但是始终无法得到结果,他们说驱动有问题,想请大虾帮忙解惑
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
代码:
index。jsp<%@ 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>
<head>
<base href="<%=basePath%>">
<title>My JSP 'index.jsp' starting page</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="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page"> </head>
<body>
<form method="get" action="check.jsp">
<table>
<tr><td>用户名:<input type="text" name="username" value=""></td></tr>
<tr><td>密码:<input type="password" name="password" value=""></td></tr>
<tr><td><input type="submit" value="提交" ></td>
<td valign="top"><br></td><td valign="top"><br></td><td valign="top"><br></td><td valign="top"><br></td><td><input type="reset" value="取消" ></td></tr>
</table>
</form>
</body>
</html>
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
JDBConnection.java(在包yours下)的源码:
package yours;import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;public class JDBConnection{ private final String dbDriver="com.mysql.jdbc.Driver";
private final String url="jdbc:mysql://localhost:3306/char1_2";
private final String userName="root";
private final String password="1991016";
private Connection con;
public JDBConnection()
{
try{
Class.forName(dbDriver).newInstance();
}catch (Exception e)
{
System.out.println("未能成功加载驱动!");
}
}
public boolean createConnection()
{
try{
con=DriverManager.getConnection(url,userName,password);
con.setAutoCommit(true);
}catch(SQLException e)
{
return false;
}
return true;
}
public ResultSet executeQuery(String sql)
{
ResultSet rs;
try
{
if(con==null)
createConnection();
Statement stmt =con.createStatement();
rs=stmt.executeQuery(sql);
}catch(Exception e)
{
return null;
}
return rs;
}
public void closeConnection()
{
if(con!=null)
{
try{
con.close();
}catch(SQLException e)
{
}
}
}
}+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++<%@ 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>
<head>
<base href="<%=basePath%>">
<title>My JSP 'check.jsp' starting page</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="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
--> </head>
<body>
<%@ page import="java.sql.*" %>
<jsp:useBean id="mysql" scope="page" class="yours.JDBConnection"/>
<%
String username=new String(request.getParameter("username"));//接收传递过来的用户名
String password=new String(request.getParameter("password"));//接收传递过来的密码
String sql="select * from table1";
ResultSet rs=null;
mysql.createConnection();
rs=mysql.executeQuery(sql);
String usr1=null;//获取数据库中的用户名
String passwd1=null;//获取用户名中的密码
boolean flag=false;
while(rs.next()){
usr1=rs.getString("username");
passwd1=rs.getString("password");
if(usr1==username&&passwd1==password)
{
out.println("欢迎你,用户:"+usr1.toString());
flag=true;
break;//找到记录就跳出循环//
}
}
if(flag)
out.println("没找到你的用户名,"+username.toString()+"没有注册!");
rs.close();
mysql.closeConnection();
%>
</body>
</html>但是始终无法得到结果,他们说驱动有问题,想请大虾帮忙解惑
解决方案 »
- 送分啦!AXIS1.4异常诡异,跪求分析。
- 不知道这样可行不可行!
- 大家用很通俗的话来描述j2ee
- 本地访问tomcat 开始还挺快,停一会就不能刷了。
- struts中文乱吗问题提问!
- 在jsp中如何存取oracle中的long类型字段(在线等)
- 用JFreeChart画的统计图在linux下汉字显示的问题
- 急问:如何给控件的value属性赋变量值??(50分全给)
- 希望高手推荐比较好的Java中class反编译工具,谢谢!!
- post模拟登录的问题,有302跳转
- 我想学习网站开发,已经有点相关基础知识了,哪位高手能不能推荐几本较好的书籍?
- 通过绝对路径下载js/css/gif如果有../怎么处理
mysql> show tables;
Empty set (0.00 sec)
这是mySQL数据库现实的东西,这是因为你现在数据库里没有数据,你现在这个cheak.jsp页面中的
if(usr1==username&&passwd1==password)
这一句话只是静态的实现判断用户名密码是否符合你所规定的,并没有添加大牌数据库里;
楼主可以写一个增删改查页面,这个应该不是问题的;楼主参考一下
org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:541)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:435)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:320)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:266)
javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
root cause java.lang.NullPointerException
org.apache.jsp.check_jsp._jspService(check_jsp.java:105)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:393)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:320)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:266)
javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
note The full stack trace of the root cause is available in the Apache Tomcat/6.0.13 logs.
--------------------------------------------------------------------------------