Package poster.util;
Import java.sql.*;
Import Java.text.SimpleDateFormat;
Import Java.util.Date;
Import Javax.xml.crypto.Data;
public class DBHelper {
private static final String url= "Jdbc:oracle:thin: @localhost: 1521:ORCL";
private static final String user= "BBS";
private static final String password= "123";
private static Connection Con=null;
static{
try{
Class.forName ("Oracle.jdbc.driver.OracleDriver");
}catch (ClassNotFoundException e) {
E.printstacktrace ();
}
try{
Con=drivermanager.getconnection (URL, user, password);
}catch (SQLException e) {
E.printstacktrace ();
}
}
/**
* Access to the connection image before each operation
* @throws SQLException
*/
public static void Getconnection () throws sqlexception{
Con=drivermanager.getconnection (URL, user, password);
}
/**
* @param sql
* @param objects
* @return Returns the number of rows affected (by adding, deleting, modifying the action)
* @throws SQLException
*/
public static int ExecuteNonQuery (String sql,object...objects) throws sqlexception{
Getconnection ();
PreparedStatement pstm=con.preparestatement (SQL);
for (int i=0;i<objects.length;i++) {
Pstm.setobject (i+1, objects[i]);
}
int result=pstm.executeupdate ();
CloseConnection ();
return result;
}
/**
* @param sql
* @param objects
* @return access to the aggregate of the inquiries according to the SQL sentence
* @throws SQLException
*/
public static ResultSet ExecuteQuery (String sql,object...objects) throws sqlexception{
Getconnection ();
PreparedStatement pstm=con.preparestatement (SQL);
for (int i=0;i<objects.length;i++) {
Pstm.setobject (i+1, objects[i]);
}
ResultSet Set=pstm.executequery ();
return set;
}
/**
* You need to close each time you run out of connections
* @throws SQLException
*/
public static void CloseConnection () throws sqlexception{
if (con!=null) {
Con.close ();
}
}
/**
* Time
* @return
*/
public static String Csss () {
Date D=new date ();
SimpleDateFormat df=new SimpleDateFormat ();
String S=df.format (d);
return s;
}
}
Method Layer!