lass DB {
public static Connection getConn() {
Connection conn = null;
try {
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/
bbs", "root" , "root");
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
return conn;
}
public static Statement createStmt(Connection conn) {
Statement stmt = null;
try {
stmt = conn.createStatement();
} catch (SQLException e) {
e.printStackTrace();
}
return stmt;
}
public static ResultSet executeQuery(Statement stmt, String sql) {
ResultSet rs = null;
try {
rs = stmt.executeQuery(
sql);
} catch (SQLException e) {
e.printStackTrace();
}
return rs;
}
/*public static ResultSet executeQuery(Connection conn, String sql) {
Statement stmt = null;
ResultSet rs = null;
try {
stmt = conn.createStatement();
rs = stmt.executeQuery(sql);
} catch (SQLException e) {
e.printStackTrace();
}
// finally {
// close(stmt);
// }
return rs;
}*/
public static int executeUpdate(Connection conn, String sql) {
int ret = 0;
Statement stmt = null;
try {
stmt = conn.createStatement();
ret = stmt.executeUpdate(sql);
} catch (SQLException e) {
e.printStackTrace();
} finally {
close(stmt);
}
return ret;
}
public static PreparedStatement prepareStmt(Connection conn, String sql) {
PreparedStatement pstmt = null;
try {
pstmt = conn.prepareStatement(sql);
} catch (SQLException e) {
e.printStackTrace();
}
return pstmt;
}
public static PreparedStatement prepareStmt(Connection conn, String sql, int autoGeneratedKeys) {
PreparedStatement pstmt = null;
try {
pstmt = conn.prepareStatement(sql, autoGeneratedKeys);
} catch (SQLException e) {
e.printStackTrace();
}
return pstmt;
}
public static void close(Connection conn) {
if(conn != null) {
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
conn = null;
}
}
public static void close(Statement stmt) {
if(stmt != null) {
try {
stmt.close();
} catch
(SQLException e) {
e.printStackTrace();
}
stmt = null;
}
}
public static void close(ResultSet rs) {
if(rs != null) {
try {
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
rs = null;
}
}
}
8.
SQLSERVER
驱动:”com.microsoft.jdbc.sqlserver.SQLServerDriver”
连接:”jdbc:microsoft:sqlserver://127.0.0.1:1433;databaseName=king”,”sa”,””
MYSQL
驱动:”com.
mysql.jdbc.Driver”
连接:"jdbc:mysql://127.0.0.1/king","root","king521"
ORACLE
驱动:"oracle.jdbc.driver.OracleDriver"
连接:"jdbc:oracle:thin:@127.0.0.1:1521:king","scott","tiger"
odbc
驱动:”sun.jdbc.odbc.jdbcOdbcDriver”
连接:”jdbc:odbc:mldn” //后面是数据源名称
9.条件必须加上单引号!
10.如果要在表单中用select和text要用2个form
11.preparedStatement之所以好,是因为:在插数据时如:Mr’smith 因为SQL语句的常量是要用单引号引起来的
ps.setInt(1,name);
ps.executeUpdate();
ps.setInt(1,uuuuu);
ps.executeUpdate(); //就这样可以连着写
12.可滚动的结果集 不建议使用