目录
一、JFormDesiner,可视化的窗口设计工具
本人采用的编译器是idea,JFormDesiner相当于Eclipse中的Windowsbuilder,JformDesiner是一个插件,能够设计自己的界面并且形成界面的代码,但是javaswing比较老旧,所以只是初学的一种工具。建议使用大于11的JDK版本,不然可能无法编译成功!!
1.JFormDesiner插件的安装
File->settings->plugins,在搜索栏中搜索JFormDesigner安装,这个插件是需要付费的,可以申请20天的免费试用期,或者自己去搜索破解方法。
实用申请网址:Get Evaluation License Key | JFormDesigner - Java/Swing GUI Designer
2.创建一个JFormDesigner Form项目
安装完成后,创建项目时就可以创建出一个Form窗体进行设计
name处随便起一个名字,Superclass处选择JFrame,按钮出选择none,布局选择null Layout,创建好后就可以开始设计了。
2.实现的界面效果
1.登录界面
2.注册界面
3.管理界面
此处的图片只是简单的贴图,想要实现从数据库的调取,还有点麻烦,我也只是简单的实现了一下,也只是为了好看。
4.修改密码
5.增加商品
在点击加号的label的时候,会弹出文件选择器,具体的会在下文讲解。
6.查找商品
7.表格的形式查找所有商品和增加库存
8.修改商品
二、数据库的建立
1.用户表(s_admin)
字段名称 |
数据类型及长度 |
允许为空 |
主键 |
字段说明 |
s_account |
Varchar(20) |
N |
是 |
账号 |
s_password |
Varchar(20) |
Y |
否 |
密码 |
s_name |
Varchar(20) |
Y |
否 |
姓名 |
s_adress |
Varchar(40) |
Y |
否 |
住址 |
s_email |
Varchar(20) |
Y |
否 |
邮箱 |
s_pow |
Varchar(20) |
Y |
否 |
权限0代表普通员工 1代表管理员 |
用户(账号,密码,姓名,住址,邮箱,用户权限)
2、商品表(s_stock)
字段名称 |
数据类型及长度 |
允许为空 |
主键 |
字段说明 |
s_id |
Varchar(20) |
N |
是 |
商品编码 |
s_name |
Varchar(20) |
N |
否 |
商品名字 |
s_price |
Decimal(10) |
Y |
否 |
商品价格 |
s_num |
Int |
N |
否 |
数量 |
s_photona |
Varchar(255) |
Y |
否 |
图片名字 |
s_path |
Longblob(20) |
Y |
否 |
存的图片 |
商品(商品编码,商品名,商品价格,数量,图片名,图片存储路径)
三、代码实现
本处只会粘贴监听事件的代码,具体的swing框架过长,主要实现的是通过监听事件与数据库建立的连接
1、工具类代码
(一)JdbcUtils,实现与数据库的连接
import java.sql.*;
public class JdbcUtils {
private static final String DRIVER = "com.mysql.cj.jdbc.Driver";
private static final String URL = "jdbc:mysql://localhost:3306/jdbc?useSSL=false";
private static final String USER = "root";
private static final String PASSWORD = "18342003";
//注册驱动程序放在代码块中,每次只能注册一次
static {
try {
Class.forName(DRIVER);
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
//创建getConnection对象,用来获得connection对象
public static Connection getConnection(){
Connection conn = null;
try {
conn = DriverManager.getConnection(URL,USER,PASSWORD);
}catch (SQLException e){
e.printStackTrace();
}
return conn;
}
//创建free方法实现关闭连接功能
//两个close用于方法重载
public static void close(Statement st, Connection conn) {
if (st != null) {
try {
st.close();
} catch (SQLException e) {
e.printStackTrace();
} finally {
if (conn != null) {
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
}
public static void close(ResultSet rs, Statement st, Connection conn) {
if (rs != null) {
try {
rs.close();
} catch (SQLException e) {
e.printStackTrace();
} finally {
if (st != null) {
try {
st.close();
} catch (SQLException e) {
e.printStackTrace();
} finally {
if (conn != null) {
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
}
}
}
}
(二)图片工具类PicUtil
主要实现图片的转换和缩放,根据路径获得ImageIcon和把BufferImage转换成ImageIcon
import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
public class PicUtils {
public static ImageIcon ZoomBySize(String srcFile,int width,int height)throws IOException{
BufferedImage img = ImageIO.read(new File(srcFile));
Image _img = img.getScaledInstance(width,height,Image.SCALE_DEFAULT);
BufferedImage image = new BufferedImage(width,height,BufferedImage.TYPE_INT_RGB);
Graphics2D graphics = image.createGraphics();
graphics.drawImage(_img,0,0,null);
graphics.dispose();
//获取到了图片流 编程imageIcon变量
ImageIcon icon = new ImageIcon(image);//转换格式,将图片流转换成icon
return icon;
}
public static ImageIcon ZoomBySize(BufferedImage img,int width,int height)throws IOException{
Image _img = img.getScaledInstance(width,height,Image.SCALE_DEFAULT);
BufferedImage image = new BufferedImage(width,height,BufferedImage.TYPE_INT_RGB);
Graphics2D graphics = image.createGraphics();
graphics.drawImage(_img,0,0,null);
graphics.dispose();
//获取到了图片流 编程imageIcon变量
ImageIcon icon = new ImageIcon(image);//转换格式,将图片流转换成icon
return icon;
}
}
(三)Tools类,弹出窗体的实现
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.sql.ResultSet;
import java.sql.SQLException;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPasswordField;
import javax.swing.table.DefaultTableModel;
public class Tools {
public Tools() {
}
public static void setWindowPos(int WIDTH, int HEIGHT, JFrame jframe) {
Toolkit kit = Toolkit.getDefaultToolkit();
Dimension screenSize = kit.getScreenSize();
int width = screenSize.width;
int height = screenSize.height;
int x = (width - WIDTH) / 2;
int y = (height - HEIGHT) / 2;
jframe.setBounds(x, y, WIDTH, HEIGHT);
}
public static String getPassword(JPasswordField jp) {
String password = new String(jp.getPassword());
return password;
}
public static void messageWindows(String msg) {
JOptionPane.showMessageDialog((Component)null, msg, "消息", 2);
}
public static int addDataTable(ResultSet rs, DefaultTableModel model, int index) {
int count = 0;
model.setNumRows(0);
String[] data = new String[index];
try {
while(rs.next()) {
++count;
for(int i = 0; i < data.length; ++i) {
data[i] = rs.getString(i + 1);
}
model.addRow(data);
}
rs.close();
return count;
} catch (SQLException var6) {
var6.printStackTrace();
return count;
}
}
}
(四)Table类,实现一个简单的表格
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
public class Table {
JTable tableL = null;
JScrollPane jscrollpane;
DefaultTableModel model = null;
public Table(Object[] columns) {
this.Table(columns);
}
void Table(Object[] columns) {
this.tableL = this.getTable(columns);
this.jscrollpane = new JScrollPane(this.tableL);
this.jscrollpane.setVerticalScrollBarPolicy(20);
}
JTable getTable(Object[] columns) {
if (this.tableL == null) {
this.tableL = new JTable();
this.model = new DefaultTableModel() {
public boolean isCellEditable(int row, int column) {
return false;
}
};
this.model.setColumnIdentifiers(columns);
this.tableL.setModel(this.model);
this.tableL.getTableHeader().setReorderingAllowed(false);
this.tableL.getTableHeader().setResizingAllowed(false);
}
return this.tableL;
}
public JTable getTables() {
return this.tableL;
}
public JScrollPane getJScrollPane() {
return this.jscrollpane;
}
public DefaultTableModel getModel() {
return this.model;
}
}
2.界面代码
(一)登录界面
{
public test2() {
initComponents();
}
private void initComponents() {
// JFormDesigner - Component initialization - DO NOT MODIFY //GEN-BEGIN:initComponents @formatter:off
// Generated using JFormDesigner Evaluation license - 刘
button1 = new JButton();
textField1 = new JTextField();
label1 = new JLabel();
passwordField1 = new JPasswordField();
label2 = new JLabel();
label3 = new JLabel();
label6 = new JLabel();
label5 = new JLabel();
//======== this ========
setForeground(SystemColor.textHighlight);
setIconImage(new ImageIcon(getClass().getResource("/image/login.png")).getImage());
setTitle("鲜花管理系统登录界面");
setBackground(new Color(0xccffff));
var contentPane = getContentPane();
contentPane.setLayout(null);
//---- button1 ----
button1.setText("安全登录");
button1.setBackground(new Color(0x99ffff));
contentPane.add(button1);
button1.setBounds(110, 195, 270, 45);
contentPane.add(textField1);
textField1.setBounds(195, 90, 155, 25);
button1.addActionListener(new ActionListener() {@Override public void actionPerformed(ActionEvent e) {
//获取账号 密码
String Account = textField1.getText();
String Password = Tools.getPassword(passwordField1);
if(Account.equals("")){
Tools.messageWindows("账号不能为空");
}else if(Password.equals("")){
Tools.messageWindows("密码不能为空");
}else{
String []date = {
Account,
Password
};
try{
Connection conn = JdbcUtils.getConnection();
PreparedStatement pst = conn.prepareStatement("select *from s_admin where s_account = ? and s_password = ?");
pst.setString(1,date[0]);
pst.setString(2,date[1]);
ResultSet rs = pst.executeQuery();
if(rs.next()){
Tools.messageWindows("登录成功");
if(rs.getString(6).equals("0")){
Tools.messageWindows("普通员工登录成功");
new Manager(Account).setVisible(true);
dispose();
}else{
Tools.messageWindows("管理员登录成功");
new Manager(Account).setVisible(true);
dispose();
}
}else{
Tools.messageWindows("账号或密码错误");
}
}catch (SQLException ex){
ex.printStackTrace();
}
}
}});
//---- label1 ----
label1.setText("\u7528\u6237\u540d");
label1.setFont(new Font("Microsoft YaHei", Font.BOLD, 16));
contentPane.add(label1);
label1.setBounds(135, 85, label1.getPreferredSize().width, 32);
contentPane.add(passwordField1);
passwordField1.setBounds(195, 125, 155, 25);
//---- label2 ----
label2.setText("\u5bc6\u7801");
label2.setFont(new Font("Microsoft YaHei", Font.BOLD, 16));
contentPane.add(label2);
label2.setBounds(135, 125, 42, label2.getPreferredSize().height);
//---- label3 ----
label3.setText("\u9c9c\u82b1\u7ba1\u7406\u7cfb\u7edf");
label3.setPreferredSize(new Dimension(99, 22));
label3.setHorizontalAlignment(SwingConstants.CENTER);
label3.setHorizontalTextPosition(SwingConstants.CENTER);
label3.setIconTextGap(20);
label3.setFont(label3.getFont().deriveFont(label3.getFont().getStyle() | Font.BOLD, label3.getFont().getSize() + 12f));
contentPane.add(label3);
label3.setBounds(80, 0, 330, 50);
//---- label6 ----
label6.setText("注册>");
contentPane.add(label6);
label6.setBounds(new Rectangle(new Point(440, 240), label6.getPreferredSize()));
label6.addMouseListener(new MouseListener() {@Override public void mouseClicked(MouseEvent e) {
new Regest1().setVisible(true);
}@Override public void mousePressed(MouseEvent e) {
}@Override public void mouseReleased(MouseEvent e) {
}@Override public void mouseEntered(MouseEvent e) {
}@Override public void mouseExited(MouseEvent e) {
}});
//---- label5 ----
label5.setIcon(new ImageIcon(getClass().getResource("/image/background.jpg")));
contentPane.add(label5);
label5.setBounds(0, -5, 500, 295);
{
// compute preferred size
Dimension preferredSize = new Dimension();
for(int i = 0; i < contentPane.getComponentCount(); i++) {
Rectangle bounds = contentPane.getComponent(i).getBounds();
preferredSize.width = Math.max(bounds.x + bounds.width, preferredSize.width);
preferredSize.height = Math.max(bounds.y + bounds.height, preferredSize.height);
}
Insets insets = contentPane.getInsets();
preferredSize.width += insets.right;
preferredSize.height += insets.bottom;
contentPane.setMinimumSize(preferredSize);
contentPane.setPreferredSize(preferredSize);
}
setSize(501, 331);
setLocationRelativeTo(getOwner());
// JFormDesigner - End of component initialization //GEN-END:initComponents @formatter:on
}
// JFormDesigner - Variables declaration - DO NOT MODIFY //GEN-BEGIN:variables @formatter:off
// Generated using JFormDesigner Evaluation license - 刘
private JButton button1;
private JTextField textField1;
private JLabel label1;
private JPasswordField passwordField1;
private JLabel label2;
private JLabel label3;
private JLabel label6;
private JLabel label5;
// JFormDesigner - End of variables declaration //GEN-END:variables @formatter:on
//登录账号
}
(二)注册界面
button1.addActionListener(new ActionListener() {@Override public void actionPerformed(ActionEvent e) {
String Account = textField1.getText(); // 账户
String Password = textField2.getText();//密码
String Name = textField3.getText();//姓名
String Adress = textField4.getText();//地址
String Email = textField5.getText();//邮箱
String pow = "0";//权限
s_admin admain = new s_admin();
admain.setAccount(Account);
admain.setPassword(Password);
admain.setName(Name);
admain.setEmail(Email);
admain.setEmail(Email);
if(Account.equals("")){
Tools.messageWindows("请输入账号");
}else if(Password.equals("")){
Tools.messageWindows("请输入密码");
}else if(Name.equals("")){
Tools.messageWindows("请输入姓名");
}else if(Adress.equals("")){
Tools.messageWindows("请输入地址");
}else if(Email.equals("")){
Tools.messageWindows("请输入邮箱");
if(Email.matches("^[a-zA-Z0-9_-]+@[a-zA-Z0-9_-]+(\\.[a-zA-Z0-9_-]+)+$\n")){
Tools.messageWindows("请输入正确格式的邮箱号码");
}
}
else{
//将注册的数据写入到数据库中
try{
Connection conn = JdbcUtils.getConnection();
int i = addAdmin(conn,admain);
if(i==2){
Tools.messageWindows("账户名已存在,请重新注册");
}else if(i==0){
Tools.messageWindows("注册失败");
} else{
Tools.messageWindows("注册成功");
dispose();
}
}catch (SQLException ex){
ex.printStackTrace();
}
}
}});
public int addAdmin(Connection conn,s_admin admin) throws SQLException {
//检查用户名是否存在
conn = JdbcUtils.getConnection();
String sql = "select * from s_admin where s_account = ?";
PreparedStatement pst = conn.prepareStatement(sql);
pst.setString(1,admin.getAccount());
ResultSet rs = pst.executeQuery();
if(rs.next()){
return 2;
}
PreparedStatement pst1 = conn.prepareStatement("insert into s_admin values (?,?,?,?,?,?)");
pst1.setString(1,admin.getAccount());
pst1.setString(2,admin.getPassword());
pst1.setString(3,admin.getName());
pst1.setString(4,admin.getAdress());
pst1.setString(5,admin.getEmail());
pst1.setString(6,"0");
return pst1.executeUpdate();
}
}
(三)Manager页面
public class Manager extends JFrame {
public Manager(String Account) {
initComponents();
this.Account = Account;
}
String Account;
private void menuItem2(ActionEvent e) {
dispose();
//修改自己的密码,打开一个新窗口,输入原来的密码,再输入新的密码
ChangePassword1 changePassword1 = new ChangePassword1(Account);
initComponents();
changePassword1.setVisible(true);
}
private void menuItem3(ActionEvent e) {
dispose();
new test2().setVisible(true);
}
private void button1(ActionEvent e) {
//添加商品,跳出来一个窗口,进行添加
new addStockView().setVisible(true);
}
private void menuItem1(ActionEvent e) {
try{
Connection conn = JdbcUtils.getConnection();
PreparedStatement pst = conn.prepareStatement(" delete from s_admin where s_account = ?");
pst.setString(1,Account);
pst.executeUpdate();
int a = pst.executeUpdate();
if(a==0||a==-1){
Tools.messageWindows("注销成功");
dispose();
new test2().setVisible(true);
}else{
Tools.messageWindows("注销失败");
//更改密码成功,返回登录界面
}}catch (SQLException ex){
ex.printStackTrace();
}
}
private void button2(ActionEvent e) {
String id = textField1.getText();//商品编号
//从数据库中调取
if (id.equals("")) {
Tools.messageWindows("请输入编码");
} else {
try {
Connection conn = JdbcUtils.getConnection();
PreparedStatement pst = conn.prepareStatement("select * from jdbc.s_stock where s_id = ?");
pst.setString(1, id);
ResultSet rs = pst.executeQuery();
if (rs.next()) {
String s_id = rs.getString(1);
String s_name = rs.getString(2);
String s_price = rs.getString(3);
String num = rs.getString(4);
InputStream in = null;
in = rs.getBinaryStream(6);
byte[] b;
BufferedImage img = null;
try {
b = new byte[in.available()];//定义字节储存图片
in.read(b);
InputStream buffin = new ByteArrayInputStream(b, 0, b.length);
img = ImageIO.read(buffin);
ImageIcon icon = PicUtils.ZoomBySize(img, 180, 180);
new findStock1(s_id, s_name, num, s_price, icon).setVisible(true);
} catch (IOException ex1) {
ex1.printStackTrace();
}
} else {
Tools.messageWindows("未找到该商品");
}
} catch (SQLException ex) {
ex.printStackTrace();
}
setVisible(true);
}
}
(四)查找商品
import com.tools.Tools;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.image.BufferedImage;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.*;
import javax.swing.*;
/**
* @author LENOVO
*/
public class findStock1 extends JFrame {
public findStock1(String id, String name, String price, String num, ImageIcon icon) {
this.id = id;
this.name = name;
this.price = price;
this.num = num;
this.icon = icon;
initComponents();
}
private void button1(ActionEvent e) {
//连接数据库进行删除
try{
Connection conn = JdbcUtils.getConnection();
PreparedStatement pst = conn.prepareStatement("delete from jdbc.s_stock where jdbc.s_stock.s_id = ?");
pst.setString(1,id);
pst.executeUpdate();
dispose();
Tools.messageWindows("删除成功");
JdbcUtils.close(pst,conn);
}catch (SQLException ex){
ex.printStackTrace();
}
}
private void button2(ActionEvent e) {
dispose();
new ModifyStock(id,name,price,icon).setVisible(true);
}
String id;
String name;
String price;
String num;
ImageIcon icon;
private void initComponents() {
// JFormDesigner - Component initialization - DO NOT MODIFY //GEN-BEGIN:initComponents @formatter:off
// Generated using JFormDesigner Evaluation license - 刘
setAlwaysOnTop(true);
ResourceBundle bundle = ResourceBundle.getBundle("Flower.Flower");
label1 = new JLabel();
label2 = new JLabel();
label5 = new JLabel();
button1 = new JButton();
button2 = new JButton();
label6 = new JLabel();
label7 = new JLabel();
//======== this ========
var contentPane = getContentPane();
contentPane.setLayout(null);
contentPane.add(label1);
label1.setBounds(5, 5, 180, 180);
label1.setIcon(icon);
//---- label2 ----
label2.setText("商品编码:"+id);
contentPane.add(label2);
label2.setBounds(195, 10, 140, label2.getPreferredSize().height);
//---- label5 ----
label5.setText("商品名称"+name);
contentPane.add(label5);
label5.setBounds(190, 45, 145, label5.getPreferredSize().height);
//---- button1 ----
button1.setText("\u5220\u9664");
contentPane.add(button1);
button1.setBounds(190, 155, 70, 30);
button1.addActionListener(this::button1);
//---- button2 ----
button2.setText("\u66f4\u6539");
contentPane.add(button2);
button2.setBounds(275, 155, 75, 30);
button2.addActionListener(this::button2);
//---- label6 ----
label6.setText("商品价格:"+price);
contentPane.add(label6);
label6.setBounds(195, 80, 145, 22);
//---- label7 ----
label7.setText("商品库存:"+num);
contentPane.add(label7);
label7.setBounds(195, 115, 145, 22);
{
// compute preferred size
Dimension preferredSize = new Dimension();
for(int i = 0; i < contentPane.getComponentCount(); i++) {
Rectangle bounds = contentPane.getComponent(i).getBounds();
preferredSize.width = Math.max(bounds.x + bounds.width, preferredSize.width);
preferredSize.height = Math.max(bounds.y + bounds.height, preferredSize.height);
}
Insets insets = contentPane.getInsets();
preferredSize.width += insets.right;
preferredSize.height += insets.bottom;
contentPane.setMinimumSize(preferredSize);
contentPane.setPreferredSize(preferredSize);
}
pack();
setLocationRelativeTo(getOwner());
// JFormDesigner - End of component initialization //GEN-END:initComponents @formatter:on
}
// JFormDesigner - Variables declaration - DO NOT MODIFY //GEN-BEGIN:variables @formatter:off
// Generated using JFormDesigner Evaluation license - 刘
private JLabel label1;
private JLabel label2;
private JLabel label5;
private JButton button1;
private JButton button2;
private JLabel label6;
private JLabel label7;
// JFormDesigner - End of variables declaration //GEN-END:variables @formatter:on
}
(五)更改商品
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.*;
import javax.swing.*;
import javax.swing.border.*;
import javax.swing.filechooser.FileNameExtensionFilter;
import javax.swing.filechooser.FileSystemView;
import com.jgoodies.forms.factories.*;
import com.tools.Tools;
/**
* @author LENOVO
*/
public class ModifyStock extends JFrame {
String id;
String name;
String price;
ImageIcon icon;
int start = 1;//当动图片时start自增,看看是不是第一次执行
public ModifyStock(String id, String name, String price,ImageIcon icon) {
this.id = id;
this.name = name;
this.price = price;
this.icon = icon;
this.start = 1;
initComponents();
}
String path = null;//图片路径
private void label5MouseClicked(MouseEvent e) {
this.path= getSelectPath();
if(path != null){
//用path进行读取存入数据库
Tools.messageWindows("成功读取");
label5.setIcon(new ImageIcon(path));
start++;
}
}
private void label6MouseClicked(MouseEvent e) {
Tools.messageWindows("您删除了图片");
start++;
path = null;
label5.setIcon(new ImageIcon(getClass().getResource("/image/\u52a0\u53f7 (2).png")));
}
private void button1(ActionEvent e){
String id = textField1.getText();
String name = textField2.getText();
String price = textField3.getText();
//图片的路径
String filename = null;
if(start==1){
//第一次执行没有动图片
}else{
if(path!=null) {
File tempFile = new File(path.trim());//提取路径中的最后一个斜杠
filename = tempFile.getName();
}
}
InputStream in = null;//用于将图片读取出来
String picName;
String path1 = path;//路径;
if(start==1) {
//没有动图片
if (id.equals("")) {
Tools.messageWindows("请输入编码");
} else if (name.equals("")) {
Tools.messageWindows("请输入商品名字");
} else if (price.equals("")) {
Tools.messageWindows("请输入价格");
} else {
try {
Connection conn = JdbcUtils.getConnection();
PreparedStatement pst = conn.prepareStatement("update jdbc.s_stock set s_name = ?,jdbc.s_stock.s_price = ? where s_id = ?");
pst.setString(3, id);
pst.setString(1, name);
pst.setString(2, price);
int a = pst.executeUpdate();//执行更新语句
if (a == 0 || a == -1) {
Tools.messageWindows("更改失败");
} else {
Tools.messageWindows("更新成功");
dispose();
}
} catch (SQLException ex) {
ex.printStackTrace();
}
}
}
else {
//动了图片
if (id.equals("")) {
Tools.messageWindows("请输入编码");
} else if (name.equals("")) {
Tools.messageWindows("请输入商品名字");
} else if (price.equals("")) {
Tools.messageWindows("请输入价格");
} else if (path1==null) {
Tools.messageWindows("请添加图片");
} else {
try {
Connection conn = JdbcUtils.getConnection();
PreparedStatement pst = conn.prepareStatement("update jdbc.s_stock set s_name = ?,jdbc.s_stock.s_price = ?,jdbc.s_stock.s_photona = ?,jdbc.s_stock.s_path=? where s_id = ?");
in = new FileInputStream(path);
pst.setString(5, id);
pst.setString(1, name);
pst.setString(2, price);
pst.setString(3, filename);
pst.setBinaryStream(4, in, in.available());
int a = pst.executeUpdate();//执行更新语句
in.close();
if (a == 0 || a == -1) {
Tools.messageWindows("更改失败");
} else {
Tools.messageWindows("更新成功");
dispose();
}
} catch (SQLException | IOException ex) {
ex.printStackTrace();
}
}
}
}
private void initComponents() {
// JFormDesigner - Component initialization - DO NOT MODIFY //GEN-BEGIN:initComponents @formatter:off
// Generated using JFormDesigner Evaluation license - 刘
ResourceBundle bundle = ResourceBundle.getBundle("Flower.Flower");
label1 = new JLabel();
textField1 = new JTextField();
label2 = new JLabel();
textField2 = new JTextField();
label3 = new JLabel();
textField3 = new JTextField();
label4 = new JLabel();
label5 = new JLabel();
button1 = new JButton();
label6 = new JLabel();
//======== this ========
setTitle(bundle.getString("addStockView.this.title"));
setIconImage(new ImageIcon(getClass().getResource("/image/login.png")).getImage());
var contentPane = getContentPane();
contentPane.setLayout(null);
//---- label1 ----
label1.setText(bundle.getString("addStockView.label1.text"));
contentPane.add(label1);
label1.setBounds(15, 30, 70, label1.getPreferredSize().height);
contentPane.add(textField1);
textField1.setEditable(false);
textField1.setText(id);
textField1.setBounds(100, 25, 90, 30);
//---- label2 ----
label2.setText(bundle.getString("addStockView.label2.text"));
contentPane.add(label2);
label2.setBounds(15, 80, 70, label2.getPreferredSize().height);
contentPane.add(textField2);
textField2.setText(name);
textField2.setBounds(100, 80, 90, 30);
//---- label3 ----
label3.setText(bundle.getString("addStockView.label3.text"));
contentPane.add(label3);
label3.setBounds(new Rectangle(new Point(15, 130), label3.getPreferredSize()));
contentPane.add(textField3);
textField3.setText(price);
textField3.setBounds(100, 130, 90, 30);
//---- label4 ----
label4.setText(bundle.getString("addStockView.label4.text"));
contentPane.add(label4);
label4.setBounds(new Rectangle(new Point(15, 180), label4.getPreferredSize()));
//---- label5 ----
//label5.setIcon(new ImageIcon(getClass().getResource("/image/\u52a0\u53f7 (2).png")));
label5.setIcon(icon);
label5.setBorder(LineBorder.createBlackLineBorder());
label5.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
label5MouseClicked(e);
}
});
contentPane.add(label5);
label5.setBounds(20, 230, 135, 140);
//---- button1 ----
button1.setText("更改商品");
button1.addActionListener(e -> button1(e));
contentPane.add(button1);
button1.setBounds(140, 415, 145, button1.getPreferredSize().height);
//---- label6 ----
label6.setIcon(new ImageIcon(getClass().getResource("/image/\u5220\u9664 (1).png")));
label6.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
label6MouseClicked(e);
}
});
contentPane.add(label6);
label6.setBounds(75, 365, 30, 30);
contentPane.setPreferredSize(new Dimension(343, 416));
setSize(429, 520);
setLocationRelativeTo(getOwner());
// JFormDesigner - End of component initialization //GEN-END:initComponents @formatter:on
}
// JFormDesigner - Variables declaration - DO NOT MODIFY //GEN-BEGIN:variables @formatter:off
// Generated using JFormDesigner Evaluation license - 刘
private JLabel label1;
private JTextField textField1;
private JLabel label2;
private JTextField textField2;
private JLabel label3;
private JTextField textField3;
private JLabel label4;
private JLabel label5;
private JButton button1;
private JLabel label6;
// JFormDesigner - End of variables declaration //GEN-END:variables @formatter:on
public static String getSelectPath(){
JFileChooser jfs = new JFileChooser(FileSystemView.getFileSystemView());//打开目录不一样
jfs.setDialogTitle("选择图片");
jfs.setAcceptAllFileFilterUsed(false);//过滤器
FileNameExtensionFilter filter = new FileNameExtensionFilter("PNG","png");
FileNameExtensionFilter filter1 = new FileNameExtensionFilter("JPG","jpg");
//过滤png和jpg的图片
jfs.addChoosableFileFilter(filter);
jfs.addChoosableFileFilter(filter1);
int returnValue = jfs.showOpenDialog(null);//打开文件选择器
String path = null;
if(returnValue == JFileChooser.APPROVE_OPTION){
//获取到了
path = jfs.getSelectedFile().getPath();
}
return path;
}
}
(六)修改密码界面
import com.tools.Tools;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.*;
import javax.swing.*;
/**
* @author LENOVO
*/
public class ChangePassword1 extends JFrame {
public ChangePassword1(String Account) {
this.Account = Account;
initComponents();
}
String Account;
private void initComponents() {
// JFormDesigner - Component initialization - DO NOT MODIFY //GEN-BEGIN:initComponents @formatter:off
// Generated using JFormDesigner Evaluation license - 刘
ResourceBundle bundle = ResourceBundle.getBundle("Flower.Flower");
label1 = new JLabel();
passwordField1 = new JPasswordField();
label2 = new JLabel();
passwordField2 = new JPasswordField();
label3 = new JLabel();
passwordField3 = new JPasswordField();
button1 = new JButton();
//======== this ========
setTitle(bundle.getString("ChangePassword.this.title"));
setIconImage(new ImageIcon(getClass().getResource("/image/\u4fee\u6539\u8d26\u53f7\u5bc6\u7801.png")).getImage());
var contentPane = getContentPane();
contentPane.setLayout(null);
//---- label1 ----
label1.setText(bundle.getString("ChangePassword.label1.text"));
contentPane.add(label1);
label1.setBounds(new Rectangle(new Point(5, 15), label1.getPreferredSize()));
contentPane.add(passwordField1);
passwordField1.setBounds(0, 40, 450, passwordField1.getPreferredSize().height);
//---- label2 ----
label2.setText(bundle.getString("ChangePassword.label2.text"));
contentPane.add(label2);
label2.setBounds(new Rectangle(new Point(5, 100), label2.getPreferredSize()));
contentPane.add(passwordField2);
passwordField2.setBounds(0, 130, 455, passwordField2.getPreferredSize().height);
//---- label3 ----
label3.setText(bundle.getString("ChangePassword.label3.text"));
contentPane.add(label3);
label3.setBounds(new Rectangle(new Point(5, 180), label3.getPreferredSize()));
contentPane.add(passwordField3);
passwordField3.setBounds(0, 210, 450, passwordField3.getPreferredSize().height);
//---- button1 ----
button1.setText(bundle.getString("ChangePassword.button1.text"));
contentPane.add(button1);
button1.setBounds(5, 295, 445, button1.getPreferredSize().height);
button1.addActionListener(new ActionListener() {@Override public void actionPerformed(ActionEvent e) {
//查看旧密码和老密码是否一致
//更改密码
String olps = Tools.getPassword(passwordField1);
String nlps = Tools.getPassword(passwordField2);
String onlps = Tools.getPassword(passwordField3);
if(olps.equals("")){
Tools.messageWindows("旧密码为空");
}else if(nlps.equals("")){
Tools.messageWindows("新密码 不能为空");
} else if(onlps.equals("")){
Tools.messageWindows("确认密码不能为空");
} else if(!nlps.equals(onlps)){
Tools.messageWindows("新密码和确认密码不一致");
}else{
//更改密码
String []data = {
nlps,
Account,
olps
};
try{
Connection conn = JdbcUtils.getConnection();
PreparedStatement pst = conn.prepareStatement("update s_admin set s_password = ? where s_account = ? and s_password = ?");
pst.setString(1,nlps);
pst.setString(2,Account);
pst.setString(3,olps);
int a = pst.executeUpdate();
if(a==0||a==-1){
Tools.messageWindows("账号不存在或者密码错误");
}else{
Tools.messageWindows("密码更改成功");
//更改密码成功,返回登录界面
dispose();
new test2().setVisible(true);
}
}catch (SQLException ex){
ex.printStackTrace();
}
}
}});
{
// compute preferred size
Dimension preferredSize = new Dimension();
for(int i = 0; i < contentPane.getComponentCount(); i++) {
Rectangle bounds = contentPane.getComponent(i).getBounds();
preferredSize.width = Math.max(bounds.x + bounds.width, preferredSize.width);
preferredSize.height = Math.max(bounds.y + bounds.height, preferredSize.height);
}
Insets insets = contentPane.getInsets();
preferredSize.width += insets.right;
preferredSize.height += insets.bottom;
contentPane.setMinimumSize(preferredSize);
contentPane.setPreferredSize(preferredSize);
}
pack();
setLocationRelativeTo(getOwner());
// JFormDesigner - End of component initialization //GEN-END:initComponents @formatter:on
}
// JFormDesigner - Variables declaration - DO NOT MODIFY //GEN-BEGIN:variables @formatter:off
// Generated using JFormDesigner Evaluation license - 刘
private JLabel label1;
private JPasswordField passwordField1;
private JLabel label2;
private JPasswordField passwordField2;
private JLabel label3;
private JPasswordField passwordField3;
private JButton button1;
// JFormDesigner - End of variables declaration //GEN-END:variables @formatter:on
}
(七)表格形式查找商品
在button20(查找商品)中添加监听事件,在商品编码处为空时,显示所有的商品信息,在编码处有编码时,显示特定商品的信息,在textfild3中输入进货数量,点击进货后,商品数量增加,实现进货功能,两个功能都需要连接到数据库,显示并修改数据库中的数据,此处用到了table表格来显示,更加清晰。代码如下所示
//定义一个表格
Object []columns = {"商品编码","商品名字","商品价格","商品数量"};
Table t1Table = new Table(columns);
JTable table = t1Table.getTables();//获取表格
JScrollPane JS = t1Table.getJScrollPane();//获取滚动条
DefaultTableModel model = t1Table.getModel();//表格控制权限
// JS.setPreferredSize(new Dimension(700,615));
panel1.add(JS);
JS.setBounds(5, 65, 700, 615);
//---- button20 ----
button20.setText(bundle.getString("Manager.button20.text"));
button20.addActionListener(new ActionListener() {@Override public void actionPerformed(ActionEvent e) {
//查找商品
if(textField2.getText().equals("")){
//查找全部
try{
Connection conn = JdbcUtils.getConnection();
Statement st = conn.createStatement();
String sql = "select * from s_stock";
ResultSet rs = st.executeQuery(sql);
Tools.addDataTable(rs,model,4);
}catch (SQLException ex){
ex.printStackTrace();
}
}else{
//查找单个
String id = textField2.getText();
try{
Connection conn = JdbcUtils.getConnection();
PreparedStatement pst = conn.prepareStatement("select * from jdbc.s_stock where s_id = ?");
pst.setString(1,id);
ResultSet rs = pst.executeQuery();
Tools.addDataTable(rs,model,4);
}catch (SQLException ex){
ex.printStackTrace();
}
}
}});
panel2.add(button20);
button20.setBounds(325, 5, 85, 35);
//---- button21 ----
button21.setText(bundle.getString("Manager.button21.text"));
button21.addActionListener(new ActionListener() {@Override public void actionPerformed(ActionEvent e) {
以上只是部分代码,需要源码的可以私信联系我,喜欢或者有帮助的话可以点赞关注支持一下!!