|
5Java金币
本帖最后由 danceinstyle 于 2014-4-1 21:44 编辑
本人刚注册账号,悬赏不是很高,请详解!
我本来要用switch语句来获取用户要进行哪一种操作。但是输入要执行哪一个操作后程序直接结束了,不等我输入要增删查更的内容了。运行结果和Access数据库文件都在下面,还有Access数据库文件的截图,请看图
代码如下:
import java.io.IOException;
import java.sql.*;
class getConn {
Connection connection;
Statement statement;
public getConn() {
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
connection = DriverManager.getConnection ("jdbcdbc:Student","","");
statement = connection.createStatement();
} catch (Exception exception) {
exception.printStackTrace();
}
}
}
public class AccessDB {
public static void main(String[] args) throws Exception{
AccessDB adb = new AccessDB();
byte[] operationOptions = new byte[1];
System.out.println("请选择你要执行的操作。1.查询数据库所有的内容 2.插入记录 3.删除记录 4.更新记录");
try {
System.in.read(operationOptions);
} catch (IOException ioex) {
ioex.printStackTrace();
}
String operations = new String(operationOptions);
operations = operations.trim();
int ope = Integer.parseInt(operations);
switch (ope) {
case 1:adb.getRecords();
break;
case 2:adb.insertRecords();
break;
case 3:adb.deleteRecords();
break;
case 4:adb.updateRecords();
break;
}
}
public void getRecords() {
try {
getConn con = new getConn();
Connection c = con.connection;
Statement stmt = con.statement;
String sqlstr = "select * from student";
int i = 1;
ResultSet rs = stmt.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;
}
stmt.close();
c.close();
} catch (Exception ex) {
ex.printStackTrace();
}
}
public void insertRecords() {
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(" ");
try {
getConn con = new getConn();
Connection c = con.connection;
Statement stmt = con.statement;
String sqlstr = "insert into student(num, name, age) values('" + Integer.parseInt(ui[0]) + "','" + ui[1] + "', '"
+ Integer.parseInt(ui[2]) +"')";
stmt.executeUpdate(sqlstr);
stmt.close();
c.close();
} catch (Exception ex) {
ex.printStackTrace();
}
System.out.println("记录已插入,请查看!");
}
public void deleteRecords() {
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(" ");
try {
getConn con = new getConn();
Connection c = con.connection;
Statement stmt = con.statement;
for (int i = 0; i <= ui.length-1; i++) {
stmt.executeUpdate("delete from student where num="+ui);
}
stmt.close();
c.close();
System.out.print("execute ok");
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void updateRecords() {
try {
getConn con = new getConn();
Connection c = con.connection;
Statement stmt = con.statement;
String sqlstr ="update student set name='danceinstyle' where num=2";
stmt.executeUpdate(sqlstr);
stmt.close();
c.close();
System.out.println("更新成功!");
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
Access%E6%95%B0%E6%8D%AE%E5%BA%93%E6%96%87%E4%BB%B6(344.00K)
|
|