java.sql.SQLException:path - java

I got this error when I changed my java project into an .exe file and then I try it in another PC The Error
here is the code of connecting to SQLite Database
public static Connection dbConnector() {
try {
Class.forName("org.sqlite.JDBC");
Connection conn=DriverManager.getConnection("jdbc:sqlite:C:\\Users\\3542\\Desktop\\DocProject\\DoctorProject.db");
//JOptionPane.showConfirmDialog(null, "connection succesfull");
return conn;
}catch(Exception e)
{
JOptionPane.showConfirmDialog(null, e);
return null;
}
}
I want to find a way when I transfer my application into another PC my database will work fine
I am using Eclipse SQLite and Lanuch4j for .exe
Thanks in Advance

The path doesn't exist. If you want to run it on other pc's I would recommend you to use relative paths instead of an absolute one.

Related

Java-How Do I Rerefrence Sqlite Database in the Same Folder With My Project Files?

am working on a certain java Management System where am using sqlite as my primary database. Am using netbeans gui builder to work on my project. I have googled and stack overflowed how to reference sqlite database in the same dir with my project files without including full path like c://filepath but nothing promising yet. I don't want to include the full path to my database, I want to use sqlite file from the project files so that everything will work smoothly even after distributing my project...
My Connection Class currently Looks like this;
import java.sql.Connection;
import java.sql.DriverManager;
public class Connect {
private static Connection con = null;
public static Connection connecting(){
try{
Class.forName("org.sqlite.JDBC");
con = DriverManager.getConnection("jdbc:sqlite:programming.sqlite");
JOptionPane.showMessageDialog(null, "Working", null, JOptionPane.INFORMATION_MESSAGE);
}catch(Exception e){
JOptionPane.showMessageDialog(null, e.getMessage(), null, JOptionPane.ERROR_MESSAGE);
}
return con;
}
}
However that's seems not to work.. Remember Connection class and database are saved in the same dir...
Please note am avoiding absolute path like c://users/programming.sqlite
Anything to help please???
I think it must work as you shown, but, you can load programming.sqlite as a java.io.File and then put absolute path into jdbc url connection. I mean this:
File temp = new File("programming.sqlite");
String connection = "jdbc:sqlite:" + temp.getAbsolutePath().replace("\\","\\\\");
con = DriverManager.getConnection(connection);
...
If this code doesn't work for you, then your problem must be with programming.sqlite location, because in this case it is not on the same home dir of your java programm.

SQLite Exception Path does not exist

I am trying to connect an already existing SQLite database to my java project, as a temporary solution for testing a simple log-in dialog. Here is my connector class:
import java.sql.*;
import javax.swing.*;
public class SQLite_login_connector {
//Initializes global connection variable (unused until now!)
Connection conn_log = null;
public static Connection logindb_connection(){
//Simple try catch block that prints error trace log in a JOptionPane if runtime error occurs.
try{
Class.forName("org.sqlite.JDBC");
Connection conn_log = DriverManager.getConnection("jdbc:sqlite:/SQLite/ISRSMS_Login.sqlite");
JOptionPane.showMessageDialog(null, "Connection Successful!");
return conn_log;
} catch(Exception e){
JOptionPane.showMessageDialog(null, e);
return null;
}
}
}
My SQLite database is located in a folder within my project folder:
When I try running my connector class, it throws the following exception:
For me it looks as if it assumes that the folder which contains my database is located on my F: drive, as opposed to my root project in my local repository. I have tried putting the database into my src folder, as well as into a new resource folder all-together. Every time it throws the same exception. Why does it do that?
As you have metntioned / in yor connection string it is pointing to the root directory of your drive
Remove the / in your connection string Do as follows
DriverManager.getConnection("jdbc:sqlite:SQLite/ISRSMS_Login.sqlite")
For your convenience please follow the naming convention of java language
http://www.oracle.com/technetwork/java/codeconventions-135099.html
Why is your URL .sqllite? I would expect .db - like here
jdbc:sqlite:C:/mydir/mydb.db

JDBC for SQLite in Netbeans: No suitable driver found

