H2 DB driver not in classpath - java

I'm trying to connect to my embedded H2 database via Java. I found various threads and tutorials on this and now have this code:
Connection con = null;
Properties connectionProps = new Properties();
connectionProps.put("user", "username");
connectionProps.put("password", "password");
try {
Class.forName("org.h2.Driver");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
con = DriverManager.getConnection("jdbc:h2:~/test", connectionProps);
I got the "no suitable driver found for jdbc:h2:~/test" error message. I found the Class.forName(...) in some threads as a solution to this, but it doesn't seem to be working (ClassNotFoundException).
I read that the driver is probably not in my classpath, but don't really know what I need to do with that information. In the database view of IntelliJ the driver seems to work just fine (I can click reload drivers and it confirms the h2 driver). What am I doing wrong?

There are a couple ways to add to the classpath. I recommend trying to understand this page https://howtodoinjava.com/java/basics/java-classpath/ for more information on different strategies that have similar, but different results.
Probably the simplest way for just this project, would be to edit the "run configuration" that you are using, and add a new VM option:
–classpath /path/to/local/h2/driver.jar
Use an absolute path to the jar, and this will allow it to be present in the classpath when running it this way. In the link I shared, this is the same as adding the command line argument to the java or javac command, so this argument will work outside of intellij on the command line as well.
Edit:
Based on the comments, it seems like you might be using the %PROGRAMFILES% environment variable, with a fully qualified path.
Try this instead:
-classpath %PROGRAMFILES%\H2\bin\h2-1.4.200.jar
If you were to run the following command in your command prompt
echo %PROGRAMFILES%
I suspect you'd get the response:
C:\Program Files
Which is not the x86 version. So. Either use the environment variable and omit the part of the path it's value represents (eliminating the C:\ part)
or, if that's not the correct Program Files folder at all, then avoid it and try (note the quotes which are required because a folder has spaces in it's name):
--classpath "C:\Program Files (x86)\H2\bin\h2-1.4.200.jar"

Related

java.sql.SQLException: Problems with loading native library/missing methods: no ttJdbc181 in java.library.path

I am running TimesTen facing application in local (in eclipse IDE). URL and username and password seems to be fine. But I am getting below exception.
java.sql.SQLException: Problems with loading native library/missing methods: no ttJdbc181 in java.library.path
at com.timesten.jdbc.JdbcOdbcConnection.connect(JdbcOdbcConnection.java:2012)
at com.timesten.jdbc.TimesTenDriver.connect(TimesTenDriver.java:296)
at com.timesten.jdbc.TimesTenDriver.connect(TimesTenDriver.java:152)
at org.apache.tomcat.jdbc.pool.PooledConnection.connectUsingDriver(PooledConnection.java:319)
at org.apache.tomcat.jdbc.pool.PooledConnection.connect(PooledConnection.java:212)
at org.apache.tomcat.jdbc.pool.ConnectionPool.createConnection(ConnectionPool.java:736)
at org.apache.tomcat.jdbc.pool.ConnectionPool.borrowConnection(ConnectionPool.java:668)
at org.apache.tomcat.jdbc.pool.ConnectionPool.init(ConnectionPool.java:483)
at org.apache.tomcat.jdbc.pool.ConnectionPool.<init>(ConnectionPool.java:154)
at org.apache.tomcat.jdbc.pool.DataSourceProxy.pCreatePool(DataSourceProxy.java:118)
at org.apache.tomcat.jdbc.pool.DataSourceProxy.createPool(DataSourceProxy.java:107)
at org.apache.tomcat.jdbc.pool.DataSourceProxy.getPool(DataSourceProxy.java:214)
I checked the bin folder of TimsTen installation directory did not see ttJdbc181.dll file Instead I found ttJdbcCS.181 in the folder. I don't know why code is looking for ttjdbc181.dll file. I have tried adding ttjdbc8,9,10,11 to class path still same issue. Any help to resolve this issue will be appreciated.
Had same stack trace.
All I had to do is to add "jdbc:timesten:client:" to my connection string. It is driver and protocol as far as my understanding goes.
My connection string looks like follows:
jdbc:timesten:client:TTC_Server=127.0.0.1;TTC_Server_DSN=myDsn;UID=user;PWD=userpasswd;TCP_PORT=9999;
This was issue on Windows. Assume same would arise for Linux.
Also please ensure you have correct CLASSPATH, Lib, Include, Path environment variables.
Windows installer usually takes care of this. On Linux you have to run ttenv.sh script from directory of your installed instance, this will set env vars as required.

Python 3.7 connecting to HSQLDB on MAC

