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.
Related
I have to do a task for my university where I should write a function that returns a connection to a database. I create a new Connection object in the try block and now want to return it, but i need to ensure that my function always returns a connection even if i land in the catch block. How do I write a function that always returns a connection? I have tried to use finally, but the variable cn only exists in the try block.
import java.sql.Connection;
import java.sql.DriverManager;
import java.util.Properties;
public class ConnectDB {
final String propfile = "/properties/db.properties";
public static void main(String[] args) {
ConnectDB con = new ConnectDB();
con.connect();
}
public Connection connect() {
try {
final java.io.InputStream propFile = new ConnectDB().getClass().getResourceAsStream(propfile);
final Properties props = new Properties(System.getProperties());
props.load(propFile);
Connection cn = DriverManager.getConnection(props.getProperty("url"), props.getProperty("user"), props.getProperty("password"));
return cn;
}
catch (Exception e) {
System.out.println("connect ERROR :\n" + e.toString());
}
}
}
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 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();
}
}
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?
why is it my Connection code not working? I have everything right password,user, and the host and the driver but why is it not working??
import java.sql.*;
public class Connection {
public String url = "jdbc:mysql://localhost:3306/Testdb";
public String driver = "com.mysql.jdbc.Driver";
public String user = "root";
public String pass = "123192";
public void JdbcConnection(){
try{
Class.forName(driver);
}catch(Exception e){
e.printStackTrace();
}
try{
Connection con = DriverManager.getConnection(url,user,pass);
}catch(Exception e){
e.printStackTrace();
}
}
}
The problem is that "Connection con" should be of type java.sql.Connection, but since your own class is called Connection, your code thinks you are making a reference to that instead of java.sql.Connection. You can disambiguate the type of con by using the full class name like so:
try{
java.sql.Connection con = DriverManager.getConnection(url,user,pass);
}catch(Exception e){
e.printStackTrace();
}
Please change the name of your class (it must not a Connection especially when you import the java.sql.*) and you need to specify the username and password while obtaining a connection.
public class TestConnection {
....
Connection con = DriverManager.getConnection(url,user,pass);
}