刚学jsp,写了一个很简单的查询数据库程序如下。运行结果是没有查出来(access数据库里有一条记录)
rs.getRow()返回结果为空,请帮忙看看!<%@page contentType="text/html; charset=gb2312" language="java" import="java.sql.*" errorPage=""%>
<HTML>
<head>
<title>jdbc-odbc test</title>
</head>
<body>
<center>
<font size=5 color=blue>connect access database</font>
</center>
<br>
<hr>
<br>
<center>
<%
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver";
string url="jdbcdbc.ry";
string user="";
string pwd="";
Connection con=DriverManager.getConnection(url,user,pwd);
Statement stmt=con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);
ResultSet rs=stme.executeQuery("select * from ry");
rs.first();
%>
<p>
there are
<%=rs.getRow()%>
records
</p><table>
<tr>
<td><%=rs.getString("pcreator"%></td>
</tr>
</table><%
rs.close();
stmt.close();
con.close();
%></body>
</html>
rs.getRow()返回结果为空,请帮忙看看!<%@page contentType="text/html; charset=gb2312" language="java" import="java.sql.*" errorPage=""%>
<HTML>
<head>
<title>jdbc-odbc test</title>
</head>
<body>
<center>
<font size=5 color=blue>connect access database</font>
</center>
<br>
<hr>
<br>
<center>
<%
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver";
string url="jdbcdbc.ry";
string user="";
string pwd="";
Connection con=DriverManager.getConnection(url,user,pwd);
Statement stmt=con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);
ResultSet rs=stme.executeQuery("select * from ry");
rs.first();
%>
<p>
there are
<%=rs.getRow()%>
records
</p><table>
<tr>
<td><%=rs.getString("pcreator"%></td>
</tr>
</table><%
rs.close();
stmt.close();
con.close();
%></body>
</html>
string url="jdbcdbc.ry"; //odbc数据源url写法有误 jdbc:odbc:xxx
jdbc:odbc.ry 改成jdbc:odbc:ry 还是不对
<%@ page import="java.sql.*"%>
<%@ page contentType="text/html; charset=GBK"%>
<%
String name="";
name="故事会";
System.out.println(name);String strurl="jdbc:odbc:driver={Microsoft Access Driver (*.mdb)};DBQ=D:/Java/Tomcat 4.1/webapps/ROOT/user/books.mdb";
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
System.out.println("before connection");
Connection conn=DriverManager.getConnection(strurl);
Statement stmt=conn.createStatement();
Statement stmt1=conn.createStatement();
%>
<table border="1" width="100%" id="table1">
<%
ResultSet rs=stmt.executeQuery("select * from users");
ResultSet rs1=stmt1.executeQuery("select * from news");
String s="";
String p="";
String id="";
%>
<tr>
<td>用户名</td>
<td>密码</td>
</tr>
<%
while(rs.next())
{
s = rs.getString("name");
p = rs.getString("pwd");
id=rs.getString("id");
%>
<tr>
<td><%=s%> </td>
<td><%=p%> </td>
<td>
<a href="add.jsp?id=<%=id%>&tablename=users">添加</a>
<a href="edit.jsp?id=<%=id%>&tablename=users">修改</a>
<a href="del.jsp?id=<%=id%>&tablename=users">删除</a>
</td>
</tr>
<%
}
while(rs1.next())
{
s = rs1.getString("title");
p = rs1.getString("content");
id=rs1.getString("id");
%>
<tr>
<td><%=s%> </td>
<td><%=p%> </td>
<td>
<a href="add.jsp?id=<%=id%>&tablename=news">添加</a>
<a href="edit.jsp?id=<%=id%>&tablename=news">修改</a>
<a href="del.jsp?id=<%=id%>&tablename=news">删除</a>
</td>
</tr>
<%
}
System.out.println("定义成功!");
String path = getServletContext().getRealPath("/");
System.out.println(path);
conn.commit();
conn.setAutoCommit(true);
conn.close();
%>
</table>
</html>
我也是刚学,给你一段我写的代码,是access数据库,应该好理解
是能查询的,也许对你有其他,我还在研究javabean
stmt拼写错误………………
这句也错了! 代码错误太多!!
我里面是两个表一起读取的,是users表和news表,都有三个字段,字段名也在里面,很简单的
<%@page contentType="text/html; charset=gb2312" language="java" import="java.sql.*" errorPage=""%>
<HTML>
<head>
<title>jdbc-odbc test</title>
</head>
<body>
<center>
<font size=5 color=blue>connect access database</font>
</center>
<br/>
<hr>
<br/>
<center>
<%
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con=DriverManager.getConnection("jdbc:odbc:ry");
Statement stmt=con.createStatement();
ResultSet rs=stmt.executeQuery("select * from ry");
while(rs.next()) {
%>
<table width="640" border="1" align="center" bordercolor="#7188e0">
<th><%= rs.getString(1) %></th>
<th><%= rs.getString(2) %></th>
</table>
<%
}
rs.close();
stmt.close();
con.close();
}
catch(Exception e){
System.out.println("error");
}
%></body>
</html>