JDBC连接数据库7大步骤
加载驱动→创建连接数据库对象→创建操作数据库对象→编写sql语句→执行sql语句,返回结果集→操作结果集→释放资源
1.Class.forName(“com.mysql.jdbc.Driver”);
2. Connection conn = DriverManager.getConnection( “jdbc:mysql://localhost:3306/kgcdb?
serverTimezone=GMT%2B8&useSSL=false”, “user”, “password”);
3. Statement st = conn.createStatement();
4. String sql = " sql语句 ";
5. ResultSet rs = stat.executeQuery(sql);
6. while (rs.next()) {
遍历结果集
}
7. rs.close();
st.close();
conn.close();