package ghg;
import java.sql.*;
class Create {
public static void main(String args[]) {
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
} catch (ClassNotFoundException ce) {
System.out.println("SQLException:" + ce.getMessage());
}
try {
Connection con = DriverManager
.getConnection("jdbc:odbc:driver={Microsoft Access Driver (*.mdb)};DBQ=D:/Student.mdb");
Statement stmt = con.createStatement();
// 建立数据表
// String sql=
// "create table student(num char(4),name char(6),sex char(4),age int,score int)";
// stmt.executeUpdate(sql);
// 插入5条记录
String sql1 = "insert into student values('9901','张学军','男',18,78)";
stmt.executeUpdate(sql1);
String sql2 = "insert into student values('9901','张学风','女',18,88)";
stmt.executeUpdate(sql2);
String sql3 = "insert into student values('9901','张学红','男',18,56)";
stmt.executeUpdate(sql3);
String sql4 = "insert into student values('9901','学军','女',18,90)";
stmt.executeUpdate(sql4);
String sql5 = "insert into student values('9901','张学','男',18,89)";
stmt.executeUpdate(sql5);
// 更新分数低于60的年龄
String sqlstr = "update student set age=age+1 where score<60";
stmt.executeUpdate(sqlstr);
// 更新分数低于60的更新为60
sqlstr = "update student set score=60 where score<60";
stmt.executeUpdate(sqlstr);
// 查询分数大于80的记录
ResultSet rs = stmt
.executeQuery("select * from student where score>80");
while (rs.next()) {
System.out.println("no= " + rs.getString(1) + "\t" + "name ="
+ rs.getString(2) + "\t" + "age =" + rs.getInt(4)
+ "\t" + "score =" + rs.getFloat(5));
}
stmt.close();
con.close();
rs.close();
} catch (SQLException e) {
System.out.println("SQLException:1 " + e.getMessage());
}
}
}
实验截图如下: