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.