Ps.参数只包含了username和password。题目还要求加上hobby
2.请编写程序,用于根据id 删除account 表的记录, account 表结构为(id ,name, balance)。其中数据库连接url=”jdbc:mysql://localhost:3306/db”, Driver=”com.mysql.jdbc.Driver”,用户名及密码都为root。
ackage test;
import java.sql.*;
public class test {
public static void main(String[] args) {
Statement statement = null;
ResultSet resultSet = null;
Connection connection = null;
try {
//加载驱动
Class.forName(“com.mysql.cj.jdbc.Driver”);
//获取连接
String url = “jdbc:mysql://localhost:3306/db”;
String user = “root”;
String password = “root”;
connection = DriverManager.getConnection(url,user,password);
//取得数据库操作对象
statement = connection.createStatement();
//执行sql
String sql = “delete from account where id =1”;
resultSet = statement.executeQuery(sql);
//输出
while (resultSet.next()){
System.out.println(resultSet.getInt(“id”)+” “+resultSet.getString(“name”)+””+resultSet.getString(“balance”));
}
} catch (ClassNotFoundException | SQLException e) {
e.printStackTrace();
}finally {
//关闭流
if (resultSet != null) {
try {
resultSet.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (statement != null) {
try {
((java.sql.Statement) statement).close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (connection != null){
try {
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
}
Ps.删除的是id=1