To access DBF files through Java Dynamic creation of ODBC data sources

Source: Internet
Author: User
Tags odbc

Using Java to dynamically create ODBC data sources to access DBF files requires the use of registry to modify the registry.

In fact, the main is the dynamic creation of ODBC data sources, the beginning is very simple, you can set up a data source manually, of course, can be directly generated through the program, the problem is not big. The following only say how to modify.

import com.ice.jni.registry.RegStringValue;
import com.ice.jni.registry.Registry;
import com.ice.jni.registry.RegistryKey;
public class TestC {
 public static void main(String[] str) {
  try {
  RegistryKey child = Registry.HKEY_CURRENT_USER
.openSubKey("Software").openSubKey("ODBC").openSubKey("ODBC.INI").
   openSubKey("data_0930",RegistryKey.ACCESS_ALL);
   //操作权限是通过RegistryKey来获取的。
   String de = "F:\\commony\\test\\data\\070901";
   //我的DBF数据的目录
   //其中,data_0930是我第一次设置的数据源的一个注册表的名称
   System.out.println(child.getStringValue("SourceDB"));
   child.setValue(new RegStringValue(child,"SourceDB",de));
   System.out.println(child.getFullName());
  } catch (Exception e) {
   e.printStackTrace();
  }
 }
}

And then it's through, sun.jdbc.odbc.JdbcOdbcDriver to get the data,

import java.sql.DriverManager;
import java.sql.*;
public class TestOdbc {
 public TestOdbc() {}
 public static void main(String[] args) {
  java.sql.Connection conn = null;
  java.sql.PreparedStatement pt = null;
  java.sql.ResultSet rs = null;
  try {
   Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
  }
  catch (ClassNotFoundException ex1) {
  }
  try {
   conn = DriverManager.getConnection("jdbc:odbc:data_0930", "", "");
   pt = conn.prepareStatement(" select * from test_table");
   rs = pt.executeQuery();
   while(rs.next()){
    System.out.println("==="+rs.getString(1));
    System.out.println("==="+rs.getString(2));
    System.out.println("==="+rs.getString(3));
    System.out.println("==="+rs.getString(4));
    System.out.println("==="+rs.getString(5));
   }
  }
  catch (SQLException ex) {}
 }
}

In fact, the file name, can be dynamically acquired, generally through the Java file class to get:

import java.io.File;
public class TestD {
 public static void main(String[] args){
  File file = new File("F:\\commony\\test\\data");
  File[] df = file.listFiles();
  for(int k =0;k<df.length;k++){
   if(df[k].isDirectory()){
    //因为文件夹中包含DBF文件,所以判断是文件夹,而不是文件
    System.out.println("===kkkk=="+k+"====="+df[k].getName());
   }
  }
 }
}

The first is this, need to pay attention to is the use of registry, in fact very simple, as long as the DLL file into the classpath can be.

The second is actually more simple, that is, through another Kaiyuan class package Jdbf.jar, the use of the method is also very simple, the internet has a lot of information can be queried. I will not say more.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.