Java课程设计——资源管理系统的设计与实现
课程设计构思
从一开始,我就没定下我要写些什么,就一个想法,先写吧,是啥就是啥,但总体方向还是选择的管理系统,可能是大一关于数据结构课程的设计心存遗憾,就还是想做一个稍微入眼那么一丢丢的系统吧
然后从Java入门开始,我就一直在使用CSDN的嘛,所以我也想做一个类似的界面出来,随后想了想,觉得以我目前的水平来说,有点异想天开,害,说到底,还是学得不扎实,争取在这个暑假进行完查漏补缺
所以我退而求其次,选择做一个超级简陋的资源管理系统,并给它取名为"识纠"——学习知识,纠正其身.本来还想做个什么留言板之类,但叠在一起感觉有些许不伦不类的味道,日后进行完善
今天就要答辩了,祝我顺利!
课程设计内容
-
实现友好的用户注册登录界面
① 注册登录不为空
② 记住密码后可以直接跳过密码输入过程
③ 登录成功会直接展示登陆后的信息
④ 登录失败会有相应的提示信息 -
按不同文件进行分类
①在数据库中创建对应表格
②在图形化界面展示文件的缩略图 -
实现资源的上传下载
① 增加文件- 选择要添加数据的文件类型
- 输入被存储文件的文件名
- 从本地文件夹选择文件
- 添加文件至数据库
② 删除文件
- 选择要删除数据的文件类型
- 输入被删除文件的文件名
- 从数据库中删除文件
③ 修改文件
- 选择要更改数据的文件类型
- 输入被更改文件的文件名
- 输入更改后的文件名
- 从本地文件夹选择文件
- 在数据库中修改文件
④查找文件
- 选择要查找数据的文件类型
- 输入要查找文件的文件名
- 将查找到的文件内容展示在面板上
大致流程图
详细设计
- 先创建一个窗口,做一个总体布局
- 在窗口中添加面板组件
public static void showGUI(){
//定义一个图形化界面
//识纠:不断学习新知识,也不断在此过程纠正完善自己
jFrame.setSize(600,525);
jFrame.setLocation(700,300);
jFrame.setUndecorated(true); //设置JFrame窗口边框不显示
jFrame.setResizable(false); //禁止改变窗口大小
//进行布局分配
BorderLayout bl = new BorderLayout();
jFrame.setLayout(bl);
//设置顶部面板
//创建并加入顶部面板
JPanel p_north = CreatePanel.CreateNorthPanel(jFrame);
jFrame.add(p_north,BorderLayout.PAGE_START);
//创建并加入左侧面板
JPanel p_west = CreatePanel.CreateWestPanel(jFrame);
jFrame.add(p_west,BorderLayout.WEST);
//创建并加入中部面板
JPanel p_center = CreatePanel.CreateCenterPanel(jFrame);
jFrame.add(p_center,BorderLayout.CENTER);
//创建并加入底部面板
JPanel p_south = CreatePanel.CreateSouthPanel(jFrame);
jFrame.add(p_south,BorderLayout.PAGE_END);
jFrame.setVisible(true);
- 顶部面板
就只是一个背景图片,是还在想要做什么的时候随手画的图
//顶部面板区域
public static JPanel CreateNorthPanel(JFrame jFrame){
//1.创建一个顶部面板JPanel
JPanel panel = new JPanel();
//取消面板内默认布局
panel.setLayout(null);
//设置顶部面板尺寸
panel.setPreferredSize(new Dimension(0,300));
//向顶部面板添加背景图片
ImageIcon img = new ImageIcon("src/image/img.png");
JLabel background = new JLabel(img);
//设置背景图片的位置及尺寸
background.setBounds(0,0,600,img.getIconHeight());
panel.add(background);
//在顶部面板右上角添加一个退出按钮
JButton out = new JButton(new ImageIcon("src/image/img_1.png"));
out.setBounds(570,0,30,30);
out.setRolloverIcon(new ImageIcon("src/image/img_2.png"));
//取消按钮边框效果
out.setBorderPainted(false);
out.addActionListener(e->jFrame.dispose());
panel.add(out);
return panel;
}
- 左侧面板
在左侧位置放置一个头像,本来是想实现通过中部面板的下拉框内容的改变而更换头像,目前没有实现,需要再找时间进行完善(有没有大佬指点指点呀哈哈^ _ ^~~)
//左侧面板区域
public static JPanel CreateWestPanel(JFrame jFrame){
JPanel panel = new JPanel();
panel.setLayout(null);
panel.setPreferredSize(new Dimension(200,0));
ImageIcon imageIcon = new ImageIcon("src/image/circle-cropped.png");
JLabel bg = new JLabel(imageIcon);
bg.setBounds(50,0,120,150);
panel.add(bg);
return panel;
}
- 中部面板
在此面板中输入用户名和密码,进行验证登录
如果已注册或已存在相应的账号密码,将会直接进入下一个界面,否则会提示错误信息,如果输入内容为空,将会提示完善信息.
//中部面板区域
public static JPanel CreateCenterPanel(JFrame jFrame){
JPanel panel = new JPanel();
panel.setLayout(null); //取消面板内默认布局
//创建一个下拉框组件,并初始化账号
String str[]={"cici","lili","bobo"};
JComboBox<Object>jc = new JComboBox<>(str);
//获取下拉框选择的选项
String string = (String) jc.getSelectedItem();
/*ChangeBudda c =new ChangeBudda();
c.setName(string);*/
JLabel jtext1 = new JLabel("用户名");
jtext1.setBounds(0,30,85,25);
jtext1.setFont(new Font("黑体",0,20));
panel.add(jtext1);
JLabel jtext2 = new JLabel("密 码");
jtext2.setBounds(0,80,85,25);
jtext2.setFont(new Font("黑体",0,20));
panel.add(jtext2);
panel.add(jc);
//设置下拉框可编辑
jc.setEditable(true);
jc.setBounds(85,30,200,25);
//设置下拉框内容字体
jc.setFont(new Font("Calibri",0,19));
//创建一个JPasswordField密码框组件
JPasswordField jp= new JPasswordField();
//设置密码框面板为流式布局
jp.setLayout(new FlowLayout(FlowLayout.RIGHT,0,0));
jp.setBounds(85,80,200,25);
jp.setPreferredSize(new Dimension(200,25));
//输入密码后可以通过回车键进行登录,回车键的keycode就是10
jp.addKeyListener(new KeyAdapter() {
@Override
public void keyPressed(KeyEvent e) {
if (10==e.getKeyCode()){
jp.addActionListener(listener);
}
}
});
panel.add(jp);
//创建两个JCheckBox多选框组件
JCheckBox jcb1 = new JCheckBox("记住密码");
jcb1.addActionListener(e -> {
//从数据库中获取密码并自动填充
String s = new LoginDao().fillPassword((String) jc.getSelectedItem());
jp.setText(s);
});
JCheckBox jcb2 = new JCheckBox("自动登录");
jcb1.setFocusPainted(false);
jcb2.setFocusPainted(false);
jcb1.setFont(new Font("宋体",0,15));
jcb2.setFont(new Font("宋体",0,15));
jcb1.setBounds(90,120,100,30);
jcb2.setBounds(190,120,100,30);
panel.add(jcb1);
panel.add(jcb2);
listener = new LoginListener(jc,jp,jFrame);
return panel;
}
进行密码的校验
public void actionPerformed(ActionEvent e){
//获取登录的账号和密码
String name = (String) jco.getSelectedItem();
String pwd = new String(jpa.getPassword());
//创建LoginDao对象
LoginDao loginDao = new LoginDao();
//查询登录用户,如果有此用户并且密码正确则返回true
Boolean b= false;
try {
//从数据库中查找指定的账号密码
b=loginDao.findUser(name,pwd);
//判断输入的账号和密码是否正确
if (b){
//账号正确,先关闭当前JFrame登录窗口
jf.dispose();
LoginSuccess.show(name);
}else{
//帐号或密码输入错误,弹出提示信息
JOptionPane.showMessageDialog(null,"你输入的账户名或密码不正确,请重新输入");
jpa.setText("");
}
} catch (SQLException e1) {
e1.printStackTrace();
}
}
从数据库中查找指定的账号密码
//查询用户
public Boolean findUser(String member,String pwd) throws SQLException {
try {
//1.加载数据库驱动
Class.forName("com.mysql.jdbc.Driver");
String url = "jdbc:mysql://localhost:3306/hncu";
String username="root";
String password="1020";
//2.获取数据库连接
conn = DriverManager.getConnection(url,username,password);
//3.定义需要执行的SQL
String sql = "select * from user "+
"where username= ? " +
"and password= ?";
//4.创建PreparedStatement对象
ps = conn.prepareStatement(sql);
ps.setString(1,member);
ps.setString(2,pwd);
//5.执行SQL并将获取的数据信息存放在ResultSet中
rs = ps.executeQuery();
//6.如果查询的结果集中有超过一条的记录,则登陆成功
if (rs.next()){
return true;
}
}catch (Exception e){
e.printStackTrace();
}finally {
//6.释放资源
if (rs!=null){
rs.close();
}
if (ps!=null){
ps.close();
}
if (conn!=null){
conn.close();
}
}
return false;
}
- 底部面板
在此面板中可以直接登录,能够进行账号注册
//底部面板区域
public static JPanel CreateSouthPanel(JFrame jFrame){
//创建一个底部面板
JPanel panel = new JPanel();
panel.setLayout(null); //取消面板内默认布局
panel.setPreferredSize(new Dimension(0,80));
JButton jregister = new JButton("注 册",new ImageIcon("src/image/img_5.png"));
jregister.setPreferredSize(new Dimension(80,50));
jregister.setRolloverIcon(new ImageIcon("src/image/img_9.png"));
jregister.setFont(new Font("黑体",0,16));
jregister.setBounds(0,500,25,25);
jregister.setHorizontalTextPosition(SwingConstants.CENTER);
jregister.setBorderPainted(false);
//设置不显示按钮区域
jregister.setContentAreaFilled(false);
jregister.setBounds(0,55,80,25);
jregister.setToolTipText("注册账号");
jregister.addActionListener(e -> {
new LoginGUI();
});
//创建底部中间登录图标组件
ImageIcon image = new ImageIcon("src/image/img_5.png");
JButton jb= new JButton("登 录",image);
jb.setFont(new Font("黑体",1,25));
jb.setBounds(200,0,290,50);
//将文字放在图片中间
jb.setHorizontalTextPosition(SwingConstants.CENTER);
jb.setFocusPainted(false);
jb.setContentAreaFilled(false);
jb.setBorderPainted(false);
jb.setRolloverIcon(new ImageIcon("src/image/img_6.png"));
jb.addActionListener(listener);
JButton bt = new JButton(new ImageIcon("src/image/img_8.png"));
bt.setBounds(540,55,80,25);
bt.setFocusPainted(false);
bt.setContentAreaFilled(false);
bt.setBorderPainted(false);
bt.setRolloverIcon(new ImageIcon("src/image/img_9.png"));
bt.setToolTipText("二维码登录");
panel.add(jregister);
panel.add(jb);
panel.add(bt);
return panel;
}
账号注册界面
public class LoginGUI {
public LoginGUI(){
JFrame frame = new JFrame("注册窗口");
frame.setSize(600,525);
frame.setLayout(new BorderLayout());
frame.setLocation(700,300);
frame.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
frame.setResizable(false); //禁止改变窗口大小
JPanel panel1 = new JPanel();
panel1.setLayout(null);
panel1.setBounds(0,0,600,525);
JLabel title = new JLabel("注册信息填写");
JLabel username = new JLabel( "用 户 名");
JLabel password = new JLabel( "设 置 密 码 ");
JLabel dpassword = new JLabel("重复确认密码");
title.setFont(new Font("黑体",0,25));
username.setFont(new Font("黑体",0,20));
password.setFont(new Font("黑体",0,20));
dpassword.setFont(new Font("黑体",0,20));
title.setBounds(220,20,200,50);
username.setBounds(120,120,120,30);
password.setBounds(120,180,120,30);
dpassword.setBounds(120,240,120,30);
JTextField tusername = new JTextField();
JPasswordField tpassword = new JPasswordField();
JPasswordField tdpassword = new JPasswordField();
tusername.setBounds(300,120,200,30);
tpassword.setBounds(300,180,200,30);
tdpassword.setBounds(300,240,200,30);
tusername.setFont(new Font("黑体",0,20));
tpassword.setFont(new Font("Ink Free",0,20));
tdpassword.setFont(new Font("Ink Free",0,20));
JButton btn = new JButton("注册");
btn.setFont(new Font("黑体",0,25));
btn.setBounds(200,350,200,50);
btn.addActionListener(e1 -> {
String member = tusername.getText();
String pwd = new String(tpassword.getPassword());
String tpwd = new String(tdpassword.getPassword());
if (member.equals("")||member==null||pwd.equals("")||pwd==null||tpwd.equals("")||tpwd==null){
JOptionPane.showMessageDialog(null,"输入内容不能为空,请完善注册信息");
}else {
if(!tpwd.equals(pwd)){
JOptionPane.showMessageDialog(null,"两次密码输入不一致,请重新输入");
tdpassword.setText("");
}else{
try {
Boolean b = new LoginDao().addUser(member,pwd);
if (b){
JOptionPane.showMessageDialog(null,"注册成功");
frame.dispose();
}else{
JOptionPane.showMessageDialog(null,"注册失败,请重新注册");
}
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}}
});
panel1.add(title);
panel1.add(username);
panel1.add(password);
panel1.add(dpassword);
panel1.add(tusername);
panel1.add(tpassword);
panel1.add(tdpassword);
panel1.add(btn);
frame.add(panel1);
frame.setVisible(true);
}
}
7.登录成功界面
账号密码都通过验证后,就可以进行登录界面的操作
public class LoginSuccess {
public static void show(String username){
JFrame f = new JFrame("欢迎来到识纠");
f.setLayout(new BorderLayout());
f.setSize(1720,1000);
f.setLocation(0,0);
f.setVisible(true);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel_top= CreatePanel_f.CreateTopPanel();
f.add(panel_top,BorderLayout.PAGE_START);
JPanel panel_left = CreatePanel_f.CreateLeftPanel(username,f);
f.add(panel_left,BorderLayout.WEST);
JPanel panel_bottom = CreatePanel_f.CreateBottomPanel();
f.add(panel_bottom,BorderLayout.PAGE_END);
}
}
顶部面板中放置了背景图片,中部面板展示缩略图,
功能按钮设置在登录成功界面的底部
public class CreatePanel_f {
//设置顶部面板
public static JPanel CreateTopPanel(){
JPanel panel = new JPanel();
panel.setLayout(null);
//设置此组件的首选大小
panel.setPreferredSize(new Dimension(0,500));
JLabel bg = new JLabel(new ImageIcon("src/image/img_11.png"));
bg.setBounds(0,-30,1920,500);
//将顶部背景图片标签添加到顶部面板中
panel.add(bg);
return panel;
}
//设置中部面板
public static JPanel CreateLeftPanel(String username,JFrame jf){
JPanel panel = new JPanel();
panel.setLayout(null); //取消面板内默认布局
panel.setPreferredSize(new Dimension(1800,0));
//创建一个文本标签表欢迎语
JLabel text = new JLabel("欢迎来到识纠,"+username+
",希望你能在这收获到更多!");
//设置显示位置及尺寸
text.setBounds(340,0,410,50);
text.setFont(new Font("黑体",0,20));
//创建一个图片标签表头像
JLabel buddha = new JLabel(new ImageIcon("src/image/circle-cropped (2).png"));
buddha.setBounds(340,50,120,120);
//用户名
JButton name = new JButton(username);
name.setFont(new Font("黑体",0,25));
name.setBounds(340,170,120,25);
name.setBorderPainted(false);
name.setContentAreaFilled(false);
name.addActionListener(e -> {
});
JLabel title= new JLabel("个人资源库");
title.setFont(new Font("楷体",0,35));
title.setBounds(480,20,220,145);
JButton jb1 = new JButton("我的图库",new ImageIcon("src/image/img_10.png"));
JButton jb2 = new JButton("我的文档",new ImageIcon("src/image/img_10.png"));
JButton jb3 = new JButton("我的收藏",new ImageIcon("src/image/img_10.png"));
jb1.setFont(new Font("华文行楷",0,25));
jb2.setFont(new Font("华文行楷",0,25));
jb3.setFont(new Font("华文行楷",0,25));
jb1.setBounds(480,120,600,45);
jb2.setBounds(480,165,600,45);
jb3.setBounds(480,210,600,45);
jb1.setFocusPainted(false);
jb2.setFocusPainted(false);
jb3.setFocusPainted(false);
//将文字居中显示在图片上
jb1.setHorizontalTextPosition(SwingConstants.CENTER);
jb2.setHorizontalTextPosition(SwingConstants.CENTER);
jb3.setHorizontalTextPosition(SwingConstants.CENTER);
//设置为透明按钮,仅图标按钮
jb1.setContentAreaFilled(false);
jb2.setContentAreaFilled(false);
jb3.setContentAreaFilled(false);
//取消边框
jb1.setBorderPainted(false);
jb2.setBorderPainted(false);
jb3.setBorderPainted(false);
//设置鼠标停放时的图标
jb1.setRolloverIcon(new ImageIcon("src/image/img_9.png"));
jb2.setRolloverIcon(new ImageIcon("src/image/img_9.png"));
jb3.setRolloverIcon(new ImageIcon("src/image/img_9.png"));
//展示缩略图
jb1.addActionListener(e -> {/*
JFrame frame = new JFrame();
frame.setSize(500,600);
frame.setLocation(500,600);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);*/
String ftype = "pictures";
JScrollPane pane = new ShowDao().Show(ftype);
pane.setBounds(1085,0,507,300);
panel.add(pane);
});
jb2.addActionListener(e->{
String ftype = "files";
JScrollPane pane = new ShowDao().Show(ftype);
pane.setBounds(1085,0,507,300);
panel.add(pane);
});
jb3.addActionListener(e->{
String ftype = "favors";
JScrollPane pane = new ShowDao().Show(ftype);
pane.setBounds(1085,0,507,300);
panel.add(pane);
});
panel.add(text);
panel.add(buddha);
panel.add(name);
panel.add(title);
panel.add(jb1);
panel.add(jb2);
panel.add(jb3);
return panel;
}
//设置底部面板
public static JPanel CreateBottomPanel(){
JPanel panel = new JPanel();
panel.setLayout(null);
panel.setPreferredSize(new Dimension(1200,150));
JButton addbtn = new JButton("增加");
JButton delbtn = new JButton("删除");
JButton updatebtn = new JButton("修改");
JButton findbtn = new JButton("查找");
addbtn.setFont(new Font( "黑体",0,15));
delbtn.setFont(new Font( "黑体",0,15));
updatebtn.setFont(new Font( "黑体",0,15));
findbtn.setFont(new Font( "黑体",0,15));
addbtn.setBounds(355,10,200,50);
delbtn.setBounds(700,10,200,50);
updatebtn.setBounds(1045,10,200,50);
findbtn.setBounds(1390,10,200,50);
panel.add(addbtn);
panel.add(delbtn);
panel.add(updatebtn);
panel.add(findbtn);
addbtn.addActionListener(e -> {
JFrame addf = new JFrame("添加操作窗口");
addf.setSize(900,700);
addf.setLocation(600,200);
addf.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
addf.setVisible(true);
addf.setResizable(false);
String sname = "增添";
Fun_CRUD fun_crud = new Fun_CRUD();
JPanel panel1 = fun_crud.all(sname);
panel1 = fun_crud.add(panel1);
addf.add(panel1,BorderLayout.PAGE_START);
});
delbtn.addActionListener(e -> {
JFrame delf = new JFrame("删除操作窗口");
delf.setLayout(new BorderLayout());
delf.setSize(900,700);
delf.setLocation(600,200);
delf.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
delf.setVisible(true);
delf.setResizable(false);
String s= "删除";
Fun_CRUD fun_crud = new Fun_CRUD();
JPanel panel2 = fun_crud.all(s);
panel2 = fun_crud.delete(panel2);
delf.add(panel2,BorderLayout.PAGE_START);
});
updatebtn.addActionListener(e -> {
JFrame upf = new JFrame("修改操作窗口");
upf.setLayout(new BorderLayout());
upf.setSize(900,700);
upf.setLocation(600,200);
upf.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
upf.setVisible(true);
upf.setResizable(false);
String sname = "修改";
Fun_CRUD fun_crud = new Fun_CRUD();
JPanel panel1 = fun_crud.all(sname);
panel1 = fun_crud.update(panel1);
upf.add(panel1,BorderLayout.PAGE_START);
});
findbtn.addActionListener(e -> {
JFrame findf = new JFrame("查找操作窗口");
findf.setLayout(new BorderLayout());
findf.setSize(900,700);
findf.setLocation(600,200);
findf.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
findf.setVisible(true);
findf.setResizable(false);
String sname = "查找";
Fun_CRUD fun_crud = new Fun_CRUD();
JPanel panel1 = fun_crud.all(sname);
panel1 = fun_crud.find(panel1);
findf.add(panel1,BorderLayout.PAGE_START);
});
return panel;
}
}
缩略图的展示:即从数据库中读取表并显示到面板中
public JScrollPane Show(String ftype){
try {
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection(url,name,password);
String sql= "select * from "+ftype;
ps=conn.prepareStatement(sql);
rs=ps.executeQuery();
GetRow g = new GetRow();
int a= g.getRow(ftype);
String[] colNames = {"序号","名称","文档-"+ftype};
Object[][] rowData =new Object[a][3];
int count = 0;
while (rs.next()){
rowData[count][0]=rs.getString("id");
rowData[count][1]=rs.getString("f_name");
rowData[count][2]=rs.getObject("f_data");
count++;
}
JTable table = new JTable(rowData,colNames);
table.setBounds(1090,-70,100,100);
table.setRowHeight(50);
scrollPane = new JScrollPane(table);
scrollPane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);
scrollPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
return scrollPane;
} catch (Exception e) {
e.printStackTrace();
}finally {
if (rs!=null){
try {
rs.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
if (ps!=null){
try {
ps.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
if (conn!=null){
try {
conn.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
}
return scrollPane;
}
1.添加操作窗口:输入添加的文件名,文件路径
public static JPanel add(JPanel panel){
JLabel label = new JLabel(" 请输入文件名");
JTextField inputText = new JTextField(20);
label.setFont(new Font("宋体",0,20));
label.setBounds(30,80,150,30);
inputText.setFont(new Font("华文行楷",0,20));
inputText.setBounds(200,80,300,30);
JButton btn = new JButton("选择文件");
btn.setFont(new Font("宋体",0,20));
btn.setBounds(30,130,150,30);
JTextField urlText = new JTextField();
urlText.setFont(new Font("楷体",0,18));
urlText.setBounds(200,130,660,30);
JFileChooser jfc=new JFileChooser();
btn.addActionListener(e -> {
jfc.showOpenDialog(jfc);
File file = jfc.getSelectedFile();
String s = file.getAbsolutePath();
urlText.setText(s);
});
JButton addtobase = new JButton("添加该文件到数据库中");
addtobase.setFont(new Font("宋体",0,20));
addtobase.setBounds(160,200,560,35);
addtobase.addActionListener(e -> {
String input_text = inputText.getText();
String url_text = urlText.getText();
ShowDao showDao = new ShowDao();
if (input_text!=null && !input_text.equals("") && url_text!=null && !url_text.equals("")) {
Boolean b = showDao.add(item, input_text, url_text);
if (b) {
ShowDao_inner showDao_inner = new ShowDao_inner();
JScrollPane scrollPane = showDao_inner.Show(item);
scrollPane.setBounds(160, 300, 500, 300);
panel.add(scrollPane);
} else {
JOptionPane.showMessageDialog(null, "添加失败");
}
}else{
JOptionPane.showMessageDialog(null,"请完善信息再进行添加操作");
}
});
panel.add(addtobase);
panel.add(btn);
panel.add(urlText);
panel.add(jfc);
panel.add(label);
panel.add(inputText);
return panel;
}
添加到数据库中
public Boolean add(String ftype ,String inputText,String urlText){
try {
Class.forName("com.mysql.jdbc.Driver");
conn=DriverManager.getConnection(url,name,password);
String s = "insert into "+ftype+"(f_name,f_data) values(?,?)";
System.out.println(s);
File file = new File(""+urlText);
FileReader fr = new FileReader(file);
ps=conn.prepareStatement(s);
ps.setString(1,inputText);
ps.setCharacterStream(2,fr,(int)file.length());
ps.executeUpdate();
System.out.println("成功写入数据");
return true;
} catch (Exception e) {
e.printStackTrace();
}finally {
if (rs!=null){
try {
rs.close();
} catch (Exception e) {
e.printStackTrace();
}
}
if (ps!=null){
try {
ps.close();
} catch (Exception e) {
e.printStackTrace();
}
}
if (conn!=null){
try {
conn.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
return false;
}
将添加数据成功的表返回并展示在添加窗口中
public class ShowDao_inner {
private static PreparedStatement ps = null;
private static Connection conn = null;
private static ResultSet rs = null;
private static JScrollPane scrollPane;
public JScrollPane Show(String ftype){
try {
Class.forName("com.mysql.jdbc.Driver");
String url = "jdbc:mysql://localhost:3306/hncu";
String name = "root";
String password = "1020";
conn = DriverManager.getConnection(url,name,password);
String sql= "select * from "+ftype ;
ps=conn.prepareStatement(sql);
rs=ps.executeQuery();
GetRow g = new GetRow();
int a= g.getRow(ftype);
String[] colNames = {"序号","名称","文档-"+ftype};
Object[][] rowData =new Object[a][3];
int count = 0;
while (rs.next()){
rowData[count][0]=rs.getString("id");
rowData[count][1]=rs.getString("f_name");
rowData[count][2]=rs.getObject("f_data");
count++;
}
/*
TableModel dataModel = new AbstractTableModel() {
public int getColumnCount() { return 3; }
public int getRowCount() { return 8;}
public Object getValueAt(int row, int col) { return new Integer(row*col); }
};
*/
JTable table = new JTable(rowData,colNames);
table.setBounds(0,0,100,100);
table.setRowHeight(50);
// JPanel panel = new JPanel();
// panel.add(table);
// panel.setPreferredSize(new Dimension(507,300));
// scrollPane = new JScrollPane();
// scrollPane.setViewportView(panel);
scrollPane = new JScrollPane(table);
scrollPane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);
scrollPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
return scrollPane;
} catch (Exception e) {
e.printStackTrace();
}finally {
if (rs!=null){
try {
rs.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
if (ps!=null){
try {
ps.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
if (conn!=null){
try {
conn.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
}
return scrollPane;
}
}
获取数据库表中的数据记录数
public class GetRow {
private static PreparedStatement ps = null;
private static Connection conn = null;
private static ResultSet rs = null;
private static String url = "jdbc:mysql://localhost:3306/hncu";
private static String name = "root";
private static String password = "1020";
public GetRow(){
}
public int getRow(String ftype){
try {
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection(url,name,password);
String sql= "select * from "+ftype;
ps=conn.prepareStatement(sql);
rs=ps.executeQuery();
int rowCount = 0;
while(rs.next()) {
rowCount++;
}
return rowCount;
} catch (Exception e) {
e.printStackTrace();
}finally {
if (rs!=null){
try {
rs.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
if (ps!=null){
try {
ps.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
if (conn!=null){
try {
conn.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
}
return 0;
}
}
2.删除操作窗口
public static JPanel delete(JPanel panel){
JLabel label = new JLabel(" 请输入文件名");
JTextField inputText = new JTextField();
label.setFont(new Font("宋体",0,20));
label.setBounds(30,80,150,30);
inputText.setFont(new Font("华文行楷",0,20));
inputText.setBounds(200,80,300,30);
JButton deletefrombase = new JButton("从数据库中删除该文件");
deletefrombase.setFont(new Font("宋体",0,20));
deletefrombase.setBounds(160,200,560,35);
deletefrombase.addActionListener(e -> {
String input_text = inputText.getText();
ShowDao showDao = new ShowDao();
if (!input_text.equals("") && input_text!=null) {
Boolean b = showDao.delete(item,input_text);
if (b) {
ShowDao_inner showDao_inner = new ShowDao_inner();
JScrollPane scrollPane = showDao_inner.Show(item);
scrollPane.setBounds(160, 300, 500, 300);
panel.add(scrollPane);
} else {
JOptionPane.showMessageDialog(null, "删除失败");
}
}else{
JOptionPane.showMessageDialog(null,"请完善信息再进行删除操作");
}
});
panel.add(deletefrombase);
panel.add(label);
panel.add(inputText);
return panel;
}
从数据库中删除文件
public Boolean delete(String ftype ,String inputText){
try {
Class.forName("com.mysql.jdbc.Driver");
conn=DriverManager.getConnection(url,name,password);
String s = "delete from "+ftype+" where f_name=?";
System.out.println(s);
// File file = new File(""+urlText);
// fr = new FileReader(file);
ps=conn.prepareStatement(s);
ps.setString(1,inputText);
ps.executeUpdate();
System.out.println("成功删除数据");
return true;
} catch (Exception e) {
e.printStackTrace();
}finally {
if (rs!=null){
try {
rs.close();
} catch (Exception e) {
e.printStackTrace();
}
}
if (ps!=null){
try {
ps.close();
} catch (Exception e) {
e.printStackTrace();
}
}
if (conn!=null){
try {
conn.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
return false;
}
3.修改操作窗口
public static JPanel update(JPanel panel){
JLabel label = new JLabel(" 请输入要更改的文件名");
JTextField inputText = new JTextField();
label.setFont(new Font("宋体",0,20));
label.setBounds(30,60,240,30);
inputText.setFont(new Font("华文行楷",0,20));
inputText.setBounds(270,60,300,30);
JLabel label2 = new JLabel(" 请输入修改后的文件名");
JTextField up_text = new JTextField();
label2.setFont(new Font("宋体",0,20));
label2.setBounds(30,110,240,30);
up_text.setFont(new Font("华文行楷",0,20));
up_text.setBounds(270,110,300,30);
JButton btn = new JButton("修改文件路径");
btn.setFont(new Font("宋体",0,20));
btn.setBounds(30,150,200,30);
JTextField urlText = new JTextField();
urlText.setFont(new Font("楷体",0,18));
urlText.setBounds(240,150,600,30);
JFileChooser jfc=new JFileChooser();
btn.addActionListener(e -> {
jfc.showOpenDialog(jfc);
File file = jfc.getSelectedFile();
urlText.setText(file.getAbsolutePath());
});
/*JTextField updatefname = new JTextField(20);
JTextField updatefurl = new JTextField(20);
updatefname.setFont(new Font("华文行楷",0,20));
updatefname.setBounds(200,80,300,30);
updatefurl.setFont(new Font("华文行楷",0,20));
updatefurl.setBounds(200,80,300,30);*/
/* ButtonGroup group = new ButtonGroup();
JRadioButton updatefname = new JRadioButton("修改文件名");
JRadioButton updatefurl = new JRadioButton("修改文件路径");
updatefname.setFont(new Font("华文行楷",0,20));
updatefurl.setFont(new Font("华文行楷",0,20));
updatefname.setBounds(140,160,300,30);
updatefurl.setBounds(140,200,300,30);
group.add(updatefname);
group.add(updatefurl);
updatefname.addActionListener(e -> {
JTextField fname = new JTextField();
fname.setFont(new Font("华文行楷",0,20));
fname.setBounds(200,160,300,30);
String up_name = fname.getText();
System.out.println(up_name);
panel.add(fname);
});
JFileChooser jFileChooser= new JFileChooser();
updatefurl.addActionListener(e -> {
JTextField furl = new JTextField();
furl.setFont(new Font("华文行楷",0,20));
furl.setBounds(200,200,300,30);
jFileChooser.showOpenDialog(jFileChooser);
File file =jFileChooser.getSelectedFile();
furl.setText(file.getAbsolutePath());
String up_url = furl.getText();
System.out.println(up_url);
panel.add(furl);
});*/
/*JButton button = new JButton("输出文件名和地址");
button.setBounds(30,430,150,30);
button.addActionListener(e->{
String input_text = inputText.getText();
String url_text = urlText.getText();
System.out.println(input_text);
System.out.println(url_text);
});
panel.add(button);*/
JButton updatebase = new JButton("从数据库中修改该文件");
updatebase.setFont(new Font("宋体",0,20));
updatebase.setBounds(160,200,560,35);
// String item = (String) comboBox.getSelectedItem();
// System.out.println(comboBox.getSelectedItem());
updatebase.addActionListener(e -> {
String input_text = inputText.getText();
String url_text = urlText.getText();
String uptext = up_text.getText();
System.out.println(input_text);
System.out.println(url_text);
System.out.println(uptext);
ShowDao showDao = new ShowDao();
if ((!input_text.equals("") && input_text!=null )&&(!url_text.equals("") && url_text!=null)&&(!uptext.equals("") && uptext!=null)) {
Boolean b = showDao.update(item,input_text,url_text,uptext);
if (b) {
ShowDao_inner showDao_inner = new ShowDao_inner();
JScrollPane scrollPane = showDao_inner.Show(item);
scrollPane.setBounds(160, 300, 500, 300);
panel.add(scrollPane);
} else {
JOptionPane.showMessageDialog(null, "修改失败");
}
}else{
JOptionPane.showMessageDialog(null,"请完善信息再进行修改操作");
}
});
panel.add(label2);
panel.add(up_text);
panel.add(btn);
panel.add(urlText);
panel.add(updatebase);
// panel.add(updatefname);
// panel.add(updatefurl);
panel.add(label);
panel.add(inputText);
return panel;
}
数据库中文件的修改
public Boolean update(String ftype ,String inputText,String urlText,String uptext){
try {
Class.forName("com.mysql.jdbc.Driver");
conn=DriverManager.getConnection(url,name,password);
String s = "update "+ftype+" set f_name= ? ,f_data= ? where f_name=?";
System.out.println(s);
// File file = new File(""+urlText);
// fr = new FileReader(file);
ps=conn.prepareStatement(s);
ps.setString(1,uptext);
ps.setString(2,urlText);
ps.setString(3,inputText);
ps.executeUpdate();
System.out.println("成功更新数据");
return true;
} catch (Exception e) {
e.printStackTrace();
}finally {
if (rs!=null){
try {
rs.close();
} catch (Exception e) {
e.printStackTrace();
}
}
if (ps!=null){
try {
ps.close();
} catch (Exception e) {
e.printStackTrace();
}
}
if (conn!=null){
try {
conn.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
return false;
}
4.查找操作窗口
public static JPanel find(JPanel panel){
JLabel label = new JLabel(" 请输入要查找的文件名");
JTextField inputText = new JTextField();
label.setFont(new Font("宋体",0,20));
label.setBounds(30,80,240,30);
inputText.setFont(new Font("华文楷体",0,20));
inputText.setBounds(270,80,300,30);
JButton findfrombase = new JButton("从数据库中查找该文件");
findfrombase.setFont(new Font("宋体",0,20));
findfrombase.setBounds(160,200,560,35);
findfrombase.addActionListener(e -> {
String input_text = inputText.getText();
ShowDao showDao = new ShowDao();
if (!input_text.equals("") && input_text!=null){
JScrollPane scrollPane =new JScrollPane();
scrollPane = showDao.find(item,input_text,scrollPane);
scrollPane.setBounds(50, 300, 800, 300);
panel.add(scrollPane);
} else{
JOptionPane.showMessageDialog(null,"请完善信息再进行修改操作");
}
});
panel.add(findfrombase);
panel.add(label);
panel.add(inputText);
return panel;
}
从数据库查找到文件并显示
public JScrollPane find(String ftype ,String inputText,JScrollPane scrollPane){
try {
Class.forName("com.mysql.jdbc.Driver");
conn=DriverManager.getConnection(url,name,password);
String s = "select f_data from "+ftype+" where f_name=?";
// System.out.println(s);
// File file = new File(""+urlText);
// fr = new FileReader(file);
ps=conn.prepareStatement(s);
ps.setString(1,inputText);
rs = ps.executeQuery();
if(rs.next()){
int sb_length = sb.length();// 取得字符串的长度
sb.delete(0,sb_length);
sb.append(rs.getObject("f_data")+"\n");
}else{
JOptionPane.showMessageDialog(null,"找不到该文件,该文件可能被移动或删除");
}
String str = sb.toString();
JPanel panel = new JPanel();
panel.setLayout(null);
panel.setPreferredSize(new Dimension(2800,1900));
panel.setBounds(0,0,2800,1900);
/*JLabel label = new JLabel("查找的文件内容:");
label.setFont(new Font("黑体",0,20));
label.setBounds(0,0,200,300);*/
JTextArea area = new JTextArea();
area.setText("");
area.setFont(new Font("楷体",0,25));
area.setBounds(0,0,2800,3300);
area.setEditable(false);
area.append(str);
// panel.add(label);
panel.add(area);
scrollPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED);
scrollPane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);
scrollPane.setViewportView(panel);
return scrollPane;
} catch (Exception e) {
e.printStackTrace();
}finally {
if (rs!=null){
try {
rs.close();
} catch (Exception e) {
e.printStackTrace();
}
}
if (ps!=null){
try {
ps.close();
} catch (Exception e) {
e.printStackTrace();
}
}
if (conn!=null){
try {
conn.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
return scrollPane;
}
运行截图
视频演示
Java课程设计演示20210623
课程设计总结
关于整个课程设计过程最大的收获就是以下几点:
- 一定要边写边测试
在有相对较多重复代码且容易混淆的情况下,应当对每一个相应的功能进行相应测试.这样会大大减少代码的错误率和提高编程效率.
对于数据库的相关连接操作中对SQL语句的最好的处理办法是复制一份到SQLyog中去执行,是语法错误还是连接问题就很显然了. - 对功能进行分块
通常情况下是不建议将所有代码写在一块,这样真的会很混乱,在查找
所需功能会无从下手.而对功能进行分块,就会让人一眼就明白你在做什么,包 括具体实现了些什么功能.将一长串代码通过类和函数的包装就会显得简洁 明了. - 学会使用API
方法有很多,我们不可能所有东西都记得住,所以要学会利用好工具.平时
写代码都只会用书上的东西,而这次课设,我查看了很多相关类的方法,对于 API的使用也有了更深刻的印象.通过API就可以了解到具体方法的作用和返 回值,我可以找到我需要的功能去写Java程序. - 选择正确且适合的方法和存储结构
文件的读取,表格的创建,是以数组还是集合的形式来存储,都极大的地影
响了代码的复杂程度,然而最重要的还是要自己的熟练程度.
再吐槽自己几句:
写出这么个玩意是真的看不下去啊!!!~~~
以后还是写别的吧,管理系统这一类的无非就是增删改查
嗯对,还是再多学点东西吧
参考文献
[1] Java基础入门(第二版) 清华大学出版社
[2] jdk api 1.8_google.CHM
[3] 菜鸟教程 (runoob.com)