Package com.sec.util;
Import java.sql.Connection;
Import Java.sql.DriverManager;
Import java.sql.PreparedStatement;
Import Java.sql.ResultSet;
Import java.sql.SQLException;
/**
* Database Helper Classes
*
* @author Administrator
*
*/
public class Conndb {
public static string URL = "jdbc:sqlserver://localhost:1434;databasename=shoppingdb";//Connection string
public static String USER = "sa";//Login name
public static String PWD = "123456";//Password
When the class loads, load the database driver (once this is loaded, it will not need to be loaded again)
static {
try {
Class.forName ("Com.microsoft.sqlserver.jdbc.SQLServerDriver");
} catch (ClassNotFoundException e) {
E.printstacktrace ();
}
}
/**
* Get connections
*
* @return
*/
public static Connection getconnection () {
Connection conn = null;
try {
conn = Drivermanager.getconnection (URL, USER, PWD);
Return conn;
} catch (SQLException e) {
Write log
E.printstacktrace ();
}
return null;
}
/**
* Close Related Resources
*
* @param Conn
* @param stm
* @param pstm
* @param RS
*/
public static void CloseAll (Connection conn,
PreparedStatement pstmt, ResultSet rs) {
try {
IF (conn! = null) {
Conn.close ();
}
if (pstmt! = null) {
Pstmt.close ();
}
if (rs! = null) {
Rs.close ();
}
} catch (SQLException e) {
TODO auto-generated Catch block
E.printstacktrace ();
}
}
}
Database helper Classes