[Microsoft][ODBC Driver Manager] Invalid string or buffer length

小白晒太阳 2014-04-23 11:03:15
连接access数据库,只要表格数据超过2行就报这个异常,实在是无法理解。以前做实验都没出现这个问题,以前是直接在JSP里面写,这次是JSP+JAVABEAN做的,JAVABEAN连接数据库。有没有人跟我有相同情况,求大神给个解决办法。
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ page import="java.sql.*"%>
<jsp:useBean id="sqlutil" class="com.lh.model.ConnBean" scope="page" />
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title></title>
<link href="css/table.css" rel="stylesheet" type="text/css"/>
</head>

<body>
<h3 align="center">新闻内容显示</h3>
<div align="center">
<table id="mytable" cellspacing="0">
<tr>
<th scope="row" >id</th>
<th scope="row" >title</th>
<th scope="row" >newstype</th>
<th scope="row" >author</th>
<th scope="row" >date</th>
</tr>
<%
ResultSet rs = null;
String sql = "select * from news";
int index = 0; //用于表格交替颜色显示
rs = sqlutil.executeQuery(sql);
while (rs.next()) {
%>
<tr>
<td class=<%=index%2==0?"alt":"none" %>><%=rs.getInt("ID")%></td>
<td class=<%=index%2==0?"alt":"none" %>><%=rs.getString("title")%></td>
<td class=<%=index%2==0?"alt":"none" %>><%=rs.getString("type")%></td>
<td class=<%=index%2==0?"alt":"none" %>><%=rs.getString("author")%></td>
<td class=<%=index%2==0?"alt":"none" %>><%=rs.getDate("newsdate")%></td>
</tr>
<%
index++;
}
sqlutil.close();
%>
</table>
</div>
</body>
</html>

package com.lh.model;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;

public class ConnBean {
private static String DRIVERNAME = "sun.jdbc.odbc.JdbcOdbcDriver";
private static String URL = "jdbc:odbc:news";
private static Properties pro = new Properties();
Connection conn = null;
ResultSet rs = null;
Statement stmt;
static {
try {
Class.forName(DRIVERNAME);
pro.setProperty("charSet","GB2312");
} catch (java.lang.ClassNotFoundException e) {
System.out.println("infoBean():" + e.getMessage());
}

}

public ResultSet executeQuery(String sql) { //查询
rs = null;
try {
conn = DriverManager.getConnection(URL,pro);
stmt = conn.createStatement();
rs = stmt.executeQuery(sql);
} catch (SQLException ex) {
System.err.println("aq.executeQuery:" + ex.getMessage());
}
return rs;
}

public void executeUpdate(String sql) { //更新或者添加
stmt = null;
rs = null;
try {
conn = DriverManager.getConnection(URL,pro);
stmt = conn.createStatement();
stmt.executeUpdate(sql);
} catch (SQLException e1) {
System.err.println("executeUpdate:" + e1.getMessage());
}
}

public void close() {
try {
if(rs!=null){
rs.close();
}
} catch (SQLException e) {
e.printStackTrace();
}finally{
try {
if(stmt!=null){
stmt.close();
}
} catch (SQLException e) {
e.printStackTrace();
}finally{
try {
if(conn!=null){
conn.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
}

...全文
237 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
小灯光环 2014-04-23
  • 打赏
  • 举报
回复
可能是驱动问题,去下一个64位的odbc试试: http://www.microsoft.com/en-us/download/details.aspx?id=20065
小白晒太阳 2014-04-23
  • 打赏
  • 举报
回复
无语,搞定了,这么取值就可以。。rs.getString(1)

62,615

社区成员

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

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