Trying to connect to a HSQLDB Java database using python 3.7, jaydebeapi, and jpype. I tried the following
import jaydebeapi
UserName = "SA"
Password = ""
Java_Class = "org.hsqldb.jdbcDriver"
HSQL_Driver_Path = "/Hsqldb/driver/hsqldb.jar"
Database = "jdbc:hsqldb:/Hsqldb/database/OneDatabase"
jaydebeapi.connect(Java_Class,Database,[UserName,Password],jars=HSQL_Driver_Path)
and it resulted in the following error
java.lang.RuntimeExceptionPyRaisable: java.lang.RuntimeException: Class org.hsqldb.jdbcDriver not found
google states that this is a classpath error and I need to add a class path to fix.
edit: Details of setup: Mac, anaconda, python 3.7
I tried something similar in R using the RJDBC library and was able to connect just fine.
PathDriver = "/Hsqldb/driver/hsqldb.jar"
JDBCDriver = "org.hsqldb.jdbcDriver"
drv <- JDBC(JDBCDriver,PathDriver)
# Connect to Database
DatabaseP <- "jdbc:hsqldb:file:////Hsqldb/database/OneDatabase"
Con <- dbConnect(drv,DatabaseP,"SA","")
edit: Details of setup: Mac, R, Rstudio
I separated R from Anaconda as Anaconda was blocking many of the libraries I wanted to use.
This is probably apples to oranges, but why does python need a classpath set when R performs the function just fine? and how does one set a class path for HSQLDB for python?
When starting JPype it either takes the classpath from the environment variable CLASS_PATH or from a manually specified classpath defined using the jpype.startJVM(classpath="...") command. This command is likely part of the jaydebeapi api if you don't have to call it directly from JPype.
I assume that R defines its own classpath internally so there may just be differences in the what is being seen from the JVM. This could include a different JVM is being selected, the classpath including different locations, or in some cases if the driver has a native portion such as MS SQLServer the library path may also not be correct. In order for the JDBC driver to load all piece need to be found including the jar, support jars and native shared libraries. If all else fails manually try to load the java class using Class.forname. It may provide you with further diagnostics as to which piece is missing.

Derby db connectivity problems

I receive the following error msg when attempting to connect to a derby network server:
java.sql.SQLException: No suitable driver found for jdbc:derby://localhost/studentdb;create=true
Derby is properly installed and all environment variables set. I am able to start the derby NetworkServerControl from a Windows command prompt with the following command:
java org.apache.derby.drda.NetworkServerControl start -h localhost
,and I can do this from any location within my system's directory tree.
I can start the derby ij client from within a Windows command prompt with the command:
java org.apache.derby.tools.ij
,again, from any location within my system's directory tree.
But the code snippet below is unable to make this connection:
public static void main(String[] args) {
Connection conn = null;
String url = "jdbc:derby://localhost/studentdb;create=true";
//the error happens here, the program executes no further
conn = DriverManager.getConnection(url,null);
Statement stmt = conn.createStatement();
}
Placing the port value in the url string makes no difference.
Any suggestions would be much appreciated.
You must add the derby jdbc driver to your classpath (from derbyclient.jar, since this is the ClientDriver), then use this instruction to load the driver :
Class.forName("org.apache.derby.jdbc.ClientDriver");
So I encountered this error and it was quite an irritating and hectic task to resolve this. But in the end, I managed to find a perfect video that made me install derby from the start and guided me perfectly on how to install it. However, there is one more step after the video.
Watch this video if you have set up JavaFX packages and are able to run the program normally, but facing
"java.sql.SQLException: No suitable driver found for jdbc:derby://localhost:1527/DBNAME;create=true" issue when trying to run with the database.
Link to the tutorial -> https://www.youtube.com/watch?v=OKiBsWbgrMw
Now after this is set up you will now be able to start/stop the database (via the services tab) and will be able to connect with the DB. But the issue will still persist in trying to edit the DB.
To rectify this, follow the steps ->
Right click on project ---> Properties ---> Libraries ---> Click on '+' in Classpath ---> Add jar/folder ---> Go to the lib folder inside the derby and select derbyclient.jar
VERSIONS
JAVA - 17.0.1, Netbeans - 12.6

No suitable driver when trying to create SQL database [duplicate]

