关于Java resultset 的问题。。。
我发现一个关于Javaresultset的奇怪问题。。我写的程序如下:/**Tochangethistemplate,chooseTools|Templates*ando...
我发现一个关于Java resultset 的奇怪问题。。
我写的程序如下:
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package enron.graph.similarity;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class Test2 {
private static Connection con;
private static Statement stmt;
private static ResultSet rs;
public static void main(String[] args) throws SQLException, ClassNotFoundException {
String url = "jdbc:odbc:email1";
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
con = DriverManager.getConnection(url);
stmt = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
rs = stmt.executeQuery("select * from user");
if (rs.next()) {
//模块1
/*System.out.println(rs.getString("Id"));
System.out.println(rs.getString("Id")); */
//模块2
/*System.out.println(rs.getString("Id"));
System.out.println(rs.getString("Id"));
System.out.println(rs.getString("Id"));*/
//模块3
/*System.out.println(rs.getString("Id"));
System.out.println(rs.getString("Id"));
System.out.println(rs.getString("Name"));
System.out.println(rs.getString("Id"));*/
//模块4
/*System.out.println(rs.getString("Name"));
System.out.println(rs.getString("Name"));
System.out.println(rs.getString("Name"));*/
}
}
}
我发现,如果 注释 模块2,3,4, 运行模块1,能正确输出结果, 如下:
run:
1
1
BUILD SUCCESSFUL (total time: 1 second)
如果注释 模块1,3,4, 运行模块2,出现如下异常:
run:
1
1
Exception in thread "main" java.sql.SQLException: No data found
at sun.jdbc.odbc.JdbcOdbc.standardError(JdbcOdbc.java:7138)
at sun.jdbc.odbc.JdbcOdbc.SQLGetDataString(JdbcOdbc.java:3907)
at sun.jdbc.odbc.JdbcOdbcResultSet.getDataString(JdbcOdbcResultSet.java:5698)
at sun.jdbc.odbc.JdbcOdbcResultSet.getString(JdbcOdbcResultSet.java:354)
at sun.jdbc.odbc.JdbcOdbcResultSet.getString(JdbcOdbcResultSet.java:411)
at enron.graph.similarity.Test2.main(Test2.java:35)
Java Result: 1
BUILD SUCCESSFUL (total time: 1 second)
如果注释 模块1,2,4, 运行模块3, 能正确输出,如下:
run:
1
1
k..allen
1
BUILD SUCCESSFUL (total time: 1 second)
如果注释 模块1,2,3, 运行模块4, 能正确输出,如下:
run:
k..allen
k..allen
k..allen
BUILD SUCCESSFUL (total time: 1 second)
请问为什么只运行模块2会出错误呢?(图为数据库结构)
请问 dproc2008, 你说 “each column should be read only once”? 那为什么模块四能正确输出?
大家可以自己尝试一下 以上程序。。。 展开
我写的程序如下:
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package enron.graph.similarity;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class Test2 {
private static Connection con;
private static Statement stmt;
private static ResultSet rs;
public static void main(String[] args) throws SQLException, ClassNotFoundException {
String url = "jdbc:odbc:email1";
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
con = DriverManager.getConnection(url);
stmt = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
rs = stmt.executeQuery("select * from user");
if (rs.next()) {
//模块1
/*System.out.println(rs.getString("Id"));
System.out.println(rs.getString("Id")); */
//模块2
/*System.out.println(rs.getString("Id"));
System.out.println(rs.getString("Id"));
System.out.println(rs.getString("Id"));*/
//模块3
/*System.out.println(rs.getString("Id"));
System.out.println(rs.getString("Id"));
System.out.println(rs.getString("Name"));
System.out.println(rs.getString("Id"));*/
//模块4
/*System.out.println(rs.getString("Name"));
System.out.println(rs.getString("Name"));
System.out.println(rs.getString("Name"));*/
}
}
}
我发现,如果 注释 模块2,3,4, 运行模块1,能正确输出结果, 如下:
run:
1
1
BUILD SUCCESSFUL (total time: 1 second)
如果注释 模块1,3,4, 运行模块2,出现如下异常:
run:
1
1
Exception in thread "main" java.sql.SQLException: No data found
at sun.jdbc.odbc.JdbcOdbc.standardError(JdbcOdbc.java:7138)
at sun.jdbc.odbc.JdbcOdbc.SQLGetDataString(JdbcOdbc.java:3907)
at sun.jdbc.odbc.JdbcOdbcResultSet.getDataString(JdbcOdbcResultSet.java:5698)
at sun.jdbc.odbc.JdbcOdbcResultSet.getString(JdbcOdbcResultSet.java:354)
at sun.jdbc.odbc.JdbcOdbcResultSet.getString(JdbcOdbcResultSet.java:411)
at enron.graph.similarity.Test2.main(Test2.java:35)
Java Result: 1
BUILD SUCCESSFUL (total time: 1 second)
如果注释 模块1,2,4, 运行模块3, 能正确输出,如下:
run:
1
1
k..allen
1
BUILD SUCCESSFUL (total time: 1 second)
如果注释 模块1,2,3, 运行模块4, 能正确输出,如下:
run:
k..allen
k..allen
k..allen
BUILD SUCCESSFUL (total time: 1 second)
请问为什么只运行模块2会出错误呢?(图为数据库结构)
请问 dproc2008, 你说 “each column should be read only once”? 那为什么模块四能正确输出?
大家可以自己尝试一下 以上程序。。。 展开
展开全部
stmt = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
我觉得是statement类的问题,建议用下PreparedStatement,这个效率高,稳定性好些,你试试!
我觉得是statement类的问题,建议用下PreparedStatement,这个效率高,稳定性好些,你试试!
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
rs.getString()方法
The ResultSet interface provides getter methods (getBoolean, getLong, and so on) for retrieving column values from the current row. Values can be retrieved using either the index number of the column or the name of the column. In general, using the column index will be more efficient. Columns are numbered from 1. For maximum portability, result set columns within each row should be read in left-to-right order, and each column should be read only once.
The ResultSet interface provides getter methods (getBoolean, getLong, and so on) for retrieving column values from the current row. Values can be retrieved using either the index number of the column or the name of the column. In general, using the column index will be more efficient. Columns are numbered from 1. For maximum portability, result set columns within each row should be read in left-to-right order, and each column should be read only once.
本回答被提问者和网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
好奇怪啊,怎么会出错,get的同一个列。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
2010-07-18
展开全部
搞不明白。关注一下。。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询