Sqlite JDBC in Java Server Page - java

I am trying to use Sqlite on a web page over Java.
I am getting the following error:
java.lang.NoClassDefFoundError: org/sqlite/JDBC
model.DataBase.<init>(DataBase.java:35)
I've tried the two alternatives:
this.conn = new org.sqlite.JDBC().connect("jdbc:sqlite:" + file, new Properties());
this.stm = this.conn.createStatement();
and
Class.forName("org.sqlite.JDBC");
this.conn = DriverManager.getConnection("jdbc:sqlite:" + file);
this.stm = this.conn.createStatement();
I am using Eclipse and I've configured the jar file in the build path, also, when running as a desktop application it works fine.

Just having the sqllite.jar in your build path is not enough. If you are running it as a web application place the jar in the /WEB-INF/lib folder as well.
You can tell eclipse to export that file as well.

I know you said that you've included the jar file in your build path, but if it's not also in the $CLASSPATH of the server you're running under, then you'll end up with this exception. If the Java Classloader can't find the class inside a jar file that it knows about, you'll get NoClassDefFoundError. I suggest checking the classpath.

Related

access folder in spring boot in classpath

I am trying to get access to a folder that is created in the classpath in a Spring boot application. A snippet of the code is below:
ClassLoader classLoader = ClassUtils.getDefaultClassLoader();
URL url = classLoader.getResource("converters");
LOGGER.debug("**************** url: " + url);
File file = new File(url.toURI());
Path path = Paths.get(file.getAbsolutePath());
Arrays.stream(path.toFile().listFiles()).forEach(f -> LOGGER.debug("*******files: " + f.getName()));
if (!path.toFile().isDirectory()) {
throw new IllegalArgumentException(ErrorCode.INVALID_MAPPERS_DIRECTORY.formatMessage(path));
}
The code above runs without any issues when I run it in Intellij and I get the url as below:
file:/C:/Users/user1/projects/my-service/test/build/resources/main/converters
When I run it on Linux inside the application rpm, I get the url value below:
jar:file:/opt/home/libexec/my-service-2.0.0-SNAPSHOT.jar!/BOOT-INF/lib/my-service-api-2.0.0-SNAPSHOT.jar!/converters
Any reason why is the different behavior?
The difference is the packaging.
Your IDE does not package your application to run it, it just uses the file system as this is faster.
When you package your app and deploy all the resources that your ide can access from the file system are now packaged within your spring boot fat jar file. In your case the file is inside the my-service-api-2.0.0-SNAPSHOT.jar which is packaged inside your fat jar.
Problem
I need to be able to have a reference to a folder that a user may create in the classpath of my Spring Boot application.
Solution
The following worked for me:
mapperFilesFolder = resolver.getResources("classpath*:" + mappersLocation + "/.");
Path path = Paths.get(mappersFolder[0].getURI());

ClassNotFoundException: con.mysql.jdbc.Driver

Super new to JSP and just trying to access mysql
public static Connection connect(){
try{
Class.forName("con.mysql.jdbc.Driver").newInstance();
return DriverManager.getConnection("jdbc:mysql://localhost/db_name", "root", "******");
}catch(Exception e){
throw new Error(e);
}
I continue to get this error: ClassNotFoundException: con.mysql.jdbc.Driver
I understand that I need to change the "classpath" and/or that I need to move the mysql-connector jar file to tomcat7/lib but I have 2 problems:
I have absolutely no idea what the classpath is and how to change it using ubuntu and dreamweaver.
my tomcat7 folder doesn't have a lib directory. I don't know where to put the jar file.
Assuming that you have the jar on your classpath, I think that yuor problem is a misspelt fully qualified classname: con.mysql.jdbc.Driver should be com.mysql.jdbc.Driver
This seems to be a J2EE application. Within the project should be WEB-INF directory. Within that directory, create (if necessary) a lib directory and place the mysql-connector.jar file within lib. This will cause the driver to be placed on the runtime classpath.
Also as Romski pointed out change the class name to com.mysql.jdbc.Driver, however I'm not even sure this code is necessary with newer version of the driver and JDBC. If your using Tomcat7 the JDK is most likely recent enough to not require this code. See: https://stackoverflow.com/a/12933246/714969

Can't connect to Derby database if derby.jar is in Eclipse workspace

I've been trying to create a Derby DB via Eclipse Data Tools Platform and at the beginning I couldn't manage to get it to work.. no matter what I did, I always got the same error:
Could not connect to New Derby.
Error creating SQL Model Connection connection to New Derby. (Error: null)
java.lang.NullPointerException
Error creating JDBC Connection connection to New Derby. (Error: null)
java.lang.NullPointerException
While I was trying this the derby.jar file containing the driver was inside "workspace/myproject/lib" folder. Then I put the same exact file to my Desktop, set the driver to point to the new location of my derby.jar and all started to work automagically...
I did some experiments and it happens that it works everywhere EXCEPT when I put inside my Eclipse workspace folder. Why?
And most important: if I want to deploy my application on different machines do I need the derby.jar inside my project folder (and thus inside the .jar of my project)?
Thanks in advance to everyone
EDIT: I already added derby.jar to the project through Eclipse interface, here's how:
You have to add the jar file using eclipse's interface. This website shows how:
http://www.cs.duke.edu/courses/cps004g/fall05/assign/final/addlibrary.html

Java Applet on an Xampp Server - No suitable driver found on jdbc:mysql://localhost:3306/

I'd like to preface this question by saying that I'm really new at server setup and programming.
I made an applet that views data from tables in a MySQL database and lets just say it outputs it. It's a lot more complicated than that but the point is that the applet runs perfectly in Eclipse when I run it. I had to set the MySQL-connector-java-5.1.15-bin.jar file as an external library for it, but after doing that it runs perfectly.
The problem is whenever I put the code on my Xampp server, it doesn't work right. The applet start and everything works until the applet tries to connect to the MySQL database (Xampp) using the following code:
String connectionURL = "jdbc:mysql://127.0.0.1/";
Connection connection = null;
try{
connection = DriverManager.getConnection(connectionURL, "root", "mypassword");
}catch(Exception e){
System.out.println("Error: "+e);
}
In the applet I didn't use System.out.println(), but I did manage to find out the I was getting was:
java.SQL.Exception: No suitable driver found on jdbc:mysql://127.0.0.1/
I'm wondering what I need to do to get around this error. Where do I need to put the .jar connector or how do I need to configure it with my Xampp server to get this to work?
To build a webapp, you would compile your code and build a WAR (Web Application Archive), which the artifact that would be deployed to your server, unpacked, and be put in your webapps folder. WEB-INF is the source folder where your web content goes in your project.
In eclipse, you should have this folder src/main/WEB-INF, where you could place CSS (Cascading Style Sheets), JavaScript files, images, JSPs, etc.
The fact that you have added the MySQL jar as an external dependency means that it would not be added to the WAR that you would be deploying.
The short answer to your question is that you need that MySQL jar to be on the classpath of your webapp, which you would do by placing it in (e.g.) tomcat/webapps/<your project name>/WEB-INF/lib, then restart tomcat (that is, if you use tomcat).
This will be a bit of a learning curve at first, but I would suggest you learn how to use Maven. Maven will allow you to consume dependencies, and build a proper WAR (pulling in these dependencies).
Guide to building a webapp with Maven.
I fixed it!
All I had to do was this:
<applet code="file.class" archive ="mysql-connector-java-5.1.14-bin.jar" width="500" height="500">
</applet>
in the html file that I wanted to display the applet in.

database connectivity?

How to establish SQL 2005 database connectivity with Java application. Actually I don't know how to set path for JDBC. Any help would be great.
Download the jdbc driver from http://www.microsoft.com/sqlserver/2005/en/us/java-database-connectivity.aspx.
You can either download the Windows or Unix version. It does not really matter which one you use.
If you chosed the Windows version, run the downloaded exe file; this will create a directory called Microsoft SQL Server 2005 JDBC Driver in the directory you downloaded the file to.
Add the file Microsoft SQL Server 2005 JDBC Driver\sqljdbc_1.2\enu\sqljdbc.jar to your classpath (either using the -cp option of java or, if you are using an application server, by putting it in the appropriate directory).
Set your connection. Basically, the information required for this are:
the driver class name: com.microsoft.sqlserver.jdbc.SQLServerDriver
the connection url: jdbc:sqlserver://[serverName[\instanceName][:portNumber]][;property=value[;property=value]]
Check Connecting to SQL Server with the JDBC Driver for more details and/or the resources available in the help directory (sample code available in help/samples/connections/ConnectURL.java).
This is a HOWTO from Microsoft on where to get the driver and how to configure it.
Briefly, download the .jar from Microsoft, reference it in your classpath and use:
String connectionUrl = "jdbc:sqlserver://localhost:1433;" +
"databaseName=AdventureWorks;user=MyUserName;password=*****;";
Connection con = DriverManager.getConnection(connectionUrl);
substituting the relevant info.
This link will help you with this
jdbc connection mssql
Code Snippet
private java.sql.Connection getConnection(){
try{
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
con = java.sql.DriverManager.getConnection(getConnectionUrl(),userName,password);
if(con!=null) System.out.println("Connection Successful!");
}catch(Exception e){
e.printStackTrace();
System.out.println("Error Trace in getConnection() : " + e.getMessage());
}
return con;
}
private String getConnectionUrl(){
return url+serverName+":"+portNumber+";databaseName="+databaseName+";selectMethod="+selectMethod+";";
}
Your actual problem is thus that you don't know what to do with the phenomenon "classpath".
Actually, the classpath is kind of a collection of disk file system paths which points to the whole .jar file(s) and/or to some root folder with .class file(s) where the Java Virtual Machine should lookup for classes to be imported and loaded.
You can specify the classpath during compile and runtime using the -classpath or -cp argument of javac.exe and java.exe. The -cp is just a shorthand, it does nothing different. Then you have the mysterious %CLASSPATH% environment variable which you should just entirely forget. It is a poor thing which was intented to make starters easy to manage the classpath, but at end it just confused them more.
As you're using Class#forName() to load the driver, you only need to have it in the classpath during runtime, not during compiletime. So here's a basic example how to execute it:
java -cp .;c:/path/to/mssql-jdbc-driver.jar com.example.YourClass
You see, the classpath exist of two parts, the . which represents the current working directory and the c:/path/to/mssql-jdbc-driver.jar which should be the absolute path to the JAR file. The ; is just a path separator (in Windows; in Unix and clones it should be a colon :). Note: if a path contains spaces, e.g. c:/spacy path to/file.jar, then you need to wrap the individual path with doublequotes.
If you're using an IDE such as Eclipse, then normal practice is that you create a folder in the project where in you can drop all of those 3rd party JAR files which are required by the project. Create a project folder called lib, drop the JDBC driver in there and rightclick project > Properties > Java Build Path > Libraries > Add JARs > Select the JAR file which you dropped in project's lib > OK.
That should be it. Hope this helps.

Categories