在线等!!!java连接数据库,流已关闭问题
java.sql.SQLException:流已被关闭atoracle.jdbc.dbaccess.DBError.throwSqlException(DBError.j...
java.sql.SQLException: 流已被关闭
at oracle.jdbc.dbaccess.DBError.throwSqlException(DBError.java:134)
at oracle.jdbc.dbaccess.DBError.throwSqlException(DBError.java:179)
at oracle.jdbc.dbaccess.DBError.throwSqlException(DBError.java:269)
at oracle.jdbc.ttc7.LongTTCItem.getChars(LongTTCItem.java:189)
at oracle.jdbc.dbaccess.DBDataSetImpl.getCharsStreamItem(DBDataSetImpl.java:1630)
at oracle.jdbc.driver.OracleStatement.getCharsInternal(OracleStatement.java:3347)
at oracle.jdbc.driver.OracleStatement.getStringValue(OracleStatement.java:3556)
at oracle.jdbc.driver.OracleResultSetImpl.getString(OracleResultSetImpl.java:434)
at oracle.jdbc.driver.OracleResultSet.getString(OracleResultSet.java:1482)
at com.xianyifa.bbs.Theme.ThemeInfo.ThemeInfo(ThemeInfo.java:35)
at org.apache.jsp.bbs_002dxyf.show_002dposts_002dfloor.show_005fposts_005ffloor_jsp._jspService(show_005fposts_005ffloor_jsp.java:74)
at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:374)
at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:342)
at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:267)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:128)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:286)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:845)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:583)
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:447)
at java.lang.Thread.run(Thread.java:619)
2009-3-4 15:14:55 org.apache.catalina.startup.HostConfig deployDescriptor
总是抛这个异常我的代码:
Theme T = new Theme();
T.setEmittime(rs.getTimestamp("emit_time").toString());
T.setEmitusername(rs.getString("emit_user_name"));
T.setThemematter(rs.getString("theme_matter"));
T.setThemetitle(rs.getString("theme_title"));
themeinfo.add(T);
但只要把这行注释了就没问题T.setThemematter(rs.getString("theme_matter"));最后我是把theme_matter最先读就没问题,具体什么原因不太清楚,也许数long型的缘故,它可是可以保存几G的数据类型 展开
at oracle.jdbc.dbaccess.DBError.throwSqlException(DBError.java:134)
at oracle.jdbc.dbaccess.DBError.throwSqlException(DBError.java:179)
at oracle.jdbc.dbaccess.DBError.throwSqlException(DBError.java:269)
at oracle.jdbc.ttc7.LongTTCItem.getChars(LongTTCItem.java:189)
at oracle.jdbc.dbaccess.DBDataSetImpl.getCharsStreamItem(DBDataSetImpl.java:1630)
at oracle.jdbc.driver.OracleStatement.getCharsInternal(OracleStatement.java:3347)
at oracle.jdbc.driver.OracleStatement.getStringValue(OracleStatement.java:3556)
at oracle.jdbc.driver.OracleResultSetImpl.getString(OracleResultSetImpl.java:434)
at oracle.jdbc.driver.OracleResultSet.getString(OracleResultSet.java:1482)
at com.xianyifa.bbs.Theme.ThemeInfo.ThemeInfo(ThemeInfo.java:35)
at org.apache.jsp.bbs_002dxyf.show_002dposts_002dfloor.show_005fposts_005ffloor_jsp._jspService(show_005fposts_005ffloor_jsp.java:74)
at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:374)
at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:342)
at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:267)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:128)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:286)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:845)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:583)
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:447)
at java.lang.Thread.run(Thread.java:619)
2009-3-4 15:14:55 org.apache.catalina.startup.HostConfig deployDescriptor
总是抛这个异常我的代码:
Theme T = new Theme();
T.setEmittime(rs.getTimestamp("emit_time").toString());
T.setEmitusername(rs.getString("emit_user_name"));
T.setThemematter(rs.getString("theme_matter"));
T.setThemetitle(rs.getString("theme_title"));
themeinfo.add(T);
但只要把这行注释了就没问题T.setThemematter(rs.getString("theme_matter"));最后我是把theme_matter最先读就没问题,具体什么原因不太清楚,也许数long型的缘故,它可是可以保存几G的数据类型 展开
10个回答
展开全部
仔细查看java api文档,在ResultSet类的最上面有一段文字:
默认的 ResultSet 对象不可更新,仅有一个向前移动的指针。因此,只能迭代它一次,并且只能按从第一行到最后一行的顺序进行。
就是说ResultSet只能向前移动(rs.next()),而且只能按所取的记录的列的顺序来读取,所以这里:
String str1 = rs.getString(field1);
String str2 = rs.getString(field2);
如果所取的记录里字段field2在field1之前,而读取的时候读field2在读field1之后,那么就会抛出上述异常!!
解决方法非常简单:要么修改select语句使得field2在field1之前,要么修改读取语句的顺序!
默认的 ResultSet 对象不可更新,仅有一个向前移动的指针。因此,只能迭代它一次,并且只能按从第一行到最后一行的顺序进行。
就是说ResultSet只能向前移动(rs.next()),而且只能按所取的记录的列的顺序来读取,所以这里:
String str1 = rs.getString(field1);
String str2 = rs.getString(field2);
如果所取的记录里字段field2在field1之前,而读取的时候读field2在读field1之后,那么就会抛出上述异常!!
解决方法非常简单:要么修改select语句使得field2在field1之前,要么修改读取语句的顺序!
参考资料: http://hi.baidu.com/longrm/blog/item/801a19d7fba287daa144dfcf.html
展开全部
package DBCONN;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class DatabaseConnection
{
Connection conn=null;
Statement st=null;
ResultSet rs=null;
public Connection CreateConnection()
{
try
{
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
conn=DriverManager.getConnection("jdbc:sqlserver://localhost:1433;DatabaseName=demo","sa","");
}
catch (ClassNotFoundException e)
{
e.printStackTrace();
}
catch (SQLException e)
{
e.printStackTrace();
}
return conn;
}
public ResultSet Select(String sql)
{
try
{
st=conn.createStatement();
rs=st.executeQuery(sql);
}
catch (SQLException e)
{
e.printStackTrace();
}
return rs;
}
public boolean NotSelect(String sql)
{
boolean b=false;
try
{
st=conn.createStatement();
int i=st.executeUpdate(sql);
if(i>0)
{
b=true;
}
}
catch (SQLException e)
{
e.printStackTrace();
}
return b;
}
public void Close()
{
try
{
rs.close();
st.close();
conn.close();
}
catch (SQLException e)
{
e.printStackTrace();
}
}
}
用一下上面的代码连接数据库!!!试试!!!我都是用它的!!!很管用的!!!
但是要有jar包的!只用一个jar包就哦了!!!
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class DatabaseConnection
{
Connection conn=null;
Statement st=null;
ResultSet rs=null;
public Connection CreateConnection()
{
try
{
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
conn=DriverManager.getConnection("jdbc:sqlserver://localhost:1433;DatabaseName=demo","sa","");
}
catch (ClassNotFoundException e)
{
e.printStackTrace();
}
catch (SQLException e)
{
e.printStackTrace();
}
return conn;
}
public ResultSet Select(String sql)
{
try
{
st=conn.createStatement();
rs=st.executeQuery(sql);
}
catch (SQLException e)
{
e.printStackTrace();
}
return rs;
}
public boolean NotSelect(String sql)
{
boolean b=false;
try
{
st=conn.createStatement();
int i=st.executeUpdate(sql);
if(i>0)
{
b=true;
}
}
catch (SQLException e)
{
e.printStackTrace();
}
return b;
}
public void Close()
{
try
{
rs.close();
st.close();
conn.close();
}
catch (SQLException e)
{
e.printStackTrace();
}
}
}
用一下上面的代码连接数据库!!!试试!!!我都是用它的!!!很管用的!!!
但是要有jar包的!只用一个jar包就哦了!!!
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
T.setThemematter(rs.getString("theme_matter"));
好象是rs.getString的错误..
你改成rs.getLong试试
如果还不行,就看看dbaccess和java.sql的转换问题
这个,我好久没有直接和数据库打交道了,忘了,不好意思
好象是rs.getString的错误..
你改成rs.getLong试试
如果还不行,就看看dbaccess和java.sql的转换问题
这个,我好久没有直接和数据库打交道了,忘了,不好意思
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
java.sql.SQLException: 流已被关闭
是不是sql对象里有 单例 ?
如果没有 你获取 oracle的long话 用getInt试下
是不是sql对象里有 单例 ?
如果没有 你获取 oracle的long话 用getInt试下
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
关闭顺序
先关闭ResultSet
再关闭statement
最后关闭connection
先关闭ResultSet
再关闭statement
最后关闭connection
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询