对数据库进行增删改和获得表中所有信息,方法调用怎么就不对啊!
public class StudentDAO {
static Statement stat;
static String sql;
static int id;
static int age;
static String name;
static Connection conn ;
static ResultSet rs;
static PreparedStatement ps;
//public StudentDAO(){
//}
public static Connection getConnection(){
Connection conn;
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
try {
conn = DriverManager.getConnection("jdbc:oracle:thin:@192.192.192.249:1521:orcl" ,"zhangxc","zhangxc");
return conn;
} catch (
SQLException e) {
e.printStackTrace();
return null;
}
} catch (
ClassNotFoundException e) {
e.printStackTrace();
return null;
}
}
public static void getAll(int id,String name,int age) throws SQLException{
try {
sql = "select * from student";
rs = stat.executeQuery(sql);
while (rs.next()) {
int id1 = rs.getInt(1);
String name1 = rs.getString(2);
int age1 = rs.getInt(3);
System.out.println("id:" + id + " name:" + name + " age:" + age);
// return "id:" + id + " name:" + name + " age:" + age;
}
} catch (SQLException e) {
//return "";
System.out.println("查询失败");
}
}
public static void get() throws SQLException {
try {
String id = javax.swing.JOptionPane.showInputDialog(null, "请输入学号");
sql = "select * from student where id=?";
//ResultSet rs = stat.executeQuery(sql);
PreparedStatement ps = conn.prepareStatement(sql);
ps.setString(1, id);
ResultSet rs = ps.executeQuery();
while (rs.next()) {
String idd =rs.getString(1);
String name = rs.getString(2);
int age = rs.getInt(3);
System.out.println("id:" + id + " name:" + name + " age:" + age);
}
} catch (SQLException e) {
System.out.println("查询失败");
}
}
public static void save() throws SQLException {
try {
//sql="insert into student(id,name,age) values('1','张三','2')";
// i=stat.executeUpdate(sql);
//System.out.println("插入成功");
String id = javax.swing.JOptionPane.showInputDialog(null, "请输入学号");
String name = javax.swing.JOptionPane.showInputDialog(null, "输入姓名");
String age = javax.swing.JOptionPane.showInputDialog(null, "请输入年龄");
sql = "insert into student(id,name,age) values(?,?,?)";
PreparedStatement ps = conn.prepareStatement(sql);
ps.setString(1, id);
ps.setString(2, name);
ps.setString(3, age);
int i = ps.executeUpdate();
System.out.println("添加成功");
} catch (SQLException e) {
System.out.println("添加失败");
}
}
public static void update() throws SQLException {
try {
// sql="update student set name=? where id=?";
// i=stat.executeUpdate(sql);
// System.out.println("修改成功");
// PreparedStatement pstmt = con.prepareStatement("UPDATE EMPLOYEES
// SET SALARY = ? WHERE ID = ?");
// pstmt.setBigDecimal(1, 153833.00)
// pstmt.setInt(2, 110592)
String id = javax.swing.JOptionPane.showInputDialog(null, "请输入要改的学号");
String name = javax.swing.JOptionPane.showInputDialog(null, "请输入改后的名字");
sql = "update student set name=? where id=? ";
PreparedStatement ps = conn.prepareStatement(sql);