第一种连接数据库的方法:
static final String JDBC_DRIVER="com.microsoft.sqlserver.jdbc.SQLServerDriver";
static final String DB_URL="jdbc:sqlserver://localhost:1433;DatabaseName=ReportServerTempDB";
static final String USER="sa";
static final String PASS="123457";
private Statement stmt;
private Connection conn;
private ResultSet rs;
//注册JDBC驱动器
Class.forName(JDBC_DRIVER);
//打开一个连接
conn = DriverManager.getConnection(DB_URL, USER, PASS);
stmt = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_READ_ONLY);
String sql = "SELECT userId,password FROM Table_1 where userId ='?' and password = '?'";
PreparedStatement ps = (PreparedStatement) (rs = stmt.executeQuery(sql));
第二种连接数据库的方法:
private static Connection conn;
conn = DBUtil.getConnection(DBType.INFORMIX, //此句相当于conn=DriverManager.getConnection(url);括弧里面的东西相当于url
DBConfig.sourceDriver,
"10.1.9.62",
"8004",
"case",
"casesoc",
"informix",
"informix"
);
两种数据的连接区别是,第二种比第一种简单方便快捷吗?
为什么?是因为第二种方法中DBUtil封装了JDBC的驱动,不需要再做其他的工作了(什么工作?),是吗?