package com.dell.users.connimpl;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.Statement;
import javax.swing.JOptionPane;
import com.dell.dbcn.DBConn;
import com.dell.users.bean.Student;
import com.dell.users.bean.Users;
import com.dell.users.impl.IStudent;
public class StudentConnImpl implements IStudent {
String sql = "";
Connection conn;
DBConn dbcn = new DBConn();
Statement st = null;
PreparedStatement ps = null;
ResultSet rs = null;
ResultSetMetaData rsmd = null;
boolean flag = false;
@Override
public void createTb() throws Exception {
sql = "create table student("
+ "stu_xuehao int identity(1,1) primary key,"
+ "stu_name varchar(10) not null,"
+ "stu_sushehao int not null," + "stu_banji int not null,"
+ "stu+xiebie varchar(20) not null,"
+ "stu_zhuanye varchar(20) not null,"
+ "stu_quanfei varchar(2) not null,"
+ "stu_score float not null," + "stu_time data not null" + ")";
}
@Override
public void deleteOne(Student stu) throws Exception {
sql = "delete from student where stu_id=?";
conn = dbcn.getConn2000();
PreparedStatement ps = conn.prepareStatement(sql);
ps.setInt(1, stu.getStu_id());
int a = ps.executeUpdate();
if (a > 0) {
JOptionPane.showMessageDialog(null, "删除信息成功!");
} else {
JOptionPane.showMessageDialog(null, "删除信息成功!");
}
}
@Override
public void insertOne(Student stu) throws Exception {
sql = "insert into student values(?,?,?,?,?,?,?,?,?)";
conn = dbcn.getConn2000();
ps = conn.prepareStatement(sql);
ps.setString(1, stu.getStu_xuehao());
ps.setString(2, stu.getStu_name());
ps.setString(3, stu.getStu_sushehao());
ps.setString(4, stu.getStu_banji());
ps.setString(5, stu.getStu_xibie());
ps.setString(6, stu.getStu_zhuanye());
ps.setString(7, stu.getStu_feiyong());
ps.setFloat(8, stu.getStu_score());
ps.setString(9, stu.getStu_time());
int a = ps.executeUpdate();
if (a > 0) {
JOptionPane.showMessageDialog(null, "插入数据成功!");
} else {
JOptionPane.showMessageDialog(null, "插入数据失败!");
}
}
@Override
public void insertbatch() throws Exception {
// TODO Auto-generated method stub
}
@Override
public boolean select(Student stu) throws Exception {
sql = "select * from student";
conn = dbcn.getConn2000();
st = conn.createStatement();
rs = st.executeQuery(sql);
ResultSetMetaData rsmd = rs.getMetaData();
while (rs.next()) {
System.out.println(rs.getString(1) + "\t" + rs.getString(2) + "\t"
+ rs.getString(3) + "\t" + rs.getString(4) + "\t"
+ rs.getString(5) + "\t" + rs.getString(6) + "\t"
+ rs.getString(7) + "\t" + rs.getString(8)
+ rs.getString(9) + "\t" + rs.getString(10));
}
return false;
}
@Override
public void selectAll() throws Exception {
sql = "select * from student";
conn = dbcn.getConn2000();
st = conn.createStatement();
rs = st.executeQuery(sql);
rsmd = rs.getMetaData();
while (rs.next()) {
Student stu = new Student();
stu.setStu_xuehao(rs.getString(2));
stu.setStu_name(rs.getString(3));
stu.setStu_sushehao(rs.getString(4));
stu.setStu_banji(rs.getString(5));
stu.setStu_xibie(rs.getString(6));
stu.setStu_zhuanye(rs.getString(7));
stu.setStu_feiyong(rs.getString(8));
stu.setStu_score(rs.getFloat(9));
stu.setStu_time(rs.getString(10));
}
}
@Override
public Object[][] selectAll1() throws Exception {
String sql = "select count(*) from student";
conn = dbcn.getConn2000();
st = conn.createStatement();
rs = st.executeQuery(sql);
rs.next();
int a = rs.getInt(1);
// System.out.println(a);
// 得到数据库中值得行数。列固定
Object s[][] = new Object[a][11];
String sql1 = "select * from student";
int j = 0;
Statement st1 = null;
st1 = conn.createStatement();
rs = st1.executeQuery(sql1);
while (rs.next()) {
s[j][0] = rs.getInt(1);
s[j][1] = rs.getString(2);
s[j][2] = rs.getString(3);
s[j][3] = rs.getString(4);
s[j][4] = rs.getString(5);
s[j][5] = rs.getString(6);
s[j][6] = rs.getString(7);
s[j][7] = rs.getString(8);
s[j][8] = rs.getFloat(9);
s[j][9] = rs.getString(10);
j++;
}
return s;
}
@Override
public void selectId(Student stu) throws Exception {
sql = "use Users select * from student where stu_id=?";
conn = dbcn.getConn2000();
ps = conn.prepareStatement(sql);
ps.setInt(1, stu.getStu_id());
rs = ps.executeQuery();
while (rs.next()) {
stu.setStu_xuehao(rs.getString(2));
stu.setStu_name(rs.getString(3));
stu.setStu_sushehao(rs.getString(4));
stu.setStu_banji(rs.getString(5));
stu.setStu_xibie(rs.getString(6));
stu.setStu_zhuanye(rs.getString(7));
stu.setStu_feiyong(rs.getString(8));
stu.setStu_score(rs.getFloat(9));
stu.setStu_time(rs.getString(10));
System.out.println(rs.getInt(1) + "\t" + rs.getString(2) + "\t"
+ rs.getString(3) + "\t" + rs.getString(4) + "\t"
+ rs.getString(5) + "\t" + rs.getString(6) + "\t"
+ rs.getString(7) + "\t" + rs.getString(8) + "\t"
+ rs.getString(9) + "\t" + rs.getString(10));
}
}
public boolean updateOne(Student stu) throws Exception {
sql = "update student set stu_xuehao=?,stu_name=?,stu_banji=?,stu_sushehao=?,stu_xibie=?,stu_zhuanye=?,stu_qianfei=?,stu_score=?,stu_time=? where stu_id=?";
conn = dbcn.getConn2000();
ps = conn.prepareStatement(sql);
ps.setString(1, stu.getStu_xuehao());
ps.setString(2, stu.getStu_name());
ps.setString(3, stu.getStu_banji());
ps.setString(4, stu.getStu_sushehao());
ps.setString(5, stu.getStu_xibie());
ps.setString(6, stu.getStu_zhuanye());
ps.setString(7, stu.getStu_feiyong());
ps.setFloat(8, stu.getStu_score());
ps.setString(9, stu.getStu_time());
ps.setInt(10, stu.getStu_id());
int a = ps.executeUpdate();
if (a > 0) {
flag = true;
}
return flag;
}
// 编号精确查询
@Override
public Object[][] selectjqId(Student stu) throws Exception {
String sql = "select count(*) from student";
conn = dbcn.getConn2000();
st = conn.createStatement();
rs = st.executeQuery(sql);
rs.next();
int a = rs.getInt(1);
Object s[][] = new Object[a][11];
String sql1 = "select * from student where stu_id=" + stu.getStu_id();
int j = 0;
Statement st1 = null;
st1 = conn.createStatement();
rs = st1.executeQuery(sql1);
while (rs.next()) {
s[j][0] = rs.getInt(1);
s[j][1] = rs.getString(2);
s[j][2] = rs.getString(3);
s[j][3] = rs.getString(4);
s[j][4] = rs.getString(5);
s[j][5] = rs.getString(6);
s[j][6] = rs.getString(7);
s[j][7] = rs.getString(8);
s[j][8] = rs.getFloat(9);
s[j][9] = rs.getString(10);
j++;
}
return s;
}
// 学号精确查询
@Override
public Object[][] selectjqxh(Student stu) throws Exception {
String sql = "select count(*) from student";
conn = dbcn.getConn2000();
st = conn.createStatement();
rs = st.executeQuery(sql);
rs.next();
int a = rs.getInt(1);
Object s[][] = new Object[a][11];
String sql1 = "select * from student where stu_xuehao='"
+ stu.getStu_xuehao()+"'";
int j = 0;
Statement st1 = null;
st1 = conn.createStatement();
rs = st1.executeQuery(sql1);
while (rs.next()) {
s[j][0] = rs.getInt(1);
s[j][1] = rs.getString(2);
s[j][2] = rs.getString(3);
s[j][3] = rs.getString(4);
s[j][4] = rs.getString(5);
s[j][5] = rs.getString(6);
s[j][6] = rs.getString(7);
s[j][7] = rs.getString(8);
s[j][8] = rs.getFloat(9);
s[j][9] = rs.getString(10);
j++;
}
return s;
}
// 姓名精确查询
@Override
public Object[][] selectjqname(Student stu) throws Exception {
String sql = "select count(*) from student where stu
评论1