JDBC编程步骤
1.加载驱动/注册驱动
Class.forName("com.mysql.jdbc.Driver");//加载MySQL驱动类
2.获取数据库连接
DriverManager驱动管理类
Connection连接抽象类Connection getConnection(String url, String user, String password)
Connection connection = DriverManager
.getConnection("jdbc:mysql://localhost:3307/jdbc",
"root","a12345");
3.执行SQL语句
Statement类封装了SQL的执行过程
- 查询(select) :
statement.executeQuery(sql);
- 更新(insert/update/delete):
int executeUpdate(String sql, int autoGeneratedKeys);
返回值int为执行更新SQL后的影响行数。
ResultSet封装了查询后的结果
//SQL语句
String sql