Sqlite database ..java.lang.ClassNotFoundException:org.sqlite.jdbc - java

Hello guys I have create a small java application for desktop that simple logs data into an sqllite database. The jar files work well on the computer that was built, but, when I distributed on other pc it display the message that goes like this java.lang.ClassNotFoundException:org.sqlite.jdbc
I used this code: I know I have to change the classpath apparently, but I am not sure how to proceed so that this program work on other pcs. I notice that this programs works well on the pc that it was built on but it is because i used this classpath..."jdbc:sqlite:C:\Users\USUARIO\Documents\workspace\School2015.sqlite"... which is my local computer.
My question is how do I change this classpath so that the program runs fine on other pc than the one it was built on?
public class sqlConnection {
Connection conn=null;
public static Connection dbConnector()
{
try{
Class.forName("org.sqlite.JDBC");
Connection conn=DriverManager.getConnection("jdbc:sqlite:C:\\Users\\USUARIO\\Documents\\workspace\\School2015.sqlite");
JOptionPane.showMessageDialog(null, "BIENVENIDO! Estás Conectado");
return conn;
}catch (Exception e)
{
JOptionPane.showMessageDialog(null, e);
return null;
}
}
}

Not sure how you package and/or run this but if you place the School2015.sqlite file inside of the built JAR file you are creating and reference it from the relative path instead of the absolute one you will have no problems sharing the app.
You could look into using Maven as a build tool that will allow you to package everything up.

Related

JAVA Runnable .JAR File incomplete execution. Eclipse IDE Runs to completion

Running into an issue with a JAVA application that I'm hoping someone can provide some clarity to. I have a simple Java Application which is being built in the Eclipse IDE. I have JRE 1.8.0_251 installed on PATH.
My application at a basic level performs the following:
Load a properties file
Connect to a MSSQL Database based on that SQL File (using java.sql.Connection)
Queries some data.
Updates some data.
This all works without issue in Eclipse. However, when I export my file to a runnable .JAR file, it appears to stop after attempting to initialize the database connection. However, it does not throw a SQLException and never returns from the method. I checked my .jar file in 7ZIP to make sure that the SQLJDBC42.jar was packaged in the folder and it was.
The last log line I see when my runnable JAR executes is: Log.info("Connecting to Database...") however, all items run / and are logged in Eclipse execution.
Any ideas? Code below:
public void ConnectDatabase() {
Log.info("Connecting to Database...");
try {
//Connect to Database and Log success.
ConnectionInstance = DriverManager.getConnection(connectionURL);
Log.info("Database Connection Successful.");
} catch (Exception e) {
Log.info(String.format("ERROR | DATABASE: %s", e));
//System.exit(0);
}
Log.info("Leaving Database Method.");
}

running Postgresql in Java [duplicate]

I am having some difficulty in making connectivity with Java and PostgreSQL Database.I have download the JDBC4 Postgresql Driver, Version 9.2-1002 driver and properly set the application ClassPath. My code is as under
import java.sql.*;
public class JavaPostGreSQLConnectivity
{
public static void main(String[] args)
{
DB db = new DB();
db.dbConnect("jdbc:postgresql://127.0.0.1:5432/TestDB", "postgres","pwd");
}
}
class DB
{
public DB() {}
public void dbConnect(String db_connect_string, String db_userid, String db_password)
{
try
{
Class.forName("org.postgresql.Driver");
Connection conn = DriverManager.getConnection(db_connect_string, db_userid, db_password);
System.out.println("connected");
}
catch (Exception e)
{
e.printStackTrace();
}
}
};
Upon running I am getting the below error
Is it complaining about
Class.forName("org.postgresql.Driver");
If so then what will be the driver name? However, I followed this for my learning purpose.
However, If I do
C:\Program Files (x86)\Java\jdk1.7.0\bin>java -cp C:\Users\pos
tgresql-9.2-1002.jdbc4.jar; JavaPostGreSQLConnectivity
connected
It works.Why I need to explicitly mention the driver again when I have already placed it in the classpath properly? Is there any alternative way (I just want to put the JAR file in Classpath and the program should read from there)?
Thanks in advance
The driver name is OK. It is the same as mentioned in the official docs of the driver. Therefore the driver is just not in the classpath.
You say:
I [...] properly set the application ClassPath
On the other hand you start the program by just calling:
java JavaPostGreSQLConnectivity
In that case no PG driver is on the classpath. You have to add it by hand using someting like
java -cp postgresql-jdbc4.jar JavaPostGreSQLConnectivity
EDIT The question has been changed while typing, hence the duplication.
You added the jar only in you IDE. This helps the IDE to compile your code. If you start the program using you IDE then the IDE will also set the classpath for you. But if you don't start via the IDE then nobody knows the correct classpath and it has to be set by hand.
Your options are:
start always via IDE
make some batch script which hides the setting of the classpath (common solution)
set the CLASSPATH environment variable (does not scale with other Java applications)
make an "Executable Jar" and set the classpath there. (Search this site using that term).
put the jar into a place where the JVM picks it up automatically (e.g. in the lib/ext directory of the JRE). But polluting the JRE/JDK libs is the worst option.
Note: This is all basic Java knowledge and has nothing to do with PostgreSQL.

