谁能帮忙用java语言编一个学生管理系统,访问access数据库,实现插入,删除,查询
2013-06-08 · 知道合伙人数码行家
关注
展开全部
您好,提问者:
给你一个查询access数据库,然后到处到xls的例子吧。。。
//下面是代码
import java.io.FileWriter;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class Util {
public static void main(String[] args) throws ClassNotFoundException, SQLException, IOException {
String dbUr1="jdbc:odbc:driver={Microsoft Access Driver (*.mdb)};DBQ=C:\\体检.mdb";
String user="";
String password="";
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection c=DriverManager.getConnection(dbUr1,user,password);
Statement s=c.createStatement();
ResultSet r=s.executeQuery("SELECT 病历号,体检日期,姓名,性别,年龄,家庭住址,邮政编码,联系电话 "+"FROM 体检记录表");
FileWriter fw = new FileWriter("C:\\体检统计.xls");
fw.write("病历号\t");
fw.write("体检日期\t");
fw.write("姓名\t");
fw.write("性别\t");
fw.write("年龄\t");
fw.write("家庭住址\t");
fw.write("邮政编码\t");
fw.write("联系电话\t\n");
while(r.next()){
fw.write(r.getString("病历号")+"\t");
fw.write(r.getString("体检日期")+"\t");
fw.write(r.getString("姓名")+"\t");
fw.write(r.getString("性别")+"\t");
fw.write(r.getString("年龄")+"\t");
fw.write(r.getString("家庭住址")+"\t");
fw.write(r.getString("邮政编码")+"\t");
fw.write(r.getString("联系电话")+"\t\n");
}
s.close();
fw.close();
}
}
//把需求中的体检.mdb放入到C:\根目录,下面这是体检.mdb的截图
//程序会自动生成到C:\根目录的xls文件,名称为“体检统计.xls”,运行结果截图
更多追问追答
追问
你给改成学生的白,我给你满意
追答
我不在乎你这点分,随便。。。
展开全部
public class Operation {
private Connection conn = null;
private PreparedStatement stat = null;
private ResultSet re = null;
public Operation(){
conn = new GetConn().Conn(); //调用连接数据库的函数
}
//插入
public int Insert(Student stu){
String sql = "insert into studentInfo values(?,?,?,?,?,?,?,?,?)";
//String sql4 = "update studentInfo set id ='"+id+"' where username ='"+username+"' ";
try {
stat = conn.prepareStatement(sql);
stat.setString(1, stu.getId());
stat.setString(2, stu.getSex());
stat.setString(3, stu.getUsername());
stat.setString(4, stu.getAddress());
stat.setString(5, stu.getMobileTel());
stat.setString(6, stu.getLocalTel());
stat.setString(7, stu.getQq());
stat.setString(8, stu.getJob());
stat.setString(9, stu.getCompany());
int n = stat.executeUpdate();
return n ;
} catch (SQLException e) {
e.printStackTrace();
}finally{
new GetConn().Clo(conn, stat, null);
}
return 0;
}
public int deleteFromId(String id){ //根据ID号来删除一位学生的信息
String sql = "delete from studentInfo where id = ?";
try {
stat = conn.prepareStatement(sql);
//第一个?的值
stat.setString(1, id);
int n = stat.executeUpdate();
return n;
//System.out.println(n);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return 0;
}
//查询
public ArrayList select(){
ArrayList al = new ArrayList();
String sql = "select * from studentInfo";
try {
stat = conn.prepareStatement(sql);
re = stat.executeQuery();
while(re.next()){
Student stu = new Student();
stu.setId(re.getString(1));
stu.setSex(re.getString(2));
stu.setUsername(re.getString(3));
stu.setAddress(re.getString(4));
stu.setMobileTel(re.getString(5));
stu.setLocalTel(re.getString(6));
stu.setQq(re.getString(7));
stu.setJob(re.getString(8));
stu.setCompany(re.getString(9));
al.add(stu);
}
} catch (SQLException e) {
e.printStackTrace();
}finally{
new GetConn().Clo(conn, stat, re);
}
return al;
}
private Connection conn = null;
private PreparedStatement stat = null;
private ResultSet re = null;
public Operation(){
conn = new GetConn().Conn(); //调用连接数据库的函数
}
//插入
public int Insert(Student stu){
String sql = "insert into studentInfo values(?,?,?,?,?,?,?,?,?)";
//String sql4 = "update studentInfo set id ='"+id+"' where username ='"+username+"' ";
try {
stat = conn.prepareStatement(sql);
stat.setString(1, stu.getId());
stat.setString(2, stu.getSex());
stat.setString(3, stu.getUsername());
stat.setString(4, stu.getAddress());
stat.setString(5, stu.getMobileTel());
stat.setString(6, stu.getLocalTel());
stat.setString(7, stu.getQq());
stat.setString(8, stu.getJob());
stat.setString(9, stu.getCompany());
int n = stat.executeUpdate();
return n ;
} catch (SQLException e) {
e.printStackTrace();
}finally{
new GetConn().Clo(conn, stat, null);
}
return 0;
}
public int deleteFromId(String id){ //根据ID号来删除一位学生的信息
String sql = "delete from studentInfo where id = ?";
try {
stat = conn.prepareStatement(sql);
//第一个?的值
stat.setString(1, id);
int n = stat.executeUpdate();
return n;
//System.out.println(n);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return 0;
}
//查询
public ArrayList select(){
ArrayList al = new ArrayList();
String sql = "select * from studentInfo";
try {
stat = conn.prepareStatement(sql);
re = stat.executeQuery();
while(re.next()){
Student stu = new Student();
stu.setId(re.getString(1));
stu.setSex(re.getString(2));
stu.setUsername(re.getString(3));
stu.setAddress(re.getString(4));
stu.setMobileTel(re.getString(5));
stu.setLocalTel(re.getString(6));
stu.setQq(re.getString(7));
stu.setJob(re.getString(8));
stu.setCompany(re.getString(9));
al.add(stu);
}
} catch (SQLException e) {
e.printStackTrace();
}finally{
new GetConn().Clo(conn, stat, re);
}
return al;
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询