Sy8
package jAVA;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
/*
* 数据库 连接类
*/
/**
* @author
*
*/
public class Dbconn {
private static String driver=”com.mysql.jdbc.Driver”;
private static String url = “jdbc:mysql://localhost:3306/test”;
private static String name = “root”;
private static String pass=”123456″;
public Dbconn() {
}
private static Connection conn=null;
public static Connection getConnection() {
if(conn==null){
try {
Class.forName(driver);
conn= DriverManager.getConnection(url, name, pass);
return conn;
} catch (ClassNotFoundException | SQLException e) {
}
}
return conn;
}
public static void main(String[] args) {
// TODO 自动生成的方法存根
}
}
package
jAVA;
/**
*
@
*
*/
public
class
Student0 {
private
String
id
;
private
String
name
;
private
String
sex
;
private
double
score
;
public
Student0(){
}
public
Student0(String
id
, String
name
, String
sex
,
double
score
) {
super
();
this
.
id
=
id
;
this
.
name
=
name
;
this
.
sex
=
sex
;
this
.
score
=
score
;
}
public
Student0(
int
id2
, String
name2
, String
sex2
,
double
score2
) {
//
TODO
自动生成的构造函数存根
}
public
String getId(
int
i
) {
return
id
;
}
public
String getName() {
return
name
;
}
public
String getSex() {
return
sex
;
}
public
double
getScore() {
return
score
;
}
public
static
void
main(String[]
args
) {
//
TODO
自动生成的方法存根
}
}
package jAVA;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
/*
*
*/
public class StudentDao {
Connection conn = Dbconn.getConnection();
public boolean addStudent(Student0 s) {
try {
String sql = “insert into student value(?,?,?,?)”;
PreparedStatement ppdstmt = conn.prepareStatement(sql);
ppdstmt.setString(1, s.getId(0));
ppdstmt.setString(2, s.getName());
ppdstmt.setString(3, s.getSex());
ppdstmt.setDouble(4, s.getScore());
int ret = ppdstmt.executeUpdate();
return ret>0?true:false;
} catch (SQLException e) {
}
return false;
}
public boolean delStudent(String id){
try{
String sql=”delete from student where id=?”;
PreparedStatement ppdstmt=conn.prepareStatement(sql);
ppdstmt.setString(1, id);
int ret=ppdstmt.executeUpdate();
return ret > 0 ? true:false;
}catch(SQLException e){
}
return false;
}
public boolean updateStudent(Student0 s){
try {
String sql = “update student set name=?,sex=?,score=?,where id=?”;
PreparedStatement ppdstmt = conn.prepareStatement(sql);
ppdstmt.setString(1, s.getName());
ppdstmt.setString(2, s.getSex());
ppdstmt.setDouble(3, s.getScore());
ppdstmt.setString(4, s.getId(0));
int ret = ppdstmt.executeUpdate();
return ret>0?true:false;
} catch (SQLException e) {
}
return false;
}
public List<Student0> queryStudents(){
List<Student0>list=new ArrayList<Student0>();
try{
String sql=”select * from student “;
PreparedStatement ppdstmt=conn.prepareStatement(sql);
ResultSet rs=ppdstmt.executeQuery();
while(rs.next()){
String id=rs.getString(1);
String name=rs.getString(2);
String sex=rs.getString(3);
double score=rs.getDouble(4);
Student0 s=new Student0(id,name,sex,score);
list.add(s);
}
}catch(SQLException e){
}
return list;
}
//按学号查找
public Student0 queryStudentById(int id){
try{
String sql=”select * from student where id = ? “;
PreparedStatement ppdstmt=conn.prepareStatement(sql);
ppdstmt.setLong(1, id);
ResultSet rs=ppdstmt.executeQuery();
while(rs.next()){
String name=rs.getString(2);
String sex=rs.getString(3);
double score=rs.getDouble(4);
Student0 s=new Student0(id,name,sex,score);
return s;
}
}catch(SQLException e){
}
return null;
}
public static void main(String[] args) {
}
public List<Student0> queryAllStudent() {
// TODO 自动生成的方法存根
return null;
}
public boolean updateStudent(int id) {
// TODO 自动生成的方法存根
return false;
}
public boolean resetStudent() {
// TODO 自动生成的方法存根
return false;
}
}package jAVA;
import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.table.DefaultTableModel;
import util.Common;
/**
* @author
*
*/
public class StudentMangerFrm extends JFrame{
private Container pnlMain;
private JButton btnAdd = new JButton(” 录 入 “);
private JButton btnUpdate = new JButton(” 修 改 “);
private JButton btnDelete = new JButton(” 删 除 “);
private JButton btnQuery = new JButton(” 查 询 “);
private JButton btnReset = new JButton(” 重 置 “);
private JButton btnQueryAll = new JButton(” 查询所有 “);
private JTextField txtId;
private JTextField txtName;
private JComboBox comSex;
private JTextField txtScore;
private JTable tblStu;
public StudentMangerFrm(){
initFrm();
initCompoent();
initFunction();
initShow();
}
private void initShow() {
int i=0;
StudentDao dao=new StudentDao();
List<Student0> list=dao.queryAllStudent();
String table[][]=new String[list.size()][5];
for(i=0;i<list.size();i++){
table[i][0]=Integer.toString(i+1);
table[i][1]=Integer.toString(1);
table[i][2]=list.get(i).getName();
table[i][3]=list.get(i).getSex();
table[i][4]=Double.toString(list.get(i).getScore());
}
String colName[]={“序号”,”学号”,”姓名”,”性别”,”成绩”};
DefaultTableModel tm=new DefaultTableModel(table,colName);
tblStu.setModel(tm);
}
public boolean check(){
int id=Integer.parseInt(txtId.getText().trim());
StudentDao dao=new StudentDao();
if(dao.queryStudentById(id)!=null){
return false;
}
return true;
}
private void initFunction() {
btnAdd.addActionListener(new ActionListener(){
public void actionPerfrimed(ActionEvent e){
if(txtId.getText()!=null){
int id=Integer.parseInt(txtId.getText().trim());
String name=txtName.getText().trim();
Object combSex;
double score=Double.parseDouble(txtScore.getText().trim());
String sex;
Student0 s=new Student0(id,name,sex,score);
StudentDao dao=new StudentDao();
boolean ret=dao.addStudent(s);
if(ret==true){
txtId.setText(“”);
txtName.setText(“”);
txtScore.setText(“”);
}
}
}
@Override
public void actionPerformed(ActionEvent arg0) {
// TODO 自动生成的方法存根
}
});
btnDelete.addActionListener(new ActionListener((){
public void actionPerformed(ActionEvent e){
int id=Integer.parseInt(txtId.getText());
StudentDao dao=new StudentDao();
boolean ret=dao.updateStudent(id);
}
});
btnUpdate.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
if(txtId.getText()!=null){
int id=Integer.parseInt(txtId.getText().trim());
String name=txtName.getText().trim();
JComboBox combSex;
String sex=(String) combSex.getSelectedItem();
double score=Double.parseDouble(txtScore.getText().trim());
StudentDao dao=new StudentDao();
boolean ret=dao.updateStudent(id);
}
}
});
btnReset.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
StudentDao dao=new StudentDao();
boolean ret=dao.resetStudent();
}
});
btnQueryAll . addActionListener(new ActionListener(){
public void actionPerformed(int e){
int i=e;
StudentDao dao=new StudentDao();
List<Student0> list=dao.queryAllStudent();
String table[][]=new String[list.size()][5];
for (i=0;i<list.size();i++){
table[i][0]=Integer.toString(i + 1);
table[i][1]=Integer.toString(1);
table[i][2]=list.get(i).getName();
table[i][3]=list.get(i).getSex();
table[i][4]=Double. toString(list.get(i).getScore());
}
String colName[]={ “序号”,”学号”,”姓名”,”性别”,”成绩” };
DefaultTableModel tm=new DefaultTableModel(table, colName);
tblStu. setModel(tm);
}
@Override
public void actionPerformed(ActionEvent arg0) {
// TODO 自动生成的方法存根
}
});
btnQuery.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
StudentDao dao=new StudentDao();
int id=Integer.parseInt(txtId.getText().trim());
Student0 st=dao.queryStudentById(id);
}
});
btnQuery.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
StudentDao dao=new StudentDao();
int id=Integer.parseInt(txtId.getText().trim());
Student0 st=dao.queryStudentById(id);
String table[][]=new String[1][5];
table[0][0]=”1″;
table[0][1]=Integer.toString(1);
table[0][2]=st.getName();
table[0][3]=st.getSex();
table[0][4]=Double.toString(st.getScore());
String colName[]={ “序号”,”学号”,”姓名”,”性别”,”成绩” };
DefaultTableModel tm=new DefaultTableModel(table, colName);
tblStu. setModel(tm);
}
});
}
public void initCompoent(){
//上
JPanel pnlTop = new JPanel();
JLabel lblId = new JLabel(” 学 号: “);
JTextField txtId = new JTextField(20);
JLabel lblName = new JLabel(” 姓 名: “);
JTextField txtName = new JTextField(20);
JLabel lblSex = new JLabel(” 性 别: “);
@SuppressWarnings(“rawtypes”)
JComboBox combSex = new JComboBox(new String[]{” 男 “,” 女 “});
combSex.setSelectedItem((String)” 男”);
JLabel lblScore = new JLabel(” 成 绩: “);
JTextField txtScore = new JTextField(20);
pnlTop.add(lblId);
pnlTop.add(txtId);
pnlTop.add(lblSex);
pnlTop.add(combSex);
pnlTop.add(new JLabel(” “));
pnlTop.add(lblName);
pnlTop.add(txtName);
pnlTop.add(lblScore);
pnlTop.add(txtScore);
pnlTop.add(btnAdd);
pnlTop.add(btnUpdate);
pnlTop.add(btnDelete);
pnlTop.add(btnQuery);
pnlTop.add(btnReset);
pnlTop.add(btnQueryAll);
//下
JPanel pnlBot = new JPanel(new BorderLayout());
JTable tblStu = new JTable(new DefaultTableModel(15,5));
JScrollPane srlStu = new JScrollPane(tblStu);
pnlBot.add(new JLabel(” 学生信息:”),BorderLayout.NORTH);
pnlBot.add(srlStu,BorderLayout.CENTER);
//
//pnlMain.setLayout(new FlowLayout());
pnlMain.setLayout(new GridLayout(2,1));
//pnlMain.setLayout(new BorderLayout());
pnlMain.add(pnlTop);
pnlMain.add(pnlBot);
}
public void initFrm() {
//1.标题
this.setTitle(“学生管理 – 数据库”);
//2.logo
Toolkit toolkit = Toolkit.getDefaultToolkit();
Image image = toolkit.getImage(Common.getClassPath() + “pic/logo.jpg”);
this.setIconImage(image);
//3.大小
this.setSize(660, 480);
//4.居中
Dimension screenSize = toolkit.getScreenSize();
double x = (screenSize.getWidth()-this.getWidth())/2;
double y = (screenSize.getHeight()-this.getHeight())/2;
this.setLocation((int)x, (int)y);
//正常退出
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//关闭最大化
//this.setResizable(false);
pnlMain = this.getContentPane();
}
public static void main(String[] args) {
new StudentMangerFrm().setVisible(true);
}
}