I need to load data from an SQLite file into a java program which I develop in Netbeans.
The file is to be loaded via a swing menu item. I'm using sqlitejdbc as driver.
Here are the code blocks I assume to be important:
// header stuff
package aufgabe_9;
import java.sql.*;
//...
// menu item opening the file
private void mitemOpenFileActionPerformed(java.awt.event.ActionEvent evt)
{
/**
* Handles the dialogue for selecting and loading file.
*/
JFileChooser fileChoose = new JFileChooser();
fileChoose.showOpenDialog(this); //'this' calls the current object
//Load the sql file
try {
String filePath = fileChoose.getSelectedFile().toString();
Connection conn = DriverManager.getConnection("jdbc:sqlite:" +
filePath);
//Close the connection
if (conn != null)
conn.close();
}
catch (SQLException e){System.err.println("Database problem: " + e);}
}
}
//...
When running the program and loading a file via the menu, I get the following error:
java.sql.SQLException: No suitable driver found for jdbc:sqlite:/home/levent
/temp/A9AProbeflaeche.db
After reading the respective stackexchange posts, I understand that this
problem can be caused by (1) a malformed file URL or (2) the driver not being
loaded. Here's some further information:
I added the sqlitejdbc-3.7.2.jar to the library classpath via Tools --> Libraries as well as to the project libraries via Window --> Projects.
I also checked the Classpath by using this function. It contains the path to the jdbc jar-file just as expected.
I can connect to the database via the Services menu without any problems, so I can assume the URL to be correct, as well as sqlite running on my system.
Some OS information: I'm running Netbeans 8.0 on 64 bit ARCH Linux 3.12.9-2.
Can anybody tell me what I'm missing here? Any help appreciated!
Problem solved
Here is the code that works for me:
//...
private void mitemOpenFileActionPerformed(java.awt.event.ActionEvent evt)
{
/**
* Handles the dialogue for selecting and loading file.
*/
JFileChooser fileChoose = new JFileChooser();
fileChoose.showOpenDialog(this);
//Load the sql file
try {
//Get file path
String filePath = fileChoose.getSelectedFile().toString();
//Open connection
Class.forName("org.sqlite.JDBC");
Connection conn = DriverManager.getConnection("jdbc:sqlite:" + filePath);
//Do stuff...
//Close the connection
conn.close();
}
//"Multicatch":
catch (SQLException | ClassNotFoundException e) {
System.err.println("Database problem: " + e);
}
//...
You probably need to load the driver class so that it registers itself to the DriverManager using the following code:
Class.forName("org.sqlite.JDBC");
Note: this only needs to be called once in your application.
This was a standard procedure before the Java included the ServiceLoader API, now the DriverManager uses that API to register the drivers it finds in the classpath, but the drivers need to declare a file named java.sql.Driver containing the name of the driver class in the directory META-INF\services of their jar.

connect eclipse web application to sql 2012 database

I'm looking into how to connect a web application to an sql 2012 database "MyTestDatabase" with Windows authentication. I have a similar project that is a simple java application that prints out the contents of a table. here's the code for it...
public class sqldriver {
Connection connection = null;
public sqldriver() {}
public boolean doConnection() {
try {
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
connection = DriverManager
.getConnection("jdbc:sqlserver://localhost:1433;database=MyTestDB;integratedSecurity=true");
Statement stmt = connection.createStatement();
} catch (ClassNotFoundException e) {
System.out.println("ClassNotFoundException : " + e.getMessage()); return false;
} catch (SQLException e) {
System.out.println(e.getMessage()); return false;
}
return true;
}
All I can find are tutorials for Derby and that's not what I need. The error I keep getting is...
WARNING: Failed to load the sqljdbc_auth.dll cause : no sqljdbc_auth in java.library.path
I've added the sqljdbc_auth.dll to the web-inf/lib, but it doesn't fix the problem. Is there something I've missed?
make sure you add your database in the odbc !!
Go to the start menu and type Odbc then it will appear a dialog >>
select the system dsn tap then select add then select your server whatever Mysql Or ms Access
after that enter your DB info if you select Mysql then save by click ok
then go to your Code and make a string whith your config and send it to the connection object!!
note:
You should delete your constructor and leave the compiler run the default..
To load sqljdbc_auth.dll, you either need to include the DLL in a location on the system PATH, or you need to explicitly specify the java.library.path property in the run configuration of Eclipse.

JDBC can't be found in Eclipse

public static boolean startDriver(){
try{
Class.forName("com.mydql.jdbc.Driver");
System.out.println("Loaded MySQL driver!");
}catch(ClassNotFoundException e){
System.out.println("Failed to start MySQL driver! JDBC not found!");
return false;
}
return true;
}
I'm trying to use JDBC with my Java project but I keep receiving a ClassNotFoundException. I have referenced the mysql_connector.jar that I downloaded from the MySQL website and tried everything! It's in my >Referenced Libraries folder but I still keep getting the error!
Class.forName("com.my**d**ql.jdbc.Driver");
Ought to be
Class.forName("com.mysql.jdbc.Driver");

Categories