tion {
return new LxjDbStatement(this);
}
} 3¡¢StatementÀà
Ö÷ҪʵÏÖÁ½¸ö·½·¨£¬Ò»ÊÇÖ´ÐÐûÓнá¹û¼¯·µ»ØµÄ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Çý¶¯¡£
ÏÂÃæÔپͿÉÒÔ½øÐвâÊÔÁË¡£
µÚËIJ½£¬²âÊÔ£º
²âÊÔ´úÂë±È½Ï¼òµ¥£¬²»Óùý¶à½âÊÍ£º
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