JSP中mysQL数据库读取数据问题

ReverseTime 2013-02-04 12:36:50




<%@ page language="java" import="java.util.*" pageEncoding="GB2312"%>
<%@ 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>My JSP 'h6mysql.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>
<%!
public static final String DBDRIVER="org.gjt.mm.mysql.Driver";
public static final String DBURL="jdbc:mysql://localhost:3306/mldn";
public static final String DBUSER="root";
public static final String DBPASS="root";
%>
<%
Connection conn=null;
PreparedStatement pstmt=null;
ResultSet rs=null;
%>
<%
try{
Class.forName(DBDRIVER);
conn=DriverManager.getConnection(DBUSER,DBUSER,DBPASS);
String sql="SELECT empno,ename,job,sal,hiredate FROM emp";
pstmt=conn.prepareStatement(sql);
rs=pstmt.executeQuery();
%>
<center>
<table border="1" width="80%">
<tr>
<td>雇员编号</td>
<td>雇员姓名</td>
<td>雇员工作</td>
<td>雇员工资</td>
<td>雇佣日期</td>
</tr>
<%
while(rs.next()){
int empno=rs.getInt(1);
String ename=rs.getString(2);
String job=rs.getString(3);
float sal=rs.getFloat(4);
java.util.Date date=rs.getDate(5);
%>
<tr>
<td><%=empno %></td>
<td><%=ename %></td>
<td><%=job %></td>
<td><%=sal %></td>
<td><%=date %></td>
</tr>
<%
}
%>
</table>
</center>
<%
}catch(Exception e){
System.out.println(e);
}finally{
rs.close();
pstmt.close();
conn.close();
}
%>
</body>
</html>




我找了一个读取mysQL数据库数据的例子(代码上) 编译没有错误 打开tomcat后想在网页中打开,就会弹出“错误1”,再看网页就是正在加载……,数据库建立如“错误2” 请问高人这是怎么回事啊??应该怎样修改呢??麻烦了!!!希望高手不吝赐教!!!



...全文
871 3 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
dong_y888 2013-02-06
  • 打赏
  • 举报
回复
        Class.forName(DBDRIVER).newInstance();
    	conn=DriverManager.getConnection(DBURL,DBUSER,DBPASS);
    	String sql="SELECT empno,ename,job,sal,hiredate FROM emp";
        Statement stmt=con.createStatement();
    	rs=stmt.executeQuery(sql);
dong_y888 2013-02-04
  • 打赏
  • 举报
回复
改为下面的
conn=DriverManager.getConnection(DBURL,DBUSER,DBPASS);
ReverseTime 2013-02-04
  • 打赏
  • 举报
回复
引用 1 楼 dong_y888 的回复:
改为下面的
Java code?1conn=DriverManager.getConnection(DBURL,DBUSER,DBPASS);



可是还是不行啊。。。

22,302

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 疑难问题
社区管理员
  • 疑难问题社区
  • 尘觉
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