JDBC
- java database connection
- 是java为连接数据库提供的一套接口
连接数据库步骤
1.加载驱动类
2.获取数据库连接 通过数据库账号密码获取连接
3.通过数据库连接对象 获取sql语句的执行对象
4.使用SQL语句执行对象来执行sql语句
5.接受执行sql语句后的结果集处理
6.关闭资源
Class.forName("com.mysql.jdbc.Driver");
Connection connection = DriverManager.getConnection("url", "username", "password");
PreparedStatement prepareStatement = connection.prepareStatement("update sort set sname = ?,sprice = ?,sdesc = ? where sid = ?");
prepareStatement.setString(1, "ruizhi");
prepareStatement.setString(2, "129");
prepareStatement.setString(3, "NMSL?");
prepareStatement.setString(4, "5");
int i = prepareStatement.executeUpdate();
System.out.println("受影响的行数:" + i);
connection.close();
prepareStatement.close();