public static void delete(){
try{
System.out.println("請輸入要刪除的学号");
int id=sc.nextInt();
result=pstm.executeQuery("select id,name from students");
if(result.equals(id)){
pstm.executeUpdate("delete from student where id=?");
pstm.setInt(1, id);
System.out.println("信息删除成功!");
}else{
System.out.println("学号不存在");
}
}catch(SQLException e){
System.out.println("删除失败"+e.getMessage());
}
}
想实现删除功能,为什么提示空指针,请各位帮忙看一下
try{
System.out.println("請輸入要刪除的学号");
int id=sc.nextInt();
result=pstm.executeQuery("select id,name from students");
if(result.equals(id)){
pstm.executeUpdate("delete from student where id=?");
pstm.setInt(1, id);
System.out.println("信息删除成功!");
}else{
System.out.println("学号不存在");
}
}catch(SQLException e){
System.out.println("删除失败"+e.getMessage());
}
}
想实现删除功能,为什么提示空指针,请各位帮忙看一下
pstm.executeUpdate("delete from student where id=?");
pstm.setInt(1, id);
System.out.println("信息删除成功!");
}
改成这样
//if(result.equals(id))
{
pstm.prepareStatement("delete from student where id=?");
pstm.setInt(1, id);
pstm.executeUpdate();
System.out.println("信息删除成功!");
}
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.Scanner;
public class Test5 {
public static void main(String args[]){
Connection conn = null;
PreparedStatement pstmt= null;
Statement stmt = null;
ResultSet result = null;
Scanner sc = new Scanner(System.in);
PreparedStatement pstm = null;
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
conn = DriverManager.getConnection("jdbc:oracle:thin:@127.0.0.1:1521:mydb","funfenffun","65325535");
//lz改这句
pstm = conn.prepareStatement("select id,name from students");
result = pstm.executeQuery();
System.out.println("請輸入要刪除的学号");
int id=sc.nextInt();
while(result.next()){
System.out.println(result.getInt("id"));
if(result.getInt("id") == id){
pstm = conn.prepareStatement("delete from students where id=?");
pstm.setInt(1, id);
pstm.executeUpdate();
System.out.println("信息删除成功!");
}else{
System.out.println("学号不存在");
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
假设你students表里有
1 fun1
2 fun2
两个学生,
你要删除id为2的学生,就在int id=sc.nextInt();读入id=2
resultset先测id=1的情况,这时if(result.getInt("id") == id)条件不成立,就会执行else里面的
resultset先测id=2的情况,这时if(result.getInt("id") == id)条件成立,就会执行if里面的
所以输出结果就变成
1
学号不存在
2
信息删除成功!具体有没删除lz看表里面有没删掉吧
//lz,要删一条记录不用遍历resultset的,
//delete from ... where id=...,就已经会自己根据id值找到要删的记录了
//我说怎么觉得这个做得那么奇怪呢
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.Scanner;
public class Test5 {
public static void main(String args[]){
Connection conn = null;
PreparedStatement pstmt= null;
Statement stmt = null;
ResultSet result = null;
Scanner sc = new Scanner(System.in);
PreparedStatement pstm = null;
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
conn = DriverManager.getConnection("jdbc:oracle:thin:@127.0.0.1:1521:mydb","funfenffun","65325535");
pstm = conn.prepareStatement("select id,name from students");
result = pstm.executeQuery();
System.out.println("請輸入要刪除的学号");
int id=sc.nextInt();
pstm = conn.prepareStatement("delete from students where id=?");
pstm.setInt(1, id);
int count = pstm.executeUpdate();
//count返回删除的记录条数
if(count != 0)
System.out.println("信息删除成功!");
else
System.out.println("学号不存在");
} catch (Exception e) {
e.printStackTrace();
}
}
}