code example: Import Java.sql.ResultSet;
Import java.sql.SQLException;
Import java.sql.Statement;
Import java.util.Properties;
Import java.com.sun.org
. Apache.xml.internal.security.Init;
public class Basedao {
private static String Driver = "Com.mysql.jdbc.Driver";
private static String URL = "Jdbc:mysql://localhost:3306/epet";
private static String user = "root";
private static String password = "root";
Connection con = null;
static {
Init ();
try {
Class.forName (driver);
} catch (ClassNotFoundException e) {
E.printstacktrace ();
}
}
/**
* Get connections and Catch exceptions
* @return
*/
Public Connection getconnection () {
try {
if (con==null) {
con = drivermanager.getconnection (
Url,user,password);
}
} catch (SQLException e) {
E.printstacktrace ();
}
return con;
}
private static void Init () {
Properties Params=new properties ();
String config= "Database.properties";
InputStream Is=basedao.class.getclassloader (). getresourceasstream (config);
try {
Params.load (IS);
} catch (IOException e) {
E.printstacktrace ();
}
Driver=params.getproperty ("Driver");
Url=params.getproperty ("url");
User=params.getproperty ("user");
Password=params.getproperty ("password");
}
/**
* Close Database connection
*/
public void CloseAll (ResultSet rs,statement st,connection con) {
try {
if (rs!=null) {
Rs.close ();
}
} catch (SQLException E1) {
E1.printstacktrace ();
}
try {
if (st!=null) {
St.close ();
}
} catch (SQLException E1) {
E1.printstacktrace ();
}
try {
if (con!=null) {
Con.close ();
}
} catch (SQLException e) {
E.printstacktrace ();
}
}
/**
* Delete and change the operation
* @param sql
* @param param
* @return
*/
public int exceuteupdate (String sql,object[] param) {
PreparedStatement Ps=null;
int result=0;
Con=getconnection ();
try {
Ps=con.preparestatement (SQL);
if (param!=null) {
for (int i = 0; i < param.length; i++) {
Ps.setobject (I+1,param[i]);
}
}
Result=ps.executeupdate ();
} catch (SQLException e) {
E.printstacktrace ();
}finally{
CloseAll (NULL, PS, con);
}
return result;
}
}
JDBC Connection Database Basedao class