我有一个基本的代码片段下面,但它不是working.What可能是它的问题。
public List<String> getStores() throws SQLException{
List<String> store_id=new ArrayList<>();
String query="select distinct(store_id) from stores";
Connection con=ConnectDatabase.getDb2ConObj();
Statement stmt=con.createStatement();
java.sql.ResultSet rsResultSet=stmt.executeQuery(query);
while(rsResultSet.next()){
store_id.add(rsResultSet.getString(1));
}
con.close();
return store_id;
}它抛出了下面的异常
com.mysql.jdbc.exceptions.MySQLNonTransientConnectionException: No operations allowed after connection closed.
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:888)
at com.mysql.jdbc.Connection.checkClosed(Connection.java:1931)
at com.mysql.jdbc.Connection.createStatement(Connection.java:3087)
at com.mysql.jdbc.Connection.createStatement(Connection.java:3069)
at com.dao.StoreDao.getStores(StoreDao.java:52)
at org.apache.jsp.adminViewAllStore_jsp._jspService(adminViewAllStore_jsp.java:119)ConnectDatabse的代码是
public class ConnectDatabase {
static Connection con=null;
static String connectionString="jdbc:mysql://localhost:3306/ayurveda";
static String username="root";
static String password="";
public static Connection getDb2ConObj(){
if(con==null){
try{
Class.forName("com.mysql.jdbc.Driver");
con=DriverManager.getConnection(connectionString,username,password);
}
catch(ClassNotFoundException | SQLException e)
{
System.out.println("Connect initialized with error"+e.getMessage());
e.printStackTrace();
}
}
return con;
}我不能理解same.What的原因可能是在我完成连接后关闭连接的problem.Since。
发布于 2014-07-20 22:31:41
在我将它封装在try catch finally block.Changed中之后,它工作了,代码如下所示
public List<String> getStores() throws SQLException{
List<String> store_id=new ArrayList<>();
Connection con=ConnectDatabase.getDb2ConObj();
try{
String query="select distinct(store_id) from stores";
Statement stmt=con.createStatement();
java.sql.ResultSet rsResultSet=stmt.executeQuery(query);
while(rsResultSet.next()){
store_id.add(rsResultSet.getString(1));
}
}catch(Exception e){
}finally{
con.close();
}
return store_id;
}谢谢。
发布于 2014-07-20 22:35:03
使用Java 7 -The try-with-resources Statement
根据oracle documentation,您可以将try- with -resources块与常规try块组合在一起
典型的Java应用程序操作几种类型的资源,如文件、流、套接字和数据库连接。这样的资源必须非常小心地处理,因为它们获取用于其操作的系统资源。因此,即使在出现错误的情况下,您也需要来确保它们被释放。
实际上,错误的资源管理是生产应用程序中常见的故障来源,通常的陷阱是数据库连接和文件描述符在代码中的其他位置发生异常后仍保持打开状态。这导致应用程序服务器在资源耗尽时频繁重新启动,因为操作系统和服务器应用程序通常具有资源的上限。
示例代码:
try(Connection con = getConnection()) {
...
}阅读更多Java 7 Automatic Resource Management JDBC
Statement和ResultSet。static { try { Class.forName("com.mysql.jdbc.Driver");} catch (ClassNotFoundException e) { e.printStackTrace();}}
请参阅Connecting with DataSource Objects上的Java教程
我已经发布了一个很好的ConnectionUtil类来管理整个application.的单个类中的所有连接
发布于 2014-07-20 23:12:43
您可以使用这种类型的code...for解决您的问题
public void actionPerformed(ActionEvent ae)
{
String u=t1.getText();
String p=t2.getText();
if(ae.getSource()==b1)
{
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con=DriverManager.getConnection("jdbc:odbc:newdsn");
String stp="SELECT * FROM reg";
Statement sa=con.createStatement();
rs=sa.executeQuery(stp);
while(rs.next())
{
String du=rs.getString(2);
String dp=rs.getString(3);
if(du.equals(u)&&dp.equals(p))
{
a=0;
break;
}else{ a=1;}
}
if(a==0){
JOptionPane.showMessageDialog(this,"LOGIN PAGE","Login is successful",1);
}
if(a==1){
JOptionPane.showMessageDialog(this,"LOGIN PAGE","Login is not successful",1);
}}
catch(Exception e){}
}}如果甚至抛出异常,那么您应该检查系统的32位或64位bit..you应尝试如果64位,那么请使您的dsn在32位,并使用ms access 2002-2003版本
然后你就可以得到tour solution .....thank了
https://stackoverflow.com/questions/24851444
复制相似问题