Java MySQL template, very elegant. It also takes into account both performance PreparedStatement and security (anti-SQL injection). For relatively simple database operations basically meet the requirements.
Package DAO;
Import java.sql.Connection;
Import Java.sql.DriverManager;
Import java.sql.PreparedStatement;
Import Java.sql.ResultSet;
public class Basedao {
public static final String DRIVER = "Oracle.jdbc.driver.OracleDriver";
public static final String URL = "Jdbc:oracle:thin: @localhost: 1521:ORCL";
public static final String USERNAME = "Ma";
public static final String PASSWORD = "Malei";
Connection Connection = null;
PreparedStatement preparedstatement = null;
ResultSet ResultSet = null;
Public Connection getconnection () throws Exception {
Class.forName (DRIVER);
Connection = Drivermanager.getconnection (Url,username,password);
return connection;
}
Public ResultSet executeQuery (String sql) throws Exception {
Connection = This.getconnection ();
PreparedStatement = connection.preparestatement (sql);
ResultSet = Preparedstatement.executequery ();
return resultSet;
}
public int executeupdate (String sql,object[] obj) throws Exception {
Connection = This.getconnection ();
PreparedStatement = connection.preparestatement (sql);
for (int i =0;i<obj.length;i++) {
Preparedstatement.setobject (i+1, obj[i]);
}
return Preparedstatement.executeupdate ();
}
public void CloseAll () throws Exception {
if (null! = ResultSet) {
Resultset.close ();
}
if (null! = PreparedStatement) {
Preparedstatement.close ();
}
if (null! = connection) {
Connection.close ();
}
}
}