chp7_JSP
第七章 JDBC数据库开发接口
7.3.1 JDBC的四种类型(page171)
- JDBC-ODBC桥加ODBC驱动程序
- 本地API
- JDBC网络纯Java驱动程序
- 本地协议纯Java驱动程序
7.3.2 数据驱动程序(page172)
加载Oracle的JDBC驱动程序的语句为:
Class.forName(“oracle.jdbc.driver.OracleDriver”);
使用JDBC-ODBC桥驱动程序的语句为:
Class.forName(“sun.jdbc.odbc.JdbcOdbcDriver”);
加载Sql2005驱动程序的语句为:
Class.forName(“com.microsoft.jdbc.sqlserver.SQLServerDriver”);
加载MySQL驱动下次语句为:
Class.forName(“com.mysql.jdbc.Driver”);
7.3.3 Connection对象(page172)
import java.sql.*;
public class DB {
public static Connection getConn() {
Connection conn = null;
try {
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection("jdbc:mysql://localhost/shopping?user=root&password=123456");
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
return conn;
}
public static PreparedStatement prepare(Connection conn, String sql) {
PreparedStatement pstmt = null;
try {
if(conn != null) {
pstmt = conn.prepareStatement(sql);
}
} catch (SQLException e) {
e.printStackTrace();
}
return pstmt;
}
public static PreparedStatement prepare(Connection conn, String sql, int autoGenereatedKeys) {
PreparedStatement pstmt = null;
try {
if(conn != null) {
pstmt = conn.prepareStatement(sql, autoGenereatedKeys);
}
} catch (SQLException e) {
e.printStackTrace();
}
return pstmt;
}
public static Statement getStatement(Connection conn) {
Statement stmt = null;
try {
if(conn != null) {
stmt = conn.createStatement();
}
} catch (SQLException e) {
e.printStackTrace();
}
return stmt;
}
/*
public static ResultSet getResultSet(Connection conn, String sql) {
Statement stmt = getStatement(conn);
ResultSet rs = getResultSet(stmt, sql);
close(stmt);
return rs;
}
*/
public static ResultSet getResultSet(Statement stmt, String sql) {
ResultSet rs = null;
try {
if(stmt != null) {
rs = stmt.executeQuery(sql);
}
} catch (SQLException e) {
e.printStackTrace();
}
return rs;
}
public static void executeUpdate(Statement stmt, String sql) {
try {
if(stmt != null) {
stmt.executeUpdate(sql);
}
} catch (SQLException e) {
e.printStackTrace();
}
}
public static void close(Connection conn) {
try {
if(conn != null) {
conn.close();
conn = null;
}
} catch (SQLException e) {
e.printStackTrace();
}
}
public static void close(Statement stmt) {
try {
if(stmt != null) {
stmt.close();
stmt = null;
}
} catch (SQLException e) {
e.printStackTrace();
}
}
- 本文标题:chp7_JSP
- 创建时间:2014-07-06 22:26:46
- 本文链接:2014/07/06/技能-修行-进步-JSP/chp7-JSP/
- 版权声明:本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!
评论