JavaFX8, SQLite(Embedded) exe, exec jar can't read/update the runtime database

I am creating a simple desktop application using JavaFx8 and SQLite and Eclipse Neon IDE. The Application works fine when launched to test from Eclipse. But If I create an executable jar or a Windows Exe file, it doesn't read/update the database.
Here's the code snippet used for creating and reading database.
private Connection loadPropertiesFileSQLite() throws SQLException, ClassNotFoundException {
try {
inputStream = new FileInputStream("databasesqlite.properties");
properties.load(inputStream);
Class.forName("org.sqlite.JDBC");
// database path, if it's new database, it will be created in the project folder
conSQLite = DriverManager.getConnection("jdbc:sqlite:Application.db");
System.out.println("Database Opened Successfully.");
} catch (Exception e) {
System.out.println("DDDD");
e.printStackTrace();
}
return conSQLite;
}
The connectors and jars I'm using are these:
The database is created in embedded mode when the user runs the application for the first time.
I have searched and tried scores of combinations from changing the location of database in DriverManager.getConnection("jdbc:sqlite:Application.db");
to many other things. Is there anything big that I'm missing here?
Are you using the properties file for nothing?
there is my config: sqlite-jdbc-3.7.15.jar
//auto commit on the connection.
conSQLite.setAutoCommit(true);
and check if do you have a permission at the file "Application.db".
The key is to use conSQLite = DriverManager.getConnection("jdbc:sqlite::resource:File:\\{some_path}\\Application.db");
instead of
conSQLite = DriverManager.getConnection("jdbc:sqlite:Application.db");
Now it works all fine.

DriverManager can't open SQL connector jar

It's really weird, but please hear out the story.
We both know the behavior of DriverManager during connector registering. I swear it was worked as expected, then suddenly it started keep throwing SQLException: no suitable driver found. To fix it, I have to break the jar file and expose the content folders directly under the classpath with my bare hand.
I then concluded that something stopped my DriverManager from being able to open the jar file automatically. It looks like a weird access permission problem. My system is windows 8.1 and I run java under administrator:cmd.
import java.sql.*;
public class Test{
public static void main(String... args) throws Exception{
String url = "jdbc:mysql://localhost:3306/bobbooks";
Connection conn = DriverManager.getConnection(url, "root", "password");
}
}
I have few classpath folders, only one of them contains the jar file.
OK, here is the code below, just a simple test class
I really want to fix this problem nicely, without breaking the jar file. Somebody help pls
I wonder if I understood what I was learning, but here I got the solution:
Simply edit the environment variable, to add the jar file as a CLASSPATH, like this:
(CLASSPATH:)
D:\mysql-connector-java-5.1.35-bin.jar;
then it works just fine.

Eclipse: How to prompt user to choose an application to open a file with?

I want to open arbitrary files from Eclipse. Currently I'm doing it like that:
if (((File) selectedElement).isFile()) {
try {
Desktop.getDesktop().open((File) selectedElement);
} catch (IOException e) {
//TODO prompt for the appropriate application to open this file.
e.printStackTrace();
}
}
Unfortunately, this only works, if the OS has a default application associated with the file type. That's why, if there isn't any default application defined, I want to ask the user which application I should use. But I have no idea how to 1) find a list of available application and 2) open the file with that application. Any hints how to implement that in a platform-independent way?
Any solution I can think of would depend on the OS. For example if your application is running in windows you could provide the user with a list of all *.exe files in the Program Files folder. Or all applications in the /bin, /sbin, /usr/bin, /usr/share/bin for a linux OS.
There's already another thread marked as resolved for this, check that out: Open WIth dialog in Java

Categories

Resources