【问题标题】:trying to read data from textfile - It is already opened exclusively by another user, or you need permission to view its data试图从文本文件中读取数据 - 它已被其他用户独占打开,或者您需要权限才能查看其数据
【发布时间】:2013-02-12 13:37:37
【问题描述】:
public class Txt {

     public static void main(String[] args) throws Exception {

         Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
         Connection con=DriverManager.getConnection("jdbc:odbc:txt");
         Statement st=con.createStatement();
         ResultSet rs=st.executeQuery("select eno,ename from emp");
         System.out.println(rs.getInt(1)+"   "+rs.getString(2));
     }
}

【问题讨论】:

  • 您的问题是什么?不要只是粘贴一段代码然后说“帮助”。 -1

标签: java jdbc


【解决方案1】:

您应该在代码末尾关闭连接

con.close();

使用这个例子:

public static void main(String[] args) throws Exception {

  try {
       Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
       Connection con=DriverManager.getConnection("jdbc:odbc:txt");
       Statement st=con.createStatement();
       ResultSet rs=st.executeQuery("select eno,ename from emp");
       System.out.println(rs.getInt(1)+"   "+rs.getString(2));
      }
  catch (SQLException e) {
       e.printStackTrace();
      }
  catch (Exception e) {
       e.printStackTrace();
      }
  finally {
       // Close the connection
       con.close();
      } 
}

祝你好运!

【讨论】:

  • con 应在try{} 之外声明,以便在finally{} 中可访问
  • 是的,它可以,但你应该最终使用。 finally 在所有情况下运行。如果由于其他异常而无法关闭连接,finally 阻止运行,您可以在那里关闭连接安全。
  • 我最后关闭了连接。在从文本文件读取数据的情况下它不起作用,但在将数据插入文本文件的情况下它正在工作。我正在使用 type1 jdbc 驱动程序
猜你喜欢
  • 2011-11-08
  • 1970-01-01
  • 2012-01-04
  • 2023-03-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-02-23
  • 1970-01-01
相关资源
最近更新 更多