首先说下odbc的注册原理,windows下odbc的注册均会在注册表odbc.ini 产生相应项,因此我们只用用java写入注册表即可
java写注册表需要一个com.ice.jni.registry 但是这个东西分32位和64位,因此要特别判断
下面以写一个名为jxy的odbc源为例
import com.ice.jni.registry.NoSuchKeyException; import com.ice.jni.registry.RegDWordValue; import com.ice.jni.registry.RegStringValue; import com.ice.jni.registry.Registry; import com.ice.jni.registry.RegistryException; import com.ice.jni.registry.RegistryKey; import com.ice.jni.registry.RegistryValue; import com.sun.corba.se.pept.transport.ContactInfo; public static void reg(){ //注册odbc Properties props = System.getProperties(); props.get("os.name"); if(((String) props.get("os.name")).toLowerCase().indexOf("windows")==-1){ //判断是否为windows return; } String last; File t=new File("ICE_JNIRegistry.dll"),s; if(t.exists())t.delete(); if(((String)props.get("os.arch")).indexOf("64")>-1){ //判断是否为64位系统 s=new File("ICE_JNIRegistry -64.dll"); last="ICE_JNIRegistry -64.dll"; s.renameTo(t); }else{ s=new File("ICE_JNIRegistry -32.dll"); last="ICE_JNIRegistry -32.dll"; s.renameTo(t); } try { String place; if(System.getenv("windir")!=null){ place=System.getenv("windir")+"\\system32"; place.replaceAll("\\\\{2}","\\\\"); }else{ place=(String) props.get("user.dir"); } RegistryKey odbc = Registry.HKEY_CURRENT_USER.openSubKey("Software").createSubKey("ODBC","").createSubKey("ODBC.INI",""); RegistryKey jxy=odbc.createSubKey("jxy", ""); jxy.setValue(new RegStringValue(jxy, "Driver",place+"\\SQLSRV32.dll")); //如果系统没有sql,则用当前目录的 jxy.setValue(new RegStringValue(jxy,"Server","JXY-THINK\\JXY_SQL_SERVER")); jxy.closeKey(); odbc.closeKey(); } catch (NoSuchKeyException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (RegistryException e) { // TODO Auto-generated catch block e.printStackTrace(); } s=new File("ICE_JNIRegistry.dll"); t=new File(last); s.renameTo(t); }
所需dll和jar均以图片格式上传,另存为保存成zip即可
如果用sql server 也可以不用odbc,直接用jdbc for sql server即可
private static String url="jdbc:sqlserver://JXY-THINK;instanceName=JXY_SQL_SERVER;database=学籍管理系统"; private static Connection con=null; private static String sql; private static Statement stmt; public static void init(){ try { Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver"); } catch (ClassNotFoundException e) { e.printStackTrace(); } } public static int login_s(String user,String password){ try { con=DriverManager.getConnection(url, user, password); stmt=con.createStatement(); } catch (SQLException e) { e.printStackTrace(); return 1; } return 0; }
所需文件依旧为图片……另存为zip