1 回答

TA貢獻1條經驗 獲得超3個贊
try?{
????????????//這里的是MYSQL?舉例
????????????//加載驅動?
????????????Class.forName("com.mysql.jdbc.Driver");
????????????//創(chuàng)建數據庫連接
????????????Connection?con?=?DriverManager.getConnection("jdbc:mysql://localhost:3306/test","root","root");
????????????//創(chuàng)建查詢?“請求”
????????????PreparedStatement?ps?=?con.prepareStatement("select?*?from?user");
????????????//返回查詢結果
????????????ResultSet?rs?=?ps.executeQuery();
????????????//遍歷結果
????????????while(rs.next())?{
????????????????//假如?User?表中?有個?name?列
????????????????System.out.println("name?>>?"+rs.getString("name"));
????????????}
????????????//關閉
????????????rs.close();
????????????ps.close();
????????????con.close();
????????}?catch?(Exception?e)?{
????????????e.printStackTrace();
????????}
添加回答
舉報