I am writing this because I created a simple Login GUI App to test sqlite database as I am a student of Database Systems and new to it, I used java through eclipse, whenever I run the Application this message
java.sql.SQLException path to c:user//path does not exist
Error Screenshot
I have searched a lot on google but couldn't find the solution there is a similar question on stackoverflow but there were not enough answer related to my problem, I want to know how to change the code to make the Application work and connect to database?
Any help will be much appreciated.Thanks
Here is the code:
package dbms;
import java.sql.*;
import javax.swing.*;
public class dbConnection {
Connection conn = null;
public static Connection dbConnector(){
try{
Class.forName("org.sqlite.JDBC");
Connection conn = DriverManager.getConnection("jdbc:sqlite:C:\\Users\\chusm\\workspace\\DBMS\\SQlite\\DBMS.sqlite");
JOptionPane.showMessageDialog(null, "Connection Successful!!!");
return conn;
}
catch(Exception e){
JOptionPane.showMessageDialog(null, e);
return null;
}
}
}
It looks like you've got some weird whitespace action going on in your url (between "jdbc:sqlite" and "C:"
Please copy paste this exact code in your project and run it (I only removed the weird whitespace, the rest is exactly like your code)
package dbms;
import javax.swing.*;
import java.io.File;
import java.sql.Connection;
import java.sql.DriverManager;
public class StackOverflowExample {
Connection conn = null;
public static Connection dbConnector() {
try {
Class.forName("org.sqlite.JDBC");
Connection conn = DriverManager.getConnection("jdbc:sqlite:C:\\Users\\chusm\\workspace\\DBMS\\SQlite\\DBMS.sqlite");
JOptionPane.showMessageDialog(null, "Connection Successful!!!");
return conn;
} catch (Exception e) {
JOptionPane.showMessageDialog(null, e);
return null;
}
}
public static void main(String[] args) {
Connection connection = dbConnector();
}
}
Related
As I mentioned on the subject, I am facing the warning "The value of the local variable conn is not used"
I clearly used the variable on my coding but it shows me that type of error.
It will be highly appreciated if you can advise me on this.
[CODE]
package jdbc;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class example {
public static void main(String[] ar) {
try {
Class.forName("com.mysql.jdbc.Driver");
System.out.println("Success to find Driver");
}catch(ClassNotFoundException e) {
System.err.println("error = Failed to find driver");
}//*Find MYSQL driver
Connection conn = null;
try {
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/class", "root", "****");
System.out.println("Connected to MySql");
} catch (SQLException e) {
System.err.println("error = Failed to Create connection");
}//* Creating Connection
}
}
[RESULT AFTER RUN]
Success to find Driver
error = Failed to Create connection
In your code you have assigned a reference to conn but you are not using that object anywhere, hence the warning.
To get rid of the warning either use it somewhere (maybe do a sysout somewhere) or add the #SuppressWarnings("unused") annotation
I wrote this code but it doesn't work.
It throws an error that says that this is not suitable Driver.
Can anyone explain what I am doing wrong?
import java.sql.Connection;
import java.sql.DriverManager;
public class ConnectionUtil {
public static Connection getConnection() {
try {
Class.forName("org.h2.jdbc.Driver");
Connection con = DriverManager.getConnection("org.h2:database/javadb", "root", "");
return con;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
}
I used this URL for a connection to my DB "jdbc:h2:file:./database/mydatabase"
I am fairly new java web services and I trying to use it to access an oracle database. What my project is trying to do is take the input of a zip code and return the information from the database.
I got a web services client working for an animal type using this tutorial and I am trying to take what I learned from that for my project:
http://javapapers.com/web-service/java-web-service-using-eclipse/
Here is my the code for the main class I am using:
package com.zipws.test;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
//import javax.jws.WebService;
//import javax.jws.soap.SOAPBinding;
//import javax.jws.soap.SOAPBinding.Style;
public class ZipWebServiceImpl {
public String cityFinder(String zip) {
Connection con = null;
String str = "";
try{
String user = "IVRDEVUSR";
String pass = "voice001";
String url = "jdbc:oracle:thin:#UIQ-UAT-ORA-02:1521/IVRST01";
Class.forName("oracle.jdbc.driver.OracleDriver");
con = DriverManager.getConnection(url, user, pass);
Statement stmt = con.createStatement();
ResultSet rsZip = stmt.executeQuery("SELECT *FROM ZIPLOC WHERE ZIP = " + zip);
while(rsZip.next()){
if(zip.equals(rsZip.getString("ZIP"))){
str = rsZip.getString("CITY");
}
else{
str = "Zip code for city not found!";
}
}
rsZip.close();
stmt.close();
}
catch(SQLException e){
//System.out.println("Connection Failed.");
str = "connection failed";
}
catch(ClassNotFoundException cnfe){
//System.out.println("Unable to load driver.");
str = "Unable to load driver";
}
finally{
try{
con.close();
}
catch(SQLException e){
//System.out.println("Failed to close connection.");
str = "Failed to close connection.";
}
}
return str;
}
}
The web services client classes were generated by Eclipse which I learned to do from the tutorial earlier. When I run the client and enter a zipcode to try to invoke the above class, it returns a NullPointerException and I do not know why. Can anyone possibly explain why?
You should check in the finally block if 'con != null'. Otherwise even in cases the driver could not be loaded you are trying to close the connection which can never succeed.
Debug the method and check what exception occurs. Is maybe the oracle driver missing in the classpath?
I want to create a program to use it on any computer , so when i install it must import the database.sql from its place .. so i have to add it to the package of project , but when i did i have a message that's tells (java.sql.SQLException : no such table : table-name) , even though am sure that I have a table there.
so could u tell me where is the problem . or if there is any way to import the database from project folder wherever it was ?
thank you !
import java.awt.*;
import java.sql.*;
import javax.swing.*;
public class dbc {
Connection conn = null;
ResultSet rs = null ;
PreparedStatement pst = null ;
public static Connection ConnecrDb() {
try {
Class.forName("org.sqlite.JDBC");
Connection conn = DriverManager.getConnection("jdbc:sqlite:Tab.sqlite");
return conn;
}catch (Exception e ){
JOptionPane.showMessageDialog(null, e)
return null;
}
}
}
and there is a file in the package called Tab.sqlite
Firstly: You should copy/create a database and collect the database location path then when you are try to get a connection, should put the database urlPath into DriverManager.getConnection(urlPath);
Also you can try:
public Connection DBConn() {
String connStr = "jdbc:sqlite:<location path>/myDB.db";
Connection conn = null;
try {
Class.forName("org.sqlite.JDBC");
conn = DriverManager.getConnection(connStr);
} catch (ClassNotFoundException e) {
e.printStackTrace();
System.exit(1);
} catch (SQLException e) {
e.printStackTrace();
System.exit(2);
}
return conn;
}
I solved the problem , I should write this :
Connection conn = DriverManager.getConnection("jdbc:sqlite:/src/Tab.sqlite");
if I want to import the sql database from package of project I have to add /src/myDB.sqlite
thank you all :D
I'm creating a program where (among other things) from the space for the user to change the url, name and password for the database connection.
In a class I get the values that are saved in a file and up to here everything ok. The problem arises when I'm in class concerning the connection of the batabase. The fault is not the OutProp method (which fetches the data in the properties file) because I tried it in another class and it works perfectly. So I guess there is a problem of writing code maybe in public static method Connection ConnectDb () as being static you have to behave differently, and since I have started recently to study Java I think I missed something.
PS. Writing Connection conn = DriverManager. getConnection ("jdbc: mysql://localhost/databaseprogetto/root/root"); connects to the database.
Thank you for any suggestion or solution and I hope that I explained well.
import java.io.FileInputStream;
import java.sql.*;
import javax.swing.*;
import java.io.IOException;
import java.util.Properties;
public class JavaConnect {
Connection conn = null;
static String url_database;
static String username;
static String password;
public void OutProp (){
Properties prop = new Properties();
try {
prop.load(new FileInputStream("config.properties"));
url_database = prop.getProperty("Url");
username = prop.getProperty("Username");
password = prop.getProperty("Password");
} catch (IOException ex) {
ex.printStackTrace();
}
}
public static Connection ConnectDb(){
try{
Class.forName ("com.mysql.jdbc.Driver");
Connection conn = DriverManager.getConnection (url_database,username,password);
return conn;
}
catch (Exception e) {
JOptionPane.showMessageDialog(null, e);
return null;
}
}
}
1. Please make sure you have No space on either side of the 2 operands in Property file.
Eg:
Property1=value1
Property2=value2
2. Try putting this Property file in the bin folder of your Project.