为什么用CachedRowSet的acceptchange()方法会提示无法确认表名?
importjava.sql.Connection;importjava.sql.DriverManager;importjava.sql.ResultSet;impor...
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import javax.sql.rowset.CachedRowSet;
import javax.sql.rowset.RowSetFactory;
import javax.sql.rowset.RowSetProvider;
public class CachedRowSetTest {
private static final String DRIVER_CLASS="com.microsoft.sqlserver.jdbc.SQLServerDriver";
private static final String DATABASE_URL="jdbc:sqlserver://localhost:1433;DatabaseName=Book";
private static final String DATABASE_USER="sa";
private static final String DATABASE_PASSWORD="123";
public static void main(String[] args)throws Exception{
try{
String sql="select Book_User.* from Book_User";
Class.forName(DRIVER_CLASS);
Connection conn=DriverManager.getConnection(DATABASE_URL,DATABASE_USER,DATABASE_PASSWORD);
conn.setAutoCommit(false);
Statement stmt=conn.createStatement();
ResultSet rs=stmt.executeQuery(sql);
//使用RowSetProvider创建RowSetFactory
RowSetFactory factory =RowSetProvider.newFactory();
//创建默认的CachedRowSet实例
CachedRowSet cache=factory.createCachedRowSet();
//用rs填充CachedRowSet
cache.populate(rs);
//关闭资源
//rs.close();
//psmt.close();
//conn.close();
cache.afterLast();
while(cache.previous()){
System.out.println(cache.getInt(1)+"\t"+cache.getString(2)+"\t"+cache.getString(3));
if(cache.getInt("User_id")==3){
cache.updateString("User_name", "33");
cache.updateRow();
//把对RowSet所做的修改同步到底层数据库
cache.acceptChanges(conn);
}
}
}
catch(SQLException e){
e.printStackTrace();
}
}
}
报错:
java.sql.SQLException: writeData 无法确定表名
at com.sun.rowset.internal.CachedRowSetWriter.initSQLStatements(CachedRowSetWriter.java:1083)
at com.sun.rowset.internal.CachedRowSetWriter.writeData(CachedRowSetWriter.java:300)
at com.sun.rowset.CachedRowSetImpl.acceptChanges(CachedRowSetImpl.java:880)
at com.sun.rowset.CachedRowSetImpl.acceptChanges(CachedRowSetImpl.java:943)
at CachedRowSetTest.main(CachedRowSetTest.java:49) 展开
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import javax.sql.rowset.CachedRowSet;
import javax.sql.rowset.RowSetFactory;
import javax.sql.rowset.RowSetProvider;
public class CachedRowSetTest {
private static final String DRIVER_CLASS="com.microsoft.sqlserver.jdbc.SQLServerDriver";
private static final String DATABASE_URL="jdbc:sqlserver://localhost:1433;DatabaseName=Book";
private static final String DATABASE_USER="sa";
private static final String DATABASE_PASSWORD="123";
public static void main(String[] args)throws Exception{
try{
String sql="select Book_User.* from Book_User";
Class.forName(DRIVER_CLASS);
Connection conn=DriverManager.getConnection(DATABASE_URL,DATABASE_USER,DATABASE_PASSWORD);
conn.setAutoCommit(false);
Statement stmt=conn.createStatement();
ResultSet rs=stmt.executeQuery(sql);
//使用RowSetProvider创建RowSetFactory
RowSetFactory factory =RowSetProvider.newFactory();
//创建默认的CachedRowSet实例
CachedRowSet cache=factory.createCachedRowSet();
//用rs填充CachedRowSet
cache.populate(rs);
//关闭资源
//rs.close();
//psmt.close();
//conn.close();
cache.afterLast();
while(cache.previous()){
System.out.println(cache.getInt(1)+"\t"+cache.getString(2)+"\t"+cache.getString(3));
if(cache.getInt("User_id")==3){
cache.updateString("User_name", "33");
cache.updateRow();
//把对RowSet所做的修改同步到底层数据库
cache.acceptChanges(conn);
}
}
}
catch(SQLException e){
e.printStackTrace();
}
}
}
报错:
java.sql.SQLException: writeData 无法确定表名
at com.sun.rowset.internal.CachedRowSetWriter.initSQLStatements(CachedRowSetWriter.java:1083)
at com.sun.rowset.internal.CachedRowSetWriter.writeData(CachedRowSetWriter.java:300)
at com.sun.rowset.CachedRowSetImpl.acceptChanges(CachedRowSetImpl.java:880)
at com.sun.rowset.CachedRowSetImpl.acceptChanges(CachedRowSetImpl.java:943)
at CachedRowSetTest.main(CachedRowSetTest.java:49) 展开
2个回答
展开全部
要不把这句String sql="select Book_User.* from Book_User";改成:String sql="select * from Book_User";先试试
追问
还是没用哎 这个应该不影响的 我自己想办法吧 谢啦
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
2017-03-24
展开全部
CachedResultSet crs=.....
crs.setTableName("tableName");
OK
crs.setTableName("tableName");
OK
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询