我的JSP连接数据库的代码?请帮我看看。

joerico 2003-03-04 04:18:26
<body>
<%! String data = "jdbc:odbc:Presidents"; %>
<%
try{
//加载驱动程序
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection conn = DriverManager.getConnection(data,"","");
Statement stmt = conn.createStatement();
stmt.execute("SELECT Name FROM Contacts");
ResultSet rc = stmt.getResultSet();
}
%>
<table width="100%" border="1">
<th>本轮推荐:</th>
<% while(rc.next()) { %>
<tr>
<td><%= rc.getString("Name") != null ? ds.getString("Name") :" " %></td>
</tr>
<% } %>
</table>
<% stmt.close();
}catch(Exception e){
System.out.println("Error:" + e.toString() + e.getMessage());
}
%>
</body>

大侠,代码有错吗?
...全文
29 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
joerico 2003-03-05
  • 打赏
  • 举报
回复
hch9230(走路的人)
页头的代码我都写了。

flashroom(找到啦)
ds是写错的。实际是rc。呵呵~~不好意思,笔误。可是问题还是一样,没出来。
hch9230 2003-03-05
  • 打赏
  • 举报
回复
在KevinChu(朱朱)的代码的基础上,你有没有把包import了?
在页面头要有:
<%@ page contentType="text/html;charset=gb2312" %>
<%@ page language="java" import="java.sql.*"%>
flashroom 2003-03-05
  • 打赏
  • 举报
回复
<td><%= rc.getString("Name") != null ? ds.getString("Name") :" " %></td>


ds是什么啊??
joerico 2003-03-04
  • 打赏
  • 举报
回复
KevinChu(朱朱)
我用了你的连接代码。不行,我还是在网页上得不到输出。

<table width="100%" border="1">
<tr>
<td>FIPS</td>
<td>Country</td>
<td>Year</td>
</tr>
<%
while(rs.next()){



%>
<tr>
<td><%= rs.getString(1) %></td>
<td><%= rs.getString(2) %></td>
<td><%= rs.getString(3) %></td>
</tr>
<% }
%>
</table>
KevinChu 2003-03-04
  • 打赏
  • 举报
回复
<%
//变量声明
Connection conn=null;//数据库连接对象
Statement stmt=null; //SQL语句对象
String strCon=null; //数据库连接字符串
//连接数据库
String url="jdbc:odbc:Presidents";
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
conn=DriverManager.getConnection(url);
//创建一个可以滚动的只读的SQL语句对象
//stmt=conn.createStatement();
stmt=conn.createStatement(java.sql.ResultSet.TYPE_SCROLL_INSENSITIVE,java.sql.ResultSet.CONCUR_READ_ONLY); //准备SQL语句
sql = "select * from news where type like 'e'";
//执行SQL语句并获取结果集
ResultSet rs = stmt.executeQuery(sql);

%>

81,092

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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