v->SetObjectArrayElement(retVal, 0, env->NewStringUTF(val));
return(ret);
}
ÔÙ¹¹ÔìÒ»¸ömakefile£º
LIBFLAG= -lstdc++ -lpthread -ldl
CPPFLAGS = -c -fPIC -I /Library/Java/JavaVirtualMachines/jdk1.8.0_05.jdk/Contents/Home/include -I /System/Library/Frameworks/JavaVM.framework/Versions/A/Headers
COMMON_OBJs = com_lxjdb_jdbc_LxjDbApi.o
SHARELIB = -lLxjDbApi
libLxjDbJdbcApi: $(COMMON_OBJs)
gcc $(COMMON_OBJs) $(LIBFLAG) $(SHARELIB) -rdynamic -dynamiclib -fPIC -install_name /usr/lib/libLxjDbJdbcApi.dylib -o libLxjDbJdbcApi.dylib
makeºó½«Éú³ÉÒ»¸öos xÏµĹ²Ïí¿â£º
libLxjDbJdbcApi.dylib
×¢Ò⣬ÒòΪjavaµÄjniÔÚmacÏÂʹÓù²Ïí¿â±È½ÏÌØ±ð£¬ÐèÒª½«¸ÃÎļþ¸ÄÃûΪlibLxjDbJdbcApi.jnilib²¢¿½±´µ½/usr/lib/javaĿ¼Ï¡£
µÚ¶þ²½£¬ÊµÏÖ¸÷½Ó¿ÚÀࣺ
1¡¢DriverÀà
×¢ÒâÎñ±ØÒªÖØÐ´jdbcCompliant()·½·¨²¢·µ»Øfalse£¬±íʾÎÒÃDz»ÐèÒªÈ«Ãæ¼æÈÝʵÏÖËùÓеÄjdbc½Ó¿Ú£¬Ö»ÒªÊµÏÖ±ØÐëµÄ²¿·Ö¡£
Ö÷ҪʵÏÖconnect()·½·¨£¬´úÂëÈçÏ£º
package com.lxjdb.jdbc;
import java.sql.*;
import java.util.*;
import java.util.logging.Logger;
public class Driver implements java.sql.Driver{
public LxjDbApi lxjdb;
static{
try{
java.sql.DriverManager.registerDriver(new Driver());
}
catch(SQLException e){
// System.err.println(e);
throw new RuntimeException("Can't register driver!");
}
}
public Driver() throws SQLException {
// Required for Class.forName().newInstance()
lxjdb = new LxjDbApi();
}
public boolean acceptsURL(String url) throws SQLException{
return url.startsWith("jdbc:lxjdb://");
}
public Connection connect(String url, Properties info) throws SQLException{
if( !acceptsURL( url)){
return null;
}
// Òª·Ö½âurlµÃµ½Ö÷»úµØÖ·ºÍ¶Ë¿ÚºÅ£¬²¢´ÓinfoµÃµ½Óû§ÃûºÍÃÜÂë
try {
String[] arr=url.split("//");
String url2 = arr[1];
String[] arr2=url2.split(":");
String host = arr2[0];
int port = Integer.parseInt(arr2[1]);
String user = info.getProperty("user");
String pwd = info.getProperty("password");
return new LxjDbConnection(lxjdb, host, port, user, pwd);
}
catch(Exception e){
throw new SQLException(e.getMessage());
}
}
public boolean jdbcCompliant(){
return false;
}
public DriverPropertyInfo[] getPropertyInfo(String url, Properties info)
throws SQLException {
// TODO Auto-generated method stub
return new DriverPropertyInfo[0];
}
public int getMajorVersion() {
// TODO Auto-generated method stub
return 1;
}
public int getMinorVersion() {
// TODO Auto-generated method stub
return 0;
}
public Logger getParentLogger() throws SQLFeatureNotSupportedException {
// TODO Auto-generated method stub
return null;
}
}
2¡¢ConnectionÀà
Ö÷ÒªÊÇʵÏÖcreateStatement()·½·¨£º
package com.lxjdb.jdbc;
import java.sql.*;
import java.util.Map;
import java.util.Properties;
import java.util.concurrent.Executor;
public class LxjDbConnection implements java.sql.Connection{
public long conn=0;
public LxjDbApi lxjdb;
public LxjDbConnection(LxjDbApi plxjdb, String host, int port, String user, String pwd)
throws SQLException {
lxjdb = plxjdb;
conn = lxjdb.Open(host, port, user, pwd);
if(conn==0){
String info = "Open Err: "+host+":"+port+",user="+user;
throw new SQLException(info);
}
}
public Statement createStatement() throws SQLExcep