日期:2014-05-16 浏览次数:20505 次
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
?
//这里我们建立一个DBHelper类
public class DBHelper {
//此方法为获取数据库连接,此处以及后续文章中使用的都是MS SQL2005
???? private static Connection getCon() {
??????? ??Connection con = null;
????????? try {
?????????????? String driver = "com.microsoft.sqlserver.jdbc.SQLServerDriver"; //数据库驱动
?????????????? String url =?"jdbc:sqlserver://localhost:1433;DatabaseName=FileManager";//
?????????????? String user = "admin"; //用户名
???????????????String password =?"123456";//密码
?????????????? Class.forName(driver);?//加载数据库驱动
?????????????? con = DriverManager.getConnection(url, user, password);
?????????? } catch (Exception e) {
??????????????? e.printStackTrace();
?????????? }
?????????? return con;
????? }?
????? //查询语句
????? public static ResultSet executeQuery(String sql) throws SQLException {
???????????Connection con = getCon();
?????????? Statement stmt = con.createStatement();
???????????ResultSet rs = stmt.executeQuery(sql);
????????????return rs;
????? }
????? public static ResultSet executeQuery(String sql, Object... obj)???throws SQLException {
???????? Connection con = getCon();
???????? PreparedStatement pstmt = con.prepareStatement(sql);
???????? for (int i = 0; i < obj.length; i++) {
?????????????? pstmt.setObject(i + 1, obj[i]);
???????? }
???????? ResultSet rs = pstmt.executeQuery();
???????? return rs;
???? }
???? //执行增删改
???? public static int executeNonQuery(String sql) throws SQLException {
???????? Connection con = getCon();
???????? Statement stmt = con.createStatement();
???????? return stmt.executeUpdate(sql);
??? }
??? public static int executeNonQuery(String sql, Object... obj) throws SQLException {
?? ?? Connection con = getCon();
???????? PreparedStatement pstmt = con.prepareStatement(sql);
???????? for (int i = 0; i < obj.length; i++) {
????????????? pstmt.setObject(i + 1, obj[i]);
???????? }
??????? return pstmt.executeUpdate();
??? }
}