
i have written class.forName and getconnection in one method which returns connection string object under particular class and in someother class i want to call that for statement object

Here is an example where we have created two classes. In the first class, we have created a database connection method and in another class, we have invoked the connection method and set up the connection.
import java.sql.*;
class Connect {
public Connection connectToDatabase(){
Connection con=null;
try{
Class.forName("com.mysql.jdbc.Driver");
con = DriverManager.getConnection("jdbc:mysql://localhost:3306/test","root","root");
}
catch(Exception e){}
return con;
}
}
class DatabaseConnection
{
public static void main(String[] args)
{
try{
Connect c=new Connect();
Connection conn=c.connectToDatabase();
Statement st=conn.createStatement();
ResultSet rs=st.executeQuery("Select * from data");
while(rs.next()){
System.out.println(rs.getString("name")+"\t"+rs.getString("address"));
}
}
catch(Exception e){
System.out.println(e);
}
}
}