主要实现两个方法,一是执行没有结果集返回的executeUpdate()方法,如执行update, delete, insert一类的sql语句;
二是执行select查询语句的ResultSet executeQuery(String sql)方法,显然,这个方法将返回结果集。
package com.lxjdb.jdbc;
import java.sql.*;
public class LxjDbStatement implements java.sql.Statement {
public LxjDbConnection conntion;
public LxjDbApi lxjdb;
public long conn;
public LxjDbStatement(LxjDbConnection pconn){
conntion = pconn;
lxjdb = pconn.lxjdb;
conn = pconn.conn;
}
public ResultSet executeQuery(String sql) throws SQLException {
String[] dbInfo = new String[1];
int ret = lxjdb.Exec(conn, sql, dbInfo);
if(ret<0){
throw new SQLException(dbInfo[0]);
}
return new LxjDbResultSet(conntion);
}
public int executeUpdate(String sql) throws SQLException {
String[] dbInfo = new String[1];
int ret = lxjdb.Exec(conn, sql, dbInfo);
if(ret<0){
throw new SQLException(dbInfo[0]);
}
return ret;
}
} 4、ResultSet类
处理结果集。我们的内存 数据库比较简化,返回的数据都是字符串类型,因此只要实现getString(),其它什么getInt(), getLong(), getDouble()之类的数据类型就不予实现了。
当然,还要实现移动结果集游标行指针的一些方法。
package com.lxjdb.jdbc;
import java.io.InputStream;
import java.io.Reader;
import java.math.BigDecimal;
import java.net.URL;
import java.sql.*;
import java.util.Calendar;
import java.util.Map;
public class LxjDbResultSet implements java.sql.ResultSet {
public LxjDbApi lxjdb;
public long conn;
public LxjDbResultSet(LxjDbConnection pconn){
lxjdb = pconn.lxjdb;
conn = pconn.conn;
}
public boolean next() throws SQLException {
int ret = lxjdb.Next(conn);
return ret>0 ? true : false;
}
public boolean first() throws SQLException {
if(lxjdb.Rows(conn)<1)
return false;
int ret = lxjdb.GotoRec(conn, 1);
return ret>0 ? true : false;
}
public boolean last() throws SQLException {
int r = lxjdb.Rows(conn);
if(r<1)
return false;
int ret = lxjdb.GotoRec(conn, r);
return ret>0 ? true : false;
}
public String getString(int columnIndex) throws SQLException {
String[] retVal = new String[1];
lxjdb.GetValByIndex(conn, columnIndex-1, retVal); // columnIndex是从1开始的
return retVal[0];
}
public String getString(String columnLabel) throws SQLException {
String[] retVal = new String[1];
lxjdb.LxjDbGetValByName(conn, columnLabel, retVal);
return retVal[0];
}
public int getFetchSize() throws SQLException {
return lxjdb.Rows(conn);
}
}
第三步,编译后导出为jar文件:
在Eclipse下对准项目右键,选择“Export...”,导出LxjDbJdbc.jar。
这样就成功地实现了jdbc驱动。
下面再就可以进行测试了。
第四步,测试:
测试代码比较简单,不用过多解释:
import java.sql.*;
public class JdbcTest {
public static void main(String[] args) {
String driver = "com.lxjdb.jdbc.Driver";
String userName = "sa";
String passwrod = "********";
String url = "jdbc:lxjdb://192.168.0.106:2013";
String sql = "select * from OnlineUser";
try {
System.out.println("path:["+System.getProperties().get("java.library.path")+"]");
Class.forName(driver);
Connection conn = DriverManager.getConnection(url, userName,
passwrod);
Statement stmt = conn.createStatement() ;
int ret = stmt.executeUpdate("insert into OnlineUser(UserId, DevId, Addr, LastTime, Expires) values('9999','mac','192.168.0.106:888',getdate(),2000)");
System.out.println("executeUpdate:"+ret);
ResultSet rs = stmt.executeQuery(sql);
while (rs.next()) {
System.out.println("userId : " + rs.getString(1) + " dev : "
+ rs.getString(2) + " addr : " + rs.getString("Addr")+ " time : " + rs.getString(4));
}
// 关闭记录集
if (rs != null) {
try {
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
// 关闭链接对象
if (conn != null) {
try {
conn.close