?
/** 数据库连接*/ public class BaseDao { public final static String quDong = "com.microsoft.sqlserver.jdbc.SQLServerDriver"; // 数据库驱动 public final static String duanKOu = "jdbc:sqlserver://localhost:1433;databasename = news";// jdbc:sqlservier://数据库IP:端口号;DatabaseName=数据库 public final static String name = "sa";// 数据库名字 public final static String miMa = "sa";// 密码 protected Statement stmt; protected ResultSet rs; /** 关闭所有连接 */ public void closeAll(ResultSet rs, PreparedStatement pretmt, Connection conn) { if (rs != null) { try { rs.close(); } catch (Exception e) { e.printStackTrace(); } } if (pretmt != null) { try { pretmt.close(); } catch (Exception e) { e.printStackTrace(); } } if (conn != null) { try { conn.close(); } catch (Exception e) { e.printStackTrace(); } } } /**返回数据库连接*/ public Connection fanHuiShuJuKuLianJie() { Connection connetion = null; try { Class.forName(quDong); connetion = DriverManager.getConnection(duanKOu, name, miMa); } catch (Exception e) { e.printStackTrace(); } return connetion; } /**执行SQL语句,可进行增,删,改*/ public int ShuJuKuChaoZuo(String Sql, String[] param) { Connection con = null; PreparedStatement pstmt = null; ResultSet rs = null; int num = 0; try { con = fanHuiShuJuKuLianJie();// 得到数据库连接 pstmt = con.prepareStatement(Sql); if (param != null) { for (int i = 0; i < param.length; i++) { pstmt.setString(i + 1, param[i]);// 设置参数 } } num = pstmt.executeUpdate();// 执行SQL语句 } catch (Exception e) { e.printStackTrace(); } finally { closeAll(rs, pstmt, con); } return num; } public static String getStr(String info) { try { byte[] b = info.getBytes("ISO-8859-1"); return new String(b, "UTF-8"); } catch (UnsupportedEncodingException e) { e.printStackTrace(); return ""; } } }
?