Commit 0dae2932 authored by Olivier Bertrand's avatar Olivier Bertrand

- New version of the java JdbcInterface

  modified:   storage/connect/JdbcInterface.class
  modified:   storage/connect/JdbcInterface.java

- Ignore *.tlog and .res files
  modified:   .gitignore
parent f8bc587c
...@@ -244,6 +244,8 @@ storage/mroonga/mysql-test/mroonga/storage/r/variable_version.result ...@@ -244,6 +244,8 @@ storage/mroonga/mysql-test/mroonga/storage/r/variable_version.result
*.exp *.exp
*.dep *.dep
*.idb *.idb
*.res
*.tlog
# Precompiled Headers # Precompiled Headers
*.gch *.gch
......
This diff was suppressed by a .gitattributes entry.
import java.math.*; import java.math.*;
import java.sql.*; import java.sql.*;
//import java.util.Arrays;
import java.util.Collections; import java.util.Collections;
import java.util.List; import java.util.List;
//import java.io.File;
//import java.lang.reflect.Field;
public class JdbcInterface { public class JdbcInterface {
boolean DEBUG = false; boolean DEBUG = false;
String Errmsg = "No error";
Connection conn = null; Connection conn = null;
DatabaseMetaData dbmd = null; DatabaseMetaData dbmd = null;
Statement stmt = null; Statement stmt = null;
...@@ -21,6 +25,20 @@ public class JdbcInterface { ...@@ -21,6 +25,20 @@ public class JdbcInterface {
DEBUG = b; DEBUG = b;
} // end of constructor } // end of constructor
private void SetErrmsg(Exception e) {
if (DEBUG)
System.out.println(e.getMessage());
Errmsg = e.toString();
} // end of SetErrmsg
public String GetErrmsg() {
String err = Errmsg;
Errmsg = "No error";
return err;
} // end of GetErrmsg
public int JdbcConnect(String[] parms, int fsize, boolean scrollable) { public int JdbcConnect(String[] parms, int fsize, boolean scrollable) {
int rc = 0; int rc = 0;
...@@ -32,7 +50,9 @@ public class JdbcInterface { ...@@ -32,7 +50,9 @@ public class JdbcInterface {
System.out.println("In try block"); System.out.println("In try block");
if (parms[0] != null && !parms[0].isEmpty()) { if (parms[0] != null && !parms[0].isEmpty()) {
System.out.println("b is true!"); if (DEBUG)
System.out.println("Loading class" + parms[0]);
Class.forName(parms[0]); //loads the driver Class.forName(parms[0]); //loads the driver
} // endif driver } // endif driver
...@@ -76,50 +96,40 @@ public class JdbcInterface { ...@@ -76,50 +96,40 @@ public class JdbcInterface {
} // endif fsize } // endif fsize
} catch(ClassNotFoundException e) { } catch(ClassNotFoundException e) {
System.err.println("ClassNotFoundException: " + e.getMessage()); SetErrmsg(e);
rc = 1; rc = -1;
} catch (SQLException se) { } catch (SQLException se) {
System.out.println("SQL Exception:"); SetErrmsg(se);
rc = -2;
// Loop through the SQL Exceptions
while (se != null) {
System.out.println("State : " + se.getSQLState());
System.out.println("Message: " + se.getMessage());
System.out.println("Error : " + se.getErrorCode());
se = se.getNextException();
} // end while se
rc = 2;
} catch( Exception e ) { } catch( Exception e ) {
System.out.println(e); SetErrmsg(e);
rc = 3; rc = -3;
} // end try/catch } // end try/catch
return rc; return rc;
} // end of JdbcConnect } // end of JdbcConnect
public boolean CreatePrepStmt(String sql) { public int CreatePrepStmt(String sql) {
boolean b = false; int rc = 0;
try { try {
pstmt = conn.prepareStatement(sql); pstmt = conn.prepareStatement(sql);
} catch (SQLException se) { } catch (SQLException se) {
System.out.println(se); SetErrmsg(se);
b = true; rc = -1;
} catch (Exception e) { } catch (Exception e) {
System.out.println(e); SetErrmsg(e);
b = true; rc = -2;
} // end try/catch } // end try/catch
return b; return rc;
} // end of CreatePrepStmt } // end of CreatePrepStmt
public void SetStringParm(int i, String s) { public void SetStringParm(int i, String s) {
try { try {
pstmt.setString(i, s); pstmt.setString(i, s);
} catch (Exception e) { } catch (Exception e) {
System.out.println(e); SetErrmsg(e);
} // end try/catch } // end try/catch
} // end of SetStringParm } // end of SetStringParm
...@@ -128,7 +138,7 @@ public class JdbcInterface { ...@@ -128,7 +138,7 @@ public class JdbcInterface {
try { try {
pstmt.setInt(i, n); pstmt.setInt(i, n);
} catch (Exception e) { } catch (Exception e) {
System.out.println(e); SetErrmsg(e);
} // end try/catch } // end try/catch
} // end of SetIntParm } // end of SetIntParm
...@@ -137,7 +147,7 @@ public class JdbcInterface { ...@@ -137,7 +147,7 @@ public class JdbcInterface {
try { try {
pstmt.setShort(i, n); pstmt.setShort(i, n);
} catch (Exception e) { } catch (Exception e) {
System.out.println(e); SetErrmsg(e);
} // end try/catch } // end try/catch
} // end of SetShortParm } // end of SetShortParm
...@@ -146,7 +156,7 @@ public class JdbcInterface { ...@@ -146,7 +156,7 @@ public class JdbcInterface {
try { try {
pstmt.setLong(i, n); pstmt.setLong(i, n);
} catch (Exception e) { } catch (Exception e) {
System.out.println(e); SetErrmsg(e);
} // end try/catch } // end try/catch
} // end of SetBigintParm } // end of SetBigintParm
...@@ -155,7 +165,7 @@ public class JdbcInterface { ...@@ -155,7 +165,7 @@ public class JdbcInterface {
try { try {
pstmt.setFloat(i, f); pstmt.setFloat(i, f);
} catch (Exception e) { } catch (Exception e) {
System.out.println(e); SetErrmsg(e);
} // end try/catch } // end try/catch
} // end of SetFloatParm } // end of SetFloatParm
...@@ -164,7 +174,7 @@ public class JdbcInterface { ...@@ -164,7 +174,7 @@ public class JdbcInterface {
try { try {
pstmt.setDouble(i, d); pstmt.setDouble(i, d);
} catch (Exception e) { } catch (Exception e) {
System.out.println(e); SetErrmsg(e);
} // end try/catch } // end try/catch
} // end of SetDoubleParm } // end of SetDoubleParm
...@@ -173,7 +183,7 @@ public class JdbcInterface { ...@@ -173,7 +183,7 @@ public class JdbcInterface {
try { try {
pstmt.setTimestamp(i, t); pstmt.setTimestamp(i, t);
} catch (Exception e) { } catch (Exception e) {
System.out.println(e); SetErrmsg(e);
} // end try/catch } // end try/catch
} // end of SetTimestampParm } // end of SetTimestampParm
...@@ -184,10 +194,10 @@ public class JdbcInterface { ...@@ -184,10 +194,10 @@ public class JdbcInterface {
if (pstmt != null) try { if (pstmt != null) try {
n = pstmt.executeUpdate(); n = pstmt.executeUpdate();
} catch (SQLException se) { } catch (SQLException se) {
System.out.println(se); SetErrmsg(se);
n = -1; n = -1;
} catch (Exception e) { } catch (Exception e) {
System.out.println(e); SetErrmsg(e);
n = -2; n = -2;
} //end try/catch } //end try/catch
...@@ -201,10 +211,10 @@ public class JdbcInterface { ...@@ -201,10 +211,10 @@ public class JdbcInterface {
pstmt.close(); pstmt.close();
pstmt = null; pstmt = null;
} catch (SQLException se) { } catch (SQLException se) {
System.out.println(se); SetErrmsg(se);
b = true; b = true;
} catch (Exception e) { } catch (Exception e) {
System.out.println(e); SetErrmsg(e);
b = true; b = true;
} // end try/catch } // end try/catch
...@@ -220,26 +230,30 @@ public class JdbcInterface { ...@@ -220,26 +230,30 @@ public class JdbcInterface {
System.out.println("Cancelling statement"); System.out.println("Cancelling statement");
stmt.cancel(); stmt.cancel();
} catch(SQLException se) { } catch(SQLException se) {
System.out.println(se); SetErrmsg(se);
rc += 1; rc += 1;
} // nothing more we can do } // nothing more we can do
// Close the statement and the connection // Close the statement and the connection
if (rs != null) if (rs != null)
try { try {
if (DEBUG)
System.out.println("Closing result set"); System.out.println("Closing result set");
rs.close(); rs.close();
} catch(SQLException se) { } catch(SQLException se) {
System.out.println(se); SetErrmsg(se);
rc = 2; rc = 2;
} // nothing more we can do } // nothing more we can do
if (stmt != null) if (stmt != null)
try { try {
if (DEBUG)
System.out.println("Closing statement"); System.out.println("Closing statement");
stmt.close(); stmt.close();
} catch(SQLException se) { } catch(SQLException se) {
System.out.println(se); SetErrmsg(se);
rc += 4; rc += 4;
} // nothing more we can do } // nothing more we can do
...@@ -247,14 +261,18 @@ public class JdbcInterface { ...@@ -247,14 +261,18 @@ public class JdbcInterface {
if (conn != null) if (conn != null)
try { try {
if (DEBUG)
System.out.println("Closing connection"); System.out.println("Closing connection");
conn.close(); conn.close();
} catch (SQLException se) { } catch (SQLException se) {
System.out.println(se); SetErrmsg(se);
rc += 8; rc += 8;
} //end try/catch } //end try/catch
if (DEBUG)
System.out.println("All closed"); System.out.println("All closed");
return rc; return rc;
} // end of JdbcDisconnect } // end of JdbcDisconnect
...@@ -281,7 +299,8 @@ public class JdbcInterface { ...@@ -281,7 +299,8 @@ public class JdbcInterface {
} // endswitch n } // endswitch n
} catch(Exception e) { } catch(Exception e) {
System.out.println(e); SetErrmsg(e);
m = -1;
} // end try/catch } // end try/catch
return m; return m;
...@@ -300,7 +319,7 @@ public class JdbcInterface { ...@@ -300,7 +319,7 @@ public class JdbcInterface {
} // endif rs } // endif rs
} catch(SQLException se) { } catch(SQLException se) {
System.out.println(se); SetErrmsg(se);
} // end try/catch } // end try/catch
return ncol; return ncol;
...@@ -325,7 +344,7 @@ public class JdbcInterface { ...@@ -325,7 +344,7 @@ public class JdbcInterface {
} // endif rs } // endif rs
} catch(SQLException se) { } catch(SQLException se) {
System.out.println(se); SetErrmsg(se);
} // end try/catch } // end try/catch
return ncol; return ncol;
...@@ -349,10 +368,10 @@ public class JdbcInterface { ...@@ -349,10 +368,10 @@ public class JdbcInterface {
System.out.println("Query '" + query + "' executed: n = " + n); System.out.println("Query '" + query + "' executed: n = " + n);
} catch (SQLException se) { } catch (SQLException se) {
System.out.println(se); SetErrmsg(se);
n = -1; n = -1;
} catch (Exception e) { } catch (Exception e) {
System.out.println(e); SetErrmsg(e);
n = -2; n = -2;
} //end try/catch } //end try/catch
...@@ -375,10 +394,10 @@ public class JdbcInterface { ...@@ -375,10 +394,10 @@ public class JdbcInterface {
} // endif rs } // endif rs
} catch (SQLException se) { } catch (SQLException se) {
System.out.println(se); SetErrmsg(se);
ncol = -1; ncol = -1;
} catch (Exception e) { } catch (Exception e) {
System.out.println(e); SetErrmsg(e);
ncol = -2; ncol = -2;
} //end try/catch } //end try/catch
...@@ -402,10 +421,10 @@ public class JdbcInterface { ...@@ -402,10 +421,10 @@ public class JdbcInterface {
} // endif DEBUG } // endif DEBUG
} catch (SQLException se) { } catch (SQLException se) {
System.out.println(se); SetErrmsg(se);
ncol = -1; ncol = -1;
} catch (Exception e) { } catch (Exception e) {
System.out.println(e); SetErrmsg(e);
ncol = -2; ncol = -2;
} //end try/catch } //end try/catch
...@@ -425,22 +444,22 @@ public class JdbcInterface { ...@@ -425,22 +444,22 @@ public class JdbcInterface {
System.out.println("Update Query '" + query + "' executed: n = " + n); System.out.println("Update Query '" + query + "' executed: n = " + n);
} catch (SQLException se) { } catch (SQLException se) {
System.out.println(se); SetErrmsg(se);
n = -1; n = -1;
} catch (Exception e) { } catch (Exception e) {
System.out.println(e); SetErrmsg(e);
n = -2; n = -2;
} //end try/catch } //end try/catch
return n; return n;
} // end of ExecuteQuery } // end of ExecuteUpdate
public int ReadNext() { public int ReadNext() {
if (rs != null) { if (rs != null) {
try { try {
return rs.next() ? 1 : 0; return rs.next() ? 1 : 0;
} catch (SQLException se) { } catch (SQLException se) {
System.out.println(se); SetErrmsg(se);
return -1; return -1;
} //end try/catch } //end try/catch
...@@ -454,7 +473,7 @@ public class JdbcInterface { ...@@ -454,7 +473,7 @@ public class JdbcInterface {
try { try {
return rs.absolute(row); return rs.absolute(row);
} catch (SQLException se) { } catch (SQLException se) {
System.out.println(se); SetErrmsg(se);
return false; return false;
} //end try/catch } //end try/catch
...@@ -469,7 +488,7 @@ public class JdbcInterface { ...@@ -469,7 +488,7 @@ public class JdbcInterface {
} else try { } else try {
return rsmd.getColumnLabel(n); return rsmd.getColumnLabel(n);
} catch (SQLException se) { } catch (SQLException se) {
System.out.println(se); SetErrmsg(se);
} //end try/catch } //end try/catch
return null; return null;
...@@ -484,10 +503,10 @@ public class JdbcInterface { ...@@ -484,10 +503,10 @@ public class JdbcInterface {
return rsmd.getColumnType(n); return rsmd.getColumnType(n);
} catch (SQLException se) { } catch (SQLException se) {
System.out.println("ColumnType: " + se); SetErrmsg(se);
} //end try/catch } //end try/catch
return 0; return 666; // Not a type
} // end of ColumnType } // end of ColumnType
public String ColumnDesc(int n, int[] val) { public String ColumnDesc(int n, int[] val) {
...@@ -501,11 +520,11 @@ public class JdbcInterface { ...@@ -501,11 +520,11 @@ public class JdbcInterface {
val[3] = rsmd.isNullable(n); val[3] = rsmd.isNullable(n);
return rsmd.getColumnLabel(n); return rsmd.getColumnLabel(n);
} catch (SQLException se) { } catch (SQLException se) {
System.out.println(se); SetErrmsg(se);
} //end try/catch } //end try/catch
return null; return null;
} // end of ColumnType } // end of ColumnDesc
public String StringField(int n, String name) { public String StringField(int n, String name) {
if (rs == null) { if (rs == null) {
...@@ -513,7 +532,7 @@ public class JdbcInterface { ...@@ -513,7 +532,7 @@ public class JdbcInterface {
} else try { } else try {
return (n > 0) ? rs.getString(n) : rs.getString(name); return (n > 0) ? rs.getString(n) : rs.getString(name);
} catch (SQLException se) { } catch (SQLException se) {
System.out.println(se); SetErrmsg(se);
} //end try/catch } //end try/catch
return null; return null;
...@@ -525,7 +544,7 @@ public class JdbcInterface { ...@@ -525,7 +544,7 @@ public class JdbcInterface {
} else try { } else try {
return (n > 0) ? rs.getInt(n) : rs.getInt(name); return (n > 0) ? rs.getInt(n) : rs.getInt(name);
} catch (SQLException se) { } catch (SQLException se) {
System.out.println(se); SetErrmsg(se);
} //end try/catch } //end try/catch
return 0; return 0;
...@@ -538,7 +557,7 @@ public class JdbcInterface { ...@@ -538,7 +557,7 @@ public class JdbcInterface {
BigDecimal bigDecimal = (n > 0) ? rs.getBigDecimal(n) : rs.getBigDecimal(name); BigDecimal bigDecimal = (n > 0) ? rs.getBigDecimal(n) : rs.getBigDecimal(name);
return bigDecimal != null ? bigDecimal.longValue() : 0; return bigDecimal != null ? bigDecimal.longValue() : 0;
} catch (SQLException se) { } catch (SQLException se) {
System.out.println(se); SetErrmsg(se);
} //end try/catch } //end try/catch
return 0; return 0;
...@@ -550,7 +569,7 @@ public class JdbcInterface { ...@@ -550,7 +569,7 @@ public class JdbcInterface {
} else try { } else try {
return (n > 0) ? rs.getDouble(n) : rs.getDouble(name); return (n > 0) ? rs.getDouble(n) : rs.getDouble(name);
} catch (SQLException se) { } catch (SQLException se) {
System.out.println(se); SetErrmsg(se);
} //end try/catch } //end try/catch
return 0.; return 0.;
...@@ -562,7 +581,7 @@ public class JdbcInterface { ...@@ -562,7 +581,7 @@ public class JdbcInterface {
} else try { } else try {
return (n > 0) ? rs.getFloat(n) : rs.getFloat(name); return (n > 0) ? rs.getFloat(n) : rs.getFloat(name);
} catch (SQLException se) { } catch (SQLException se) {
System.out.println(se); SetErrmsg(se);
} //end try/catch } //end try/catch
return 0; return 0;
...@@ -574,7 +593,7 @@ public class JdbcInterface { ...@@ -574,7 +593,7 @@ public class JdbcInterface {
} else try { } else try {
return (n > 0) ? rs.getBoolean(n) : rs.getBoolean(name); return (n > 0) ? rs.getBoolean(n) : rs.getBoolean(name);
} catch (SQLException se) { } catch (SQLException se) {
System.out.println(se); SetErrmsg(se);
} //end try/catch } //end try/catch
return false; return false;
...@@ -586,7 +605,7 @@ public class JdbcInterface { ...@@ -586,7 +605,7 @@ public class JdbcInterface {
} else try { } else try {
return (n > 0) ? rs.getDate(n) : rs.getDate(name); return (n > 0) ? rs.getDate(n) : rs.getDate(name);
} catch (SQLException se) { } catch (SQLException se) {
System.out.println(se); SetErrmsg(se);
} //end try/catch } //end try/catch
return null; return null;
...@@ -598,7 +617,7 @@ public class JdbcInterface { ...@@ -598,7 +617,7 @@ public class JdbcInterface {
} else try { } else try {
return (n > 0) ? rs.getTime(n) : rs.getTime(name); return (n > 0) ? rs.getTime(n) : rs.getTime(name);
} catch (SQLException se) { } catch (SQLException se) {
System.out.println(se); SetErrmsg(se);
} //end try/catch } //end try/catch
return null; return null;
...@@ -610,12 +629,24 @@ public class JdbcInterface { ...@@ -610,12 +629,24 @@ public class JdbcInterface {
} else try { } else try {
return (n > 0) ? rs.getTimestamp(n) : rs.getTimestamp(name); return (n > 0) ? rs.getTimestamp(n) : rs.getTimestamp(name);
} catch (SQLException se) { } catch (SQLException se) {
System.out.println(se); SetErrmsg(se);
} //end try/catch } //end try/catch
return null; return null;
} // end of TimestampField } // end of TimestampField
public String ObjectField(int n, String name) {
if (rs == null) {
System.out.println("No result set");
} else try {
return (n > 0) ? rs.getObject(n).toString() : rs.getObject(name).toString();
} catch (SQLException se) {
SetErrmsg(se);
} //end try/catch
return null;
} // end of ObjectField
public int GetDrivers(String[] s, int mxs) { public int GetDrivers(String[] s, int mxs) {
int n = 0; int n = 0;
List<Driver> drivers = Collections.list(DriverManager.getDrivers()); List<Driver> drivers = Collections.list(DriverManager.getDrivers());
...@@ -636,4 +667,46 @@ public class JdbcInterface { ...@@ -636,4 +667,46 @@ public class JdbcInterface {
return size; return size;
} // end of GetDrivers } // end of GetDrivers
/**
* Adds the specified path to the java library path
* from Fahd Shariff blog
*
* @param pathToAdd the path to add
static public int addLibraryPath(String pathToAdd) {
System.out.println("jpath = " + pathToAdd);
try {
Field usrPathsField = ClassLoader.class.getDeclaredField("usr_paths");
usrPathsField.setAccessible(true);
//get array of paths
String[] paths = (String[])usrPathsField.get(null);
//check if the path to add is already present
for (String path : paths) {
System.out.println("path = " + path);
if (path.equals(pathToAdd))
return -5;
} // endfor path
//add the new path
String[] newPaths = Arrays.copyOf(paths, paths.length + 1);
newPaths[paths.length] = pathToAdd;
usrPathsField.set(null, newPaths);
System.setProperty("java.library.path",
System.getProperty("java.library.path") + File.pathSeparator + pathToAdd);
Field fieldSysPath = ClassLoader.class.getDeclaredField("sys_paths");
fieldSysPath.setAccessible(true);
fieldSysPath.set(null, null);
} catch (Exception e) {
SetErrmsg(e);
return -1;
} // end try/catch
return 0;
} // end of addLibraryPath
*/
} // end of class JdbcInterface } // end of class JdbcInterface
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment