MBO ResultSet Filters - JDBC Driver - java

I am attempting to combine two disparate data sources into one MBO. The sybase documentation states that you have to develop a custom result set filter in java. Ok. No big deal. I am somewhat familiar with JDBC Rowsets so I think I can probably handle this. So I go grab JConnect 7 and am attempting to use the JDBC driver in my result set filter to get a result set that I can use to filter the data set returned from the web service I am connecting to. I have tested my connection with the simple test below:
try
{
DriverManager.registerDriver((Driver)Class.forName("com.sybase.jdbc4.jdbc.SybDriver").newInstance());
Connection conn = DriverManager.getConnection("jdbc:sybase:Tds:localhost:5500", "dba", "sql");
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery("Select * from traveler where traveler_id = 1");
while (rs.next())
{
System.out.println(rs.getRow());
}
} catch (Exception se)
{
se.printStackTrace();
}
Of which I get a rowcount of one back, which is what I am expecting. The problem is when I implement this same code in the ResultSet filter class, and attempt to preview it in the MBO, I get the following error in the eclipse debug console (distilled for clarity):
22:14:20 [ERROR] [ExecuteSection]: Execution error
java.lang.reflect.InvocationTargetException...
[...]
Caused by: java.lang.UnsatisfiedLinkError: no dbjodbc11 in java.library.path
This looks like a classpath issue, but I have the Jconnect jar in my build path, and that is verified by the fact that my little test runs successfully. Is this an eclipse issue.. is eclipse using a different classpath when executing code for the Mobile workspace project?

Can't remember the exact path right now, but there is a third-party lib folder under the <SybaseInstallation>/UnwiredWorkspace folder. Try adding your jar there.

Related

Java sqlite error (error or missing database) no such table

This is the class where I connect sqlite
public static Connection ConnectDB() {
try {
Class.forName("org.sqlite.JDBC");
Connection con= DriverManager.getConnection("jdbc:sqlite:C:\\Users\\abh\\eclipse-workspace \\Training\\Database.db");
return con;
}
catch (Exception e) {
JOptionPane.showMessageDialog(null,e);
return null;
}
}
The jar file inside the project folder name is sqlite-jdbc-3.7.2 I'm confused whether it's because of the version or if it's from my database created in sqlite
SQLite is a pretty good database platform, I highly doubt it's the problem as is the JDBC version your using as well.
The Connection method you presented does indeed work but you need to keep in mind that the connection has nothing to do with tables. It has everything to towards a connection to a particular SQLite database file and if the path doesn't exist to get to the database file specified, then it simply can not find that database. If the path does exist, and local system permissions allow it, and the database file does not exist, then the connection call will automatically create the database file specified but it will be empty. Never the less, the connection is now made and ready for a database Table to be either Created or Accessed via a query (providing there is actual data in the table).
If there was an error regards to the connection then you would receive a Message Box on the screen, and if you do then it's probably because of the whitespace that exists in the specified database path between eclipse-workspace and \\Training:
"jdbc:sqlite:C:\\Users\\abh\\eclipse-workspace \\Training\\Database.db"
Edit: Oh...as already specified by #lazylead :)
On a side: It's not a good idea to hard-code the path to your database file. This will never be the path when the application is run in the real world.

Class.forName("oracle.jdbc.OracleDriver"); generates exception: java.lang.ClassNotFoundException

I'm trying to make a program that connects to an Oracle database for the ultimate purpose of creating a few tables and running commands on them for a course I am taking. I'm currently trying to make the example given in class work but I can't get that to work. The code that generates the error "java.lang.ClassNotFoundException" is when my main hits the code:
Class.forName("oracle.jdbc.OracleDriver");
or
Class.forName("oracle.jdbc.driver.OracleDriver");
I have manually added the ojdbc6.jar, ojdbc8.jar and ojdbc14.jar files to each folder in my "PATH" system variable and I'm getting the exception:
java.lang.ClassNotFoundException: oracle.jdbc.OracleDriver
or
java.lang.ClassNotFoundException: oracle.jdbc.driver.OracleDriver
I have no idea how to fix this or what to modify. It is a course example after all...
Any/All suggestions are welcome.
Thank you for your time
I hope this might help?
I don't think you need Class.forName with latest version.
I would have done something like below and set connection,url,user and password as global final variables to avoid repeating yourself and call the method where you need it.
public Connection getConnection() throws SQLException {
return connection = DriverManager.getConnection(url, user, password);
}

Accessing MySQL through node-java

So, I have a node.js project that I am working on for a class. One of the features our group is trying to build, requires calling a Java class method that connects to a MySQL database, does some computation and returns the result to the node server. Here is what we have so far:
Using the node-java library, we are adding the required jars to classpath:
var java = require('java');
var path = require('path');
java.classpath.push(path.join(__dirname, "../java/mysql-connector-java.5.1.38-bin.jar"));
java.classpath.push(path.join(__dirname, "../java/BayesianCurve.jar"));
java.classpath.push(path.join(__dirname, "../java/commons-math3-3.6.jar"));
Then, the class is instantiated and a method is called:
var test = java.newInstanceSync('BayesianCurve');
test.getPrediction(param, function(err, data){
if(err)
{
console.log(err);
return;
}
else
res.send(JSON.stringify(data));
});
So far, so good. The Java class method then has to create a connection to a MySQL database and this is where the problems start:
Class.forName("com.mysql.jdbc.Driver");
//System.setProperty("jdbc.drivers","com.mysql.jdbc.Driver");
//DriverManager.registerDriver(new com.mysql.jdbc.Driver());
System.out.println("Here");
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/schema","root","root");
The java code, standalone, is able to connect to the database and execute queries. However, when used through the node-java module, it throws:
ClassNotFoundException: com.mysql.jdbc.Driver
One reason we thought about was that this library is linked dynamically. Somehow, the java environment in node.js is not able to figure this out. However, we are not really sure how to resolve this. Any help in this regard is appreciated.

JDBC connection hangs at executing rs.next()

I'm trying to read from a mySQL server (in my LAN) a huge resultSet. I implemented it the way I found looking for the setFetchSize method: BalusC's Example. So I did:
con = DriverManager.getConnection(url, user, password);
//con.setAutoCommit(false);
st = con.createStatement(ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY);
st.setFetchSize(Integer.MIN_VALUE);
rs = st.executeQuery(query);
int count = 0;
while (rs.next ()) {
// Process entry
String rsc = rs.getString ("resource");
String tpc = rs.getString("topic");
System.out.println(count + ": " + rsc);
++count;
}
Although, it hangs at row 1077 at the line rs.next(). It doesn't throw exception.
EDIT:
After a timeout something new happened. It returned this error
message:
JDWP exit error JVMTI_ERROR_NONE(0): getting frame location [../../../src/share/back/stepControl.c:641]
This error seems to be unrelated.
EDIT2: I've coded a PHP script for retrieving results by stream and happens exactly the same. So it has nothing to do with JDBC... I don't know what is going on!!
The second error message is a deadlock in the Eclipse debugger when hot-swapping and/or JRebel (source):
It is a known problem, actually two problems.
1) Eclipse debugger deadlocks sometimes on hot-swapping (true for any version of JRebel).
2) JVM crash on hotswapping - it is true for JRebel 4M1.
We are expecting bugfix release soon (3.6.2 together with Eclipse plugin) - It will fix first problem. Second problem should be fixed with 4M2 release.
Suggestions:
Update Eclipse to 3.7.2 and JRebel to the latest release
Start the application again.
Use logging/System.out.println() to debug
Check the log of your mysql server for problems (harddisk, network)
Run the query in a SQL workbench (to see whether this is a problem in the query/the server or your code)
OKay, the problem was at the query itself.
I've found that streaming is very sensitive on how query is built. If I make a little change (like adding SELECT DISTINCT) it buffers or hangs. Finally, I left a column out of the query and worked...

h2 (embedded mode ) database files problem

There is a h2-database file in my src directory (Java, Eclipse): h2test.db
The problem:
starting the h2.jar from the command line (and thus the h2 browser interface on port 8082), I have created 2 tables, 'test1' and 'test2' in h2test.db and I have put some data in them;
when trying to access them from java code (JDBC), it throws me "table not found exception". A "show tables" from the java code shows a resultset with 0 rows.
Also, when creating a new table ('newtest') from the java code (CREATE TABLE ... etc), I cannot see it when starting the h2.jar browser interface afterwards; just the other two tables ('test1' and 'test2') are shown (but then the newly created table 'newtest' is accessible from the java code).
I'm inexperienced with embedded databases; I believe I'm doing something fundamentally wrong here. My assumption is, that I'm accessing the same file - once from the java app, and once from the h2 console-browser interface. I cannot seem to understand it, what am I doing wrong here?
EDIT: as requested, adding some code:
Java code:
Class.forName("org.h2.Driver");
String url = "jdbc:h2:" + "db/h2test.db";
String user = "aeter";
String password = "aeter";
Connection conn = DriverManager.getConnection(url, user, password);
PreparedStatement ps2 = conn.prepareStatement("Show tables;");
ResultSet rs = ps2.executeQuery();
This resultset has 0 rows (no tables), instead of showing me the 2 tables.
H2 Console-browser interface settings:
Settings: Generic h2(embedded)
driver class: org.h2.Driver
JDBC URL: jdbc:h2:../../workspace/project_name/src/db/h2test.db
user name: aeter
password: aeter
EDIT2: I copied the database to a new folder. Now the db file in the new folder is shown with the 'newtest' table (from the java code) and with the 'test1' and 'test2' tables (from the console-browser h2 interface) - exactly the same way the older db file was shown. So the problem persists with the copy of the db file.
For embedded mode, you'll need to check the path. For example, use a path relative to your home directory:
"jdbc:h2:file:~/db/h2test.db"
To be sure, use a full path:
"jdbc:h2:file:/users/aeter/db/h2test.db"
For convenience, append ;IFEXISTS=TRUE to avoid creating spurious database files.
See Connecting to a Database using JDBC for more.
H2 Server URLs are relative to the -baseDir specified as a parameter to main().
Also there can be a problem if you use some special parameters in your JDBC url, the database file name can differ for various cases.
In my case, I had two URLs:
jdbc:h2:~/XXX;MVCC=FALSE;MV_STORE=FALSE
jdbc:h2:~/XXX
This first case created XXX.h2.db file, the second one XXX.mv.db, beware.
Also you can like this
"jdbc:h2:file:db/h2test.db"
then java looks db folder from project folder
->projectName // project folder
-->src // src folder
-->db // here your database folder
-->....
If you are using Hibernate try this in hibernate.cfg.xml file:
<property name="connection.url">jdbc:h2:file:db/h2test</property>
without *.db extension at the end

Categories