这里主要是运用到了jdbc来连接数据库,而在数据库中存在的是通讯录的记录,有id,姓名,手机号,创建时间,修改时间。同时使用的是swing的来做可视化界面,其中运用到了弹窗,按钮,标签,表格等组件,同时还有着监视器来监测的是按钮的动作。
理论知识:这里使用的是手写sql语句,而不采取MyBatis框架,首先要连接数据库,通过sql语句来读取与更新数据,要学会select,update,delete语句的使用。同时要运用到接口与实现类的结构模式,简化开发操作。
package view;
import service.ViewService;
import service.impl.ViewServiceImpl;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.sql.SQLException;
//主窗口的进入
public class Mainview extends JFrame {
ViewService v = new ViewServiceImpl();
public void Mainview() throws ClassNotFoundException, SQLException {
//数据库的加入
JFrame jFrame = new JFrame("通讯录系统");//创建对象
jFrame.setVisible(true);//窗口的可视化
jFrame.setBounds(650, 150, 700, 400);//窗口的初始化
jFrame.setResizable(false);
Container container = jFrame.getContentPane();
container.setBackground(Color.lightGray);
jFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);//关闭事件
container.setLayout(null);
//初始化按钮信息
JLabel jLabelr = new JLabel("通讯录管理系统By陈某宏");
jLabelr.setFont(new Font("行书", Font.BOLD, 30));
jLabelr.setForeground(Color.BLUE);
jLabelr.setBounds(180, 250, 400, 100);
container.add(jLabelr);
JButton button1 = new JButton("浏览所有的通讯信息");
JButton button2 = new JButton("增加通讯录信息");
JButton button3 = new JButton("查询相应ID或姓名的通讯录成员");
JButton button4 = new JButton("删除指定的通讯录成员");
JButton button5 = new JButton("修改指定的通讯录成员");
jFrame.update(jFrame.getGraphics());
button1.setBounds(150, 20, 400, 40);
button2.setBounds(150, 70, 400, 40);
button3.setBounds(150, 120, 400, 40);
button4.setBounds(150, 170, 400, 40);
button5.setBounds(150, 220, 400, 40);
//添加按钮
container.add(jLabelr);
container.add(button1);
container.add(button2);
container.add(button3);
container.add(button4);
container.add(button5);
jFrame.update(jFrame.getGraphics());
button1.addActionListener(new AbstractAction() {//按钮1 增加学生信息
@Override
public void actionPerformed(ActionEvent e) {
v.selectAll();
}
});
//按钮2
button2.addActionListener(new AbstractAction() {
@Override
public void actionPerformed(ActionEvent e) {
v.add();
}
});
button3.addActionListener(new AbstractAction() {
@Override
public void actionPerformed(ActionEvent e) {
v.selectByxx();
}
});
button4.addActionListener(new AbstractAction() {
@Override
public void actionPerformed(ActionEvent e) {
v.delete();
}
});
button5.addActionListener(new AbstractAction() {
@Override
public void actionPerformed(ActionEvent e) {
v.update();
}
});
}
}
package dao.impl;
import dao.PhonerelationshipDao;
import entity.PhonerelationshipEntity;
import utils.MysqlDriverutil;
import java.sql.*;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
public class PhonerelationshipImpl implements PhonerelationshipDao {
Connection connection;
{
try {
connection = new MysqlDriverutil().getconnection();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
@Override
public List selectAll() {
String sql = "select * from phonerelationship";
PreparedStatement std;
ArrayList phonerelationshipEntities = new ArrayList<>();
try {
std = connection.prepareStatement(sql);
ResultSet resultSet = std.executeQuery(sql);
while (resultSet.next()) {
PhonerelationshipEntity p = new PhonerelationshipEntity();
p.setId(resultSet.getString("id"));
p.setName(resultSet.getString("name"));
p.setPhonenum(resultSet.getString("phonenum"));
p.setCreattime(resultSet.getDate("createtime"));
p.setUpdatetime(resultSet.getDate("updatetime"));
phonerelationshipEntities.add(p);
}
} catch (SQLException ex) {
ex.printStackTrace();
}
return phonerelationshipEntities;
}
@Override
public List selectByIdorName(String idorname) {
String sql = "select * from phonerelationship where `id`=" + "'" + idorname + "'" + " or `name` like" + " '%" + idorname + "%'";
PreparedStatement std;
ArrayList phonerelationshipEntities = new ArrayList<>();
try {
std = connection.prepareStatement(sql);
ResultSet resultSet = std.executeQuery(sql);
while (resultSet.next()) {
PhonerelationshipEntity p = new PhonerelationshipEntity();
p.setId(resultSet.getString("id"));
p.setName(resultSet.getString("name"));
p.setPhonenum(resultSet.getString("phonenum"));
p.setCreattime(resultSet.getDate("createtime"));
p.setUpdatetime(resultSet.getDate("updatetime"));
phonerelationshipEntities.add(p);
}
} catch (SQLException ex) {
ex.printStackTrace();
}
return phonerelationshipEntities;
}
@Override
public int deleteById(String idorallname) {
String sql = "delete from phonerelationship where `id`=" + "'" + idorallname + "'" + " or `name`=" + "'" + idorallname + "'";
PreparedStatement std;
int i = 0;
try {
std = connection.prepareStatement(sql);
i = std.executeUpdate(sql);
} catch (SQLException ex) {
ex.printStackTrace();
}
return i;
}
@Override
public int updateById(PhonerelationshipEntity p) {
PreparedStatement std = null;
Timestamp timestamp = new Timestamp(new Date().getTime());
int i = 0;
String sql = "update phonerelationship set `name`=" + "'" + p.getName() + "'" + ",`phonenum`=" + "'" + p.getPhonenum() + "'" + ",updatetime=" + "'" + timestamp + "'" + " where id=" + "'" + p.getId() + "'";
try {
std = connection.prepareStatement(sql);
i = std.executeUpdate(sql);
} catch (SQLException throwables) {
throwables.printStackTrace();
}
return i;
}
@Override
public void add(PhonerelationshipEntity p) {
PreparedStatement std = null;
String sql = "insert into phonerelationship (`id`,`name`,`phonenum`,`createtime`,`updatetime`) value (?,?,?,?,?)";
try {
std = connection.prepareStatement(sql);
std.setString(1, p.getId());
std.setString(2, p.getName());
std.setString(3, p.getPhonenum());
std.setString(4, String.valueOf(new Timestamp(new Date().getTime())));
std.setString(5, String.valueOf(new Timestamp(new Date().getTime())));
std.executeUpdate();
} catch (SQLException ex) {
ex.printStackTrace();
}
}
}
本实验实现的是简单的通讯录系统,仅供参考。