I am new to java, I have a doubt:
I am retunging con from this method:
package mypackage;
public class DBconnection {
Connection con = null;
public Connection getConnection() throws Exception, SQLException
{
try
{
Class.forName("oracle.jdbc.driver.OracleDriver");
con=DriverManager.getConnection("jdbc:oracle:thin:@zzz:1521:zzz","zzz", "zzz");
}
catch(Exception e)
{
}
return con;
}
public void removeConnection() throws SQLException
{
con.close();
}
}
Now when I am calling that getConnection(); method then I am getting con , now while using prepareStatement how can I use that in my query??
I called it
DBconnection dbconnect = new DBconnection();
dbconnect.getConnection().prepareStatement(""); //is this the right way to write??
or
DBconnection dbconnect = new DBconnection();
dbconnect.getConnection();
dbconnect.con.prepareStatement(""); //is this the right way to write??
after these how should i close the connection??
dbconnect.removeConnection();
No, this is not how you get database connection in Java EE. You should leave it to Java EE to handle connections for you, and you simply get it from Java EE. This tutorial might be a good start for you: J2EE DB Connection