新手学java连接Access数据库,执行简单的查询,删除,插入,更新这4个操作,删除时有异常,大侠指教 30
我打算删除学号是55这一行,可以删除不了,数据库的内容和运行结果请看图importjava.io.IOException;importjava.sql.*;/***Cre...
我打算删除学号是55这一行,可以删除不了,数据库的内容和运行结果请看图
import java.io.IOException;
import java.sql.*;
/**
* Created by Meng on 14-3-31.
*/
public class AccessDB {
public static void main(String[] args) throws Exception{
Connection con;
Statement stmt;
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
con=DriverManager.getConnection ("jdbc:odbc:Student","","");
stmt = con.createStatement();
AccessDB adb = new AccessDB();
// adb.insertRecords(stmt);
adb.getRecords(stmt);
adb.deleteRecords(stmt);
}
public void getRecords(Statement s) {
try {
String sqlstr = "select * from student";
int i = 1;
ResultSet rs =s.executeQuery(sqlstr);
while (rs.next()) {
int num = rs.getInt(1);
String name = rs.getString(2);
int age = rs.getInt(3);
System.out.println("第" + i + "条: " +num + " " + name + " " + age);
i = i + 1;
}
} catch (Exception ex) {
ex.printStackTrace();
}
}
public void insertRecords(Statement s) {
byte[] b = new byte[255];
System.out.println("请输入你要输入的记录,记录的格式为:\"学号 姓名 年龄\"");
try {
System.in.read(b);
} catch (IOException iex) {
iex.printStackTrace();
}
String userInput = new String(b);
userInput = userInput.trim();
String[] ui = userInput.split(" ");
String sqlstr = "insert into student(num, name, age) values('" + Integer.parseInt(ui[0]) + "','" + ui[1] + "', '"
+ Integer.parseInt(ui[2]) +"')";
try {
s.executeQuery(sqlstr);
} catch (Exception ex) {
// ex.printStackTrace();
}
System.out.println("记录已插入,请查看!");
}
public void deleteRecords(Statement s) {
System.out.println("请输入你要删除的记录的学号:");
byte[] b = new byte[255];
try {
System.in.read(b);
} catch (IOException ioex) {
ioex.printStackTrace();
}
String userInput = new String(b);
userInput = userInput.trim();
String[] ui = userInput.split(" ");
for (int i = 0; i < ui.length; i ++) {
String delstr = "delete from student where num = '"+ui[i]+"'";
try {
s.executeQuery(delstr);
} catch (SQLException e) {
e.printStackTrace();
}
}
}
public void updateRecords(Statement s) {
}
} 展开
import java.io.IOException;
import java.sql.*;
/**
* Created by Meng on 14-3-31.
*/
public class AccessDB {
public static void main(String[] args) throws Exception{
Connection con;
Statement stmt;
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
con=DriverManager.getConnection ("jdbc:odbc:Student","","");
stmt = con.createStatement();
AccessDB adb = new AccessDB();
// adb.insertRecords(stmt);
adb.getRecords(stmt);
adb.deleteRecords(stmt);
}
public void getRecords(Statement s) {
try {
String sqlstr = "select * from student";
int i = 1;
ResultSet rs =s.executeQuery(sqlstr);
while (rs.next()) {
int num = rs.getInt(1);
String name = rs.getString(2);
int age = rs.getInt(3);
System.out.println("第" + i + "条: " +num + " " + name + " " + age);
i = i + 1;
}
} catch (Exception ex) {
ex.printStackTrace();
}
}
public void insertRecords(Statement s) {
byte[] b = new byte[255];
System.out.println("请输入你要输入的记录,记录的格式为:\"学号 姓名 年龄\"");
try {
System.in.read(b);
} catch (IOException iex) {
iex.printStackTrace();
}
String userInput = new String(b);
userInput = userInput.trim();
String[] ui = userInput.split(" ");
String sqlstr = "insert into student(num, name, age) values('" + Integer.parseInt(ui[0]) + "','" + ui[1] + "', '"
+ Integer.parseInt(ui[2]) +"')";
try {
s.executeQuery(sqlstr);
} catch (Exception ex) {
// ex.printStackTrace();
}
System.out.println("记录已插入,请查看!");
}
public void deleteRecords(Statement s) {
System.out.println("请输入你要删除的记录的学号:");
byte[] b = new byte[255];
try {
System.in.read(b);
} catch (IOException ioex) {
ioex.printStackTrace();
}
String userInput = new String(b);
userInput = userInput.trim();
String[] ui = userInput.split(" ");
for (int i = 0; i < ui.length; i ++) {
String delstr = "delete from student where num = '"+ui[i]+"'";
try {
s.executeQuery(delstr);
} catch (SQLException e) {
e.printStackTrace();
}
}
}
public void updateRecords(Statement s) {
}
} 展开
1个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询