项目说明
这是一个学生信息管理系统,可以向数据库中录入学生信息(学号,姓名,成绩),并通过UI界面进行增、删、改、查操作,适合初学玩java的同学作为第一个项目进行实战。
选用的数据库为java自带的derby,如果要连其他数据库也是类似的。
代码总共300行,采用的是MVC模式(模型—视图—控制器)。
下面是运行截图
项目实现
组织形式
有三个类,其中process对应的是MVC中的controller,window对应的是MVC中的view,derbey数据库对应的史MVC中的model
?
MainClass类代码
package demo2;
public class MainClass
{
public static void main(String[] args)
{
Window window = new Window();
}
}
process类代码
package demo2;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
public class process
{
Connection con;
Statement sql;
ResultSet rs;
//初始化,连接数据库
process()
{
try
{
Class.forName("org.apache.derby.jdbc.EmbeddedDriver");//加载驱动
con = DriverManager.getConnection("jdbc:derby:sqm;create=true");
sql = con.createStatement();
rs = con.getMetaData().getTables(null, null, "students", null );//判断表格是否存在
if(rs.next())//若表格不存在,则建立新表
{
sql.execute("create table students(id varchar(20) primary key not null, name varchar(20), score varchar(20))");//创建表
}
}
catch(Exception e)
{
System.out.println(e);
}
}
void insert(String a, String b, String c)//插入学生信息
{
try
{
sql.execute("insert into students values('" + a + "','" + b + "','" + c + "')");
}
catch(Exception e)
{
System.out.println("insert " + e);
}
}
String search(String s)//查询功能
{
String s1 = new String();
try
{
rs = sql.executeQuery("select * from students where id = '" + s + "'");
if(rs.next())
s1 += "找到该学生 " + "学号为 " + rs.getString(1) + "姓名为 " + rs.getString(2) + "成绩为 " + rs.getString(3);
else s1 = "未找到该学生";
}
catch(Exception e)
{
System.out.println("search " + e);
}
return s1;
}
String fix(String s, String newscore)//修改功能
{
String s1 = new String();
try
{
rs = sql.executeQuery("select * from students where id = '" + s + "'");//先看有没有该学生
if(rs.next())
{
sql.execute("update students set score = '" + newscore + "' where id = '" + s + "'");//修改操作
rs = sql.executeQuery("select * from students where id = '" + s + "'");
rs.next();
s1 += "成功修改该学生 " + "学号为 " + rs.getString(1) + "姓名为 " + rs.getString(2) + "成绩为 " + rs.getString(3);
}
else s1 = "未找到该学生";
}
catch(Exception e)
{
System.out.println("fix " + e);
}
return s1;
}
String delete(String s)//删除功能
{
try
{
sql.execute("delete from students where id = '" + s + "'");
}
catch(Exception e)
{
System.out.println("delete " + e);
}
return "成功删除该学生";
}
String selectall()//查询全表
{
String s = new String();
try
{
rs = sql.executeQuery("select * from students");
while(rs.next())
{
s += "学号为 " + rs.getString(1) + "姓名为 " + rs.getString(2) + "成绩为 " + rs.getString(3) + "\n";
}
}
catch(Exception e)
{
System.out.println("selectall " + e);
}
return s;
}
}
?Window类代码
package demo2;
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
public class Window extends JFrame
{
JTextField textName, textScore, textNumber, number1, number2, number3, number4;//读取学号,姓名,成绩
JTextArea showArea;//显示程序输出
process student;//业务处理
JButton insertStudent, searchScore, fixStudent, deleteStudent,selectall;//插入学生信息,查询,修改,删除,查询所有学生信息
Window()//构造函数
{
init();
setBounds(100,100,700,600);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}
void init()//初始化函数
{
student = new process();
showArea = new JTextArea();
//插入功能
JPanel pNorth = new JPanel();
textName = new JTextField(10);
textScore = new JTextField(10);
textNumber = new JTextField(10);
pNorth.add(new JLabel("插入学生信息"));
pNorth.add(new JLabel("学号"));
pNorth.add(textName);
pNorth.add(new JLabel("姓名"));
pNorth.add(textNumber);
pNorth.add(new JLabel("成绩"));
pNorth.add(textScore);
insertStudent = new JButton("插入学生信息");
insertStudent.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
student.insert(textName.getText(), textNumber.getText(), textScore.getText());
showArea.append(student.search(textNumber.getText()) + "\n");
}
}
);
pNorth.add(insertStudent);
//查询功能
JPanel pEast = new JPanel();
pEast.add(new JLabel("成绩查询"));
pEast.add(new JLabel("请输入学号"));
number1 = new JTextField(10);
pEast.add(number1);
searchScore = new JButton("成绩查询");
searchScore.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
showArea.append(student.search(number1.getText()) + "\n");
}
}
);
pEast.add(searchScore);
//修改功能
JPanel pSouth = new JPanel();
pSouth.add(new JLabel("修改信息"));
pSouth.add(new JLabel("请输入学号"));
number2 = new JTextField(10);
pSouth.add(number2);
pSouth.add(new JLabel("请输入修改后成绩"));
number3 = new JTextField(10);
pSouth.add(number3);
fixStudent = new JButton("修改成绩");
fixStudent.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
showArea.append(student.fix(number2.getText(),number3.getText()) + "\n");
}
}
);
pSouth.add(fixStudent);
//删除功能
JPanel pWest = new JPanel();
pWest.add(new JLabel("删除学生"));
pWest.add(new JLabel("请输入学号"));
number4 = new JTextField(10);
pWest.add(number4);
deleteStudent = new JButton("删除学生");
deleteStudent.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
showArea.append(student.delete(number4.getText()) + "\n");
}
}
);
pWest.add(deleteStudent);
//查询全表
selectall = new JButton("查询全表");
selectall.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
showArea.append(student.selectall() + "\n");
}
}
);
//盒式布局
Box box = Box.createVerticalBox();
box.add(pNorth);
box.add(pSouth);
box.add(pEast);
box.add(pWest);
box.add(selectall);
add(box,BorderLayout.NORTH);
add(new JScrollPane(showArea));
}
}
|