尝试从文本文件中读取数据 - 它已由其他用户专门打开,或者您需要获得查看其数据的权限

时间:2013-02-12 13:37:38

标签: java jdbc

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 个答案:

答案 0 :(得分: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();
      } 
}

祝你好运!