改错package text6; import java.sql.*; import org.w3c.dom.events.EventException; public class customer { public static void main(String args[]){ String url="jdbc:mysql://127.0.0.1:3306/mydatabase"; String user="root",pwd=""; Connection con; Statement sql; ResultSet rs; try { Class.forName("com.mysql.jdbc.Driver"); }catch(ClassNotFoundException e) { System.out.print("text"+e); } try { con=DriverManager.getConnection(url,user,pwd); sql=con.createStatement(); String []str={"delete from customers where customerID= 'ADDIFK01'", "delete from customers where customerID='ALBIBB01'", "INSERT INTO customers VALUES ('ALBRDE01','Dave Albrechtsberger','(508) 555-1216')", "INSERT INTO customers VALUES ('ALKAJH01','Jim alkan','(832) 555-5617')", "INSERT INTO customers VALUES ('RENDE01','Dave Arensky','(604) 555-5461')", "INSERT INTO customers VALUES ('AUBEJL01','Jill Auber','(832) 555-8480')"}; for(int i=0;i<=str.length;i++) { int k=sql.executeUpdate(str[i]); rs=sql.executeQuery(str[i]); while(rs.next()){ String number=rs.getString(1); String name=rs.getString(2); String num=rs.getString(3); System.out.println(number+","+name+","+num); System.out.println(); } } con.close(); }catch(SQLException ee) { System.out.print("text"+ee); } } }
时间: 2024-03-11 19:46:05 浏览: 163
报关单改错练习题1.pdf
以下是改错后的代码:
```java
package text6;
import java.sql.*;
public class customer {
public static void main(String args[]) {
String url = "jdbc:mysql://127.0.0.1:3306/mydatabase";
String user = "root";
String pwd = "";
Connection con = null;
Statement sql = null;
ResultSet rs = null;
try {
Class.forName("com.mysql.jdbc.Driver");
} catch (ClassNotFoundException e) {
System.out.print("text" + e);
}
try {
con = DriverManager.getConnection(url, user, pwd);
sql = con.createStatement();
String[] str = {
"delete from customers where customerID = 'ADDIFK01'",
"delete from customers where customerID = 'ALBIBB01'",
"INSERT INTO customers VALUES ('ALBRDE01', 'Dave Albrechtsberger', '(508) 555-1216')",
"INSERT INTO customers VALUES ('ALKAJH01', 'Jim alkan', '(832) 555-5617')",
"INSERT INTO customers VALUES ('RENDE01', 'Dave Arensky', '(604) 555-5461')",
"INSERT INTO customers VALUES ('AUBEJL01', 'Jill Auber', '(832) 555-8480')"
};
for (int i = 0; i < str.length; i++) {
int k = sql.executeUpdate(str[i]);
rs = sql.executeQuery("SELECT * FROM customers");
while (rs.next()) {
String number = rs.getString(1);
String name = rs.getString(2);
String num = rs.getString(3);
System.out.println(number + "," + name + "," + num);
System.out.println();
}
}
con.close();
} catch (SQLException ee) {
System.out.print("text" + ee);
} finally {
try {
if (rs != null) rs.close();
if (sql != null) sql.close();
if (con != null) con.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
```
改错的部分:
1. 原代码中未声明 `ResultSet`、`Connection`、`Statement` 变量,需要在开头声明并初始化为 null。
2. `for` 循环的判断条件应该是 `i < str.length` 而不是 `i <= str.length`。
3. 执行 SQL 语句后应该立即执行查询语句获取结果集,原代码中在循环结束后执行查询语句,会导致只输出最后一次插入记录的结果。
4. 在处理完数据库操作后需要关闭 `ResultSet`、`Statement`、`Connection` 这三个对象,可以使用 `finally` 语句块实现。
阅读全文