This question already has answers here:
SQLException: No suitable driver found for jdbc:derby://localhost:1527
(19 answers)
Closed 7 years ago.
I am trying to create a new SQL database with this Java program
import java.sql.*; //Needed for JDBC classes
public class BuildPhonebookDB {
public static void main(String[] args) throws Exception{
//Create a named constant for the URL
final String DB_URL = "jdbc:derby:Phonebook;create=true";
try {
//Create a connection to the database.
Connection conn = DriverManager.getConnection(DB_URL);
//Create a Statement object.
Statement stmt = conn.createStatement();
//Create the Entries table
stmt.execute("CREATE TABLE Entries (" +
"Name CHAR(20)"+
"Number INTEGER)"
);
System.out.println("Database Connected");
//Close the connection
conn.close();
}
catch(Exception ex) {
System.out.println(ex.getMessage());
}
}
}
When I try to run the program I get an error that:
No suitable driver found for jdbc:derby:Phonebook;create=true
I have looked at various other similar posts on Stack Overflow, such as this one, but none help. I have seen things about a driver jar, but I don't know what this is, if I need to edit this, could someone help me through it?
Thanks for any help
Did you see this guide and have you complited all step of this guide?
Apache Derby
Download Derby Download the binary Apache Derby distribution from the
Derby web site at http://db.apache.org/derby/derby_downloads.html.
These tutorial instructions use version 10.12.1.1 and assume you
downloaded one of the binary distribution files listed in the table
below:
Operating System Download File Windows db-derby-10.12.1.1-bin.zip
UNIX, Linux, and Mac db-derby-10.12.1.1-bin.tar.gz If a more recent
release is available, download that, then substitute that version
number for 10.12.1.1 in the following instructions.
Install Derby Choose the directory into which you want to install the
Derby software. You must have write permissions to this directory. The
sample instructions below use C:\Apache for Windows and /opt/Apache
for UNIX; be sure to use your actual location. Copy the software
distribution to the location you choose, then extract it as shown
below.
Windows (use your extraction tool e.g. WinZip -- these instructions
use mks unzip):
mkdir C:\Apache copy db-derby-10.12.1.1-bin.zip
> C:\Apache cd C:\Apache unzip db-derby-10.12.1.1-bin.zip
UNIX:
mkdir /opt/Apache cp db-derby-10.12.1.1-bin.tar.gz /opt/Apache
> cd /opt/Apache tar xzvf db-derby-10.12.1.1-bin.tar.gz
In both cases, the software will now be extracted into a subdirectory
named db-derby-10.12.1.1-bin.
Set DERBY_INSTALL Set the DERBY_INSTALL variable to the location where
you installed Derby. Examples are shown below, but be sure to use the
actual location on your system:
Windows: C:\> set DERBY_INSTALL=C:\Apache\db-derby-10.12.1.1-bin
UNIX Korn Shell:
$ export
> DERBY_INSTALL=/opt/Apache/db-derby-10.12.1.1-bin
Configure Embedded Derby To use Derby in its embedded mode set your
CLASSPATH to include the jar files listed below:
derby.jar: contains the Derby engine and the Derby Embedded JDBC
driver derbytools.jar: optional, provides the ij tool that is used by
a couple of sections in this tutorial You can set your CLASSPATH
explicitly with the command shown below:
Windows:
C:\> set
> CLASSPATH=%DERBY_INSTALL%\lib\derby.jar;%DERBY_INSTALL%\lib\derbytools.jar
;.
UNIX:
$ export
> CLASSPATH=$DERBY_INSTALL/lib/derby.jar:$DERBY_INSTALL/lib/derbytools.jar:.
...
Step 3: Embedded Derby
When an application accesses a Derby database using the Embedded Derby
JDBC driver, the Derby engine does not run in a separate process, and
there are no separate database processes to start up and shut down.
Instead, the Derby database engine runs inside the same Java Virtual
Machine (JVM) as the application. So, Derby becomes part of the
application just like any other jar file that the application uses.
Figure 1 depicts this embedded architecture.
Set the environment
To set up the environment, follow the "Configure Embedded Derby"
instructions.
Use this before you get the connection from the driver:
Class.forName("org.apache.derby.jdbc.ClientDriver").newInstance();

Error while compiling java file with GCC

I wrote a small java program in Netbeans. It compiles and runs perfectly. But I also need to compile it in javac in Linux because this homework is tested there. Whenever I attempt, I get the following compile error message. Do you have any idea about this message?
/usr/lib64/gcc/x86_64-suse-linux/4.6/../../../../lib64/crt1.o: In function `_start':
/home/abuild/rpmbuild/BUILD/glibc-2.14.1/csu/../sysdeps/x86_64/elf/start.S:109: undefined reference to `main'
I just write the following line for import a library
import java.sql.*;
I am just using println except sql operations. The beginning of my code is below:
Connection conn = null;
try{
String username = ".....";
String password = "....";
String url = "jdbc:mysql://localhost:3306/.....";
Class.forName("com.mysql.jdbc.Driver").newInstance();
conn = DriverManager.getConnection(url, username, password);
System.out.println("Database connection extablished.");
}
catch(Exception e){
System.out.println("Cannot connect to database server");
}
After this part of code, nothing special, just ordinary lines.
It seems that error may be because you're not defining a main method, and the compiler therefore can't find it.
However, I have to ask why you're using GCC? Normal JDK is available on Linux and should be your preferred choice unless you have a very good reason otherwise! If Netbeans isn't compiling your application on Linux then it's probably because you haven't set something up properly or installed the JDK - you can (and should) use the JDK rather than GCJ, which is now largely unmaintained.
You can either grab it through your package manager or download it separately here.
You don't need to (re-)compile it for linux. It's Java. The generated class file (from windows) will run on Linux and Windows, you just need a JRE or JDK on the target host.
Compile with javac, not GCC (or GCJ if you really want something "GCCish").

Categories

Resources