public class DBHelper {
private static final String url= "Jdbc:oracle:thin: @localhost: 1521:ORCL";//Connection string
private static final String user= "nga";//user name
private static final String password= "123";//Password
private static Connection con =null;
static{
try {
Class.forName ("Oracle.jdbc.driver.OracleDriver");
} catch (ClassNotFoundException e) {
TODO auto-generated Catch block
E.printstacktrace ();
}
try {
con = drivermanager.getconnection (Url,user,password);
} catch (SQLException e) {
TODO auto-generated Catch block
E.printstacktrace ();
}
}
/**
* Obtain the Connection object each time before doing the database operation
* @throws SQLException
*/
public static void Getconnection () throws sqlexception{
if (con!=null) {
if (con.isclosed ()) {
con = drivermanager.getconnection (Url,user,password);
}
}else{
con = drivermanager.getconnection (Url,user,password);
}
}
/**
* You need to turn it off every time you run out of connections
* @throws SQLException
*/
public static void CloseConnection () throws sqlexception{
if (Con!=null) {
Con.close ();
}
}
Ways to get Database connection objects