不知道什么错误,运行结果就是不对!帮帮忙啊!!!!
import com.noter.Student;
import java.sql.Date;
import java.util.List;
import java.util.Scanner;
import java.sql.*;
import java.util.ArrayList;
public class StudentDAO {
static Statement stat;
static String sql;
static Connection conn;
static ResultSet rs;
static PreparedStatement ps;
static int i;
static String name;
static int id;
static int age;
static Date registerDate;
static float scorce;
public static void update(Student student2) throws Exception {
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
conn = DriverManager.getConnection(
"jdbc:oracle:thin:@192.192.192.249:1521:orcl",
"zhangxc", "zhangxc");
}catch (
ClassNotFoundException e1) {
System.out.println("找不到驱动类");
e1.printStackTrace();
}catch(
SQLException e){
System.out.println("链接数据库失败");
}
try {
stat = conn.createStatement();
sql = "update student set name=?,age=?,score=?,registerDate=? where id=? ";
ps = conn.prepareStatement(sql);
ps.setInt(1, id);
ps.setString(2, name);
ps.setInt(3, age);
ps.setFloat(4, scorce);
ps.setDate(5, registerDate);
i = ps.executeUpdate();
System.out.println("修改后的信息为:" + "id:" + id + " name:" + name+ " age:" + age+" scorce:"+scorce+registerDate);
} catch (SQLException e) {
System.out.println("修改失败");
}
try{
if(ps!=null){
ps.close();
ps=null;
}
if(stat!=null){
stat.close();
stat=null;
}
if(conn!=null){
conn.close();
conn=null;
}
}catch(Exception e){
System.out.println("数据库关闭异常");
}
}
public static void main(String[] args) throws Exception {
int a = 0;
for (int j = 0; j < 100; j++) {
System.out.println("请选择您要做的工作:1:获得全部学生信息 2:获得指定id的学生信息 3:插入信息 4:更新信息 5:删除信息 请输入:");
Scanner reader = new Scanner(System.in);
a = reader.nextInt();
String id2 = javax.swing.JOptionPane.showInputDialog(null,"请输入要改的学号");
id=Integer.parseInt(id2);
String name2 = javax.swing.JOptionPane.showInputDialog(null,"请输入改后的名字");
name=name2;
String age2 = javax.swing.JOptionPane.showInputDialog(null,"请输入改后的年龄");
age=Integer.parseInt(age2);
String scorce2=javax.swing.JOptionPane.showInputDialog(null,"请输入改后的分数");
scorce = Float.parseFloat(scorce2);
String registerDate2=javax.swing.JOptionPane.showInputDialog(null,"请输入改后的日期");
registerDate = Date.valueOf(registerDate2);
Student student2=new Student();
student2.setAge(age);
student2.setId(id);
student2.setName(name);
student2.setScorce(scorce);
student2.setRegisterDate(registerDate);
update(student2);
}catch(
IllegalArgumentException s){
System.out.println("传递不正确参数");
}
}
这使仅仅做得更新的部分,运行的时候写4,就进入更新,但是加入date型之后老说修改错误。输入的date型是yyyy-mm-dd型的,不知道是哪里错了!!大家来帮忙啊!!谢谢了!!
------解决方案--------------------
catch (SQLException e) {
System.out.println("修改失败");
}
谁说没异常,是你自己屏蔽掉异常了
改
catch (SQLException e) {
e.printStackTrace();
System.out.println("修改失败");
}
保证一大堆异常信息,只有你贴上异常信息,才可能帮上你
另。。。。。。2、12楼不对的。。。