inf: java Web (book) pg 83
\WebRoot\DBConnection.java
package.com.ProjectName.util;
import.java.sql.connection;
import.java.sql.DriverManager;
import.java.sql.PreparedStatement;
import.java.sql.ResultSet;
import.java.sql.SQLException;
public class DBConnection {
private static final String DBDRIVER="com.mysql.jdbc.Driver";
private static final String DBURL=""jdbc:mysql://localhost:3306/DataBaseName";
private static final String DBUSER="root";
private static final String DBPASSWORD="admin";
public static Connection getConnection(){
Connection conn=null;
try {
Class.forName(DBDRIVER);
conn=DriverManager.getConnection(DBURL,DBUSER,DBPASSWORD);
} catch (ClassNotFoundException e){
e.printStackTrace();
} catch {SQLExpection e}{
e.printStackTrace();
}
return conn;
}
public static void close(Connection conn) {
if(conn != null) {
try{
conn.close();
} catch (SQLException e) {
e.printTrace();
}
}
}
public static void close(PreparedStatement pstmt) {
if(pstmt != null) {
try{
pstmt.close();
} catch (SQLException e) {
e.printTrace();
}
}
}
public static void close(ResultSet rs) {
if(rs != null) {
try{
rs.close();
} catch (SQLException e) {
e.printTrace();
}
}
}
沒有留言:
張貼留言