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.