0

I have a java class which works fine ( I mean mysql connection to sql is success) But in the same package, I have another class. from where I am trying to create object of connection and then intend to use connection for various purposes. Here is the class which works fine (I changed function name from public static void main(String[] args) to connect(). I am really not sure why the connection is not callable from other file.

db.java

import java.sql.*;
import java.util.Properties;

public class db
{
  // The JDBC Connector Class.
  String dbClassName = "com.mysql.jdbc.Driver";

  // Connection string. emotherearth is the database the program
  // is connecting to. You can include user and password after this
  // by adding (say) ?user=paulr&password=paulr. Not recommended!

   String CONNECTION =
                          "jdbc:mysql://127.0.0.1/news";

  public Connection Connect() throws
                             ClassNotFoundException,SQLException
  {
    System.out.println(dbClassName);
    // Class.forName(xxx) loads the jdbc classes and
    // creates a drivermanager class factory
    Class.forName(dbClassName);

    // Properties for user and password. Here the user and password are both 'paulr'
    Properties p = new Properties();
    p.put("user","root");
    p.put("password","akshay");

    // Now try to connect
    Connection c = DriverManager.getConnection(CONNECTION,p);

    System.out.println("It works !");
    return c;
    }


}

And from this classs I am trying to call the function :

import java.sql.*;

public class findl {

    public static void main(String[] args)
    {
        System.out.println("hi");
    }   
    public Connection conn(){
        db dbs = new db();
        Connection clr = dbs.Connect();
        return clr;
            }

}

Foot Notes : The error is at line Connections clr = dbs.connect() and eclipse says

 1)unhandled exception type sqlexception

 2)unhandled classnotfoundexception.

If it was a problem with loading mysql.jar, it should not have worked in first place (in original class). Please tell me what I am missing. Thanks.

oentoro
  • 740
  • 1
  • 11
  • 32
akshayb
  • 1,219
  • 2
  • 18
  • 44
  • 1
    Those are compiler errors letting you know that dbs.connect() can throw two exceptions. You'll need to wrap it in a try{} catch(){}. – Justin Jasmann Apr 24 '13 at 17:23

2 Answers2

1

The method conn in the second example must indicate that it throws SQLException and ClassNotFoundException just like public Connection Connect() throws ClassNotFoundException,SQLException in the first class.

That's your problem: checked exceptions.

Lzh
  • 3,585
  • 1
  • 22
  • 36
  • thanks it worked. Can I ask for any further study material on this ? I am from python background and not sure what exactly happened. – akshayb Apr 24 '13 at 17:25
  • Also, will the new connection be thread safe? I intend to use the connection object in threads. – akshayb Apr 24 '13 at 17:27
  • This SO question might interest you: http://stackoverflow.com/questions/1531073/is-java-sql-connection-thread-safe You can find resources on almost anything online. What further information do you need? Are you curious about the Java API: http://docs.oracle.com/javase/7/docs/api/java/sql/Connection.html – Lzh Apr 24 '13 at 18:30
1

Use this instead of second file

 import java.sql.*;

 public class findl {

  public static void main(String[] args)
        {
            System.out.println("hi");
        }   
try{
    public Connection conn(){
        db dbs = new db();
        Connection clr = dbs.Connect();
        return clr;
            }
}


 catch(SQLException s){}

catch(ClassNotFoundException e) {

}

    }

mark the try-catch

Put this code inside a try-catch and handle the sqlException in catch block

cjava
  • 656
  • 1
  • 8
  • 22