Connect Derby to java - java

I'm trying to connect to a database using Derby in Java (using NetBeans), bIt i keep getting this error when I try to create a new database:
An error occurred while crating the database java.lang.ClassNotFoundException; org.apache.derby.jdbc.ClientDriver.
I've tried using Derby 10.16.1.1 and Derby 10.14.2.1.

The error message you are seeing suggests that the class org.apache.derby.jdbc.ClientDriver is not being found by the class loader at runtime. This class is necessary for connecting to a Derby database.
There are few possible causes:
The Derby jdbc driver is not in the class path of your project or application.
The version of jdbc driver jar file isn’t correct version for the version of Derby you are using. Please, check versions.
There is a comprehensive derby documentation where you can find more info and step-by-step guide.
In order to add org.apache.derby.jdbc.ClientDriver to your class path you can do few things, depending on your development environment:
If you're using an IDE like Netbeans, you can add the JAR file to the project's build path by IDE UI, for example, that thread explains how to do so.
If you're building your project with a build tool like Maven or Gradle, you can add the JAR file as a dependency in your project's pom.xml file or build.gradle file. You can read more about that approach here and here
If you're running your project from the command line, you can add the jar file to the classpath by specifying it as a command-line argument when running your project's main class. For example, if the jar file is located in /lib/derby-jdbc.jar, the command would be:
java -cp /lib/derby-jdbc.jar YourMainClass

Related

No suitable driver found when exporting to a JAR-file?

I am trying to get my project into a JAR so I can run it as a CLI.
I have two JDBC connectors that I am using, one for MySQL one for PostgreSQL. Both are located in the same directory and work fine if I run them in the IDE.
When I create the JAR the MySQL connector still works fine, however when trying to establish a connection to PostgreSQL the following error appears.
What really irritates me is that the connector seems to be included in the build of the jar.
Both the MySQL connector and PostgreSQL connector are listed in the build.
How can I go about fixing this?
The problem is that all JDBC-4-compliant JDBC drivers contain a file /META-INF/services/java.sql.Driver that lists the java.sql.Driver implementations in the JAR files. This is used by the java.sql.DriverManager to load the available JDBC drivers.
The process you used for merging apparently doesn't merge the different files from the drivers into a single file, so it only has the content of one of the drivers. As a result, the other driver isn't loaded automatically.
Possible solutions:
Don't merge JAR files into a single JAR, but instead use the Class-Path attribute of META-INF/MANIFEST.MF to specify the JARs you use, and execute your program with java -jar your.jar
Make sure META-INF/services/java.sql.Driver is merged correctly (or maybe provide your own), depending on how you merge, there might be an option to configure which files need to be merged
Explicitly load the drivers using Class.forName("com.mysql.cj.jdbc.Driver") and Class.forName("org.postgresql.Driver") (do it for both to prevent problems if order of merging files changes, and the other file wins)

SQLServerException: This driver is not configured for integrated authentication

I am building a GWT application that queries SQL Server. While working in Eclipse everything is fine and dandy, but when I try to deploy a .war file and stick it into a JBoss deployments directory, I get this error:
Failed to load the sqljdbc_auth.dll cause : Native Library
JBossServerPath\bin\sqljdbc_auth.dll already loaded in another
classloader com.microsoft.sqlserver.jdbc.SQLServerException: This
driver is not configured for integrated authentication.
ClientConnectionId:450886fa-8bde-4f52-b213-7af1f4948913
If I remove the sqljdbc4.jar from my project directory and have the sqljdbc_auth.dll in the JBossPath\bin directory as suggested here, I get the following error.
java.sql.SQLException: No suitable driver found for
jdbc:sqlserver://localhost:1433;databaseName=DataBase;integratedSecurity=true;
Also, I can't find the lib directory referenced in the above answer.
I have the sqljdbc4.jar in my project's WEB-INF\lib directory and I'm building the project with ant.
I tried asking this question earlier, but it got marked as a duplicate of this question, which did not solve my problem.
The issue was I didn't have the line Dependencies: com.microsoft.sqlserver in my Manifest.MF file.

Deploy web application on Tomcat

I created a simple web application in eclipse which connect to sql server. I downloaded the sql driver and added it to build path like this:
Build Path->Configure Build Path->Add External JARs.
When I run my application on Tomcat I get exception: ClassNotFoundException. I think that I know what it reason is (the folder WEB-INF->lib of my application is empty). When I put jar file of driver in WEB-INF->lib - everything is good. How I can add jar file(sql driver) to folder WEB-INF->lib of my application using eclipse?
Putting the JDBC driver JAR in [Tomcat-install-root]/lib will solve your problem, and you can do that outside of Eclipse.

MySQL JDBC connector not found in jar file

I have been working on project and when i made the jar file and executed it, everything works fine but when the sql connector is called,It gives an error saying mysql driver not found.
I have included mysql connector jar file in the project build path. MySQL function works fine when executed in Eclipse.
Please Make sure that the Jar file and the Lib directory Containing the Connector Jar are in the same location like this
Myjarfile.jar
libdirectory
Myconnectorjar.jar

Eclipse: Exported library fail to instantiate oracle driver ojdbc.jar

I have a library that contains functionality to connect to on oracle database. When I export this library (as a JAR) and use it in the main project, it gives an exception when loading the driver with class.forname. It obviously cannot find the ojdbc driver. I included this driver in the build path and as exported library.
I tested and used the driver directly from the main project, and it work, it connects to oracle db.
Thanks.
The problem is that your driver is a jar file, and when you export the app as a jar file, that driver will be a jar file in a jar file. For that scenario you either need a special classloader or put the driver jar file in the classpath of your main program.
Explore your exportedjar by using WinRar and check if it contains a jar under jar.

Categories