作品(通过窗体实现数据库的增删改)

定义对象:

package jdbc;

public class ShangPin {

private int id;
private String name;
private int buyInPrice;
private int unitPrice;
private int stock;

public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getBuyInPrice() {
return buyInPrice;
}
public void setBuyInPrice(int buyInPrice) {
this.buyInPrice = buyInPrice;
}
public int getUnitPrice() {
return unitPrice;
}
public void setUnitPrice(int unitPrice) {
this.unitPrice = unitPrice;
}
public int getStock() {
return stock;
}
public void setStock(int stock) {
this.stock = stock;
}
}

包装方法:

package jdbc;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

import javax.swing.JOptionPane;

public class ShangPinManager {

private final static String DRIVER = "com.mysql.jdbc.Driver";
private final static String URL = "jdbc:mysql://127.0.0.1:3306/e-market";
private final static String USER = "root";
private final static String PWD = "1992";

public int AddShangPin(ShangPin sp) throws ClassNotFoundException, SQLException{
int n = -1;
Class.forName(DRIVER);
Connection conn = DriverManager.getConnection(URL, USER, PWD);
java.sql.Statement st = conn.createStatement();
String sql = "insert into commodityinfo(CID,CName,InputPrice,OutputPrice,Amount)"
+"values(null,‘"+sp.getName()+"‘,‘"+sp.getBuyInPrice()+"‘,‘"
+sp.getUnitPrice()+"‘,‘"+sp.getStock()+"‘)";
n = st.executeUpdate(sql);
if(n>=1){
JOptionPane.showMessageDialog(null, "添加商品成功");
}else{
JOptionPane.showMessageDialog(null, "添加商品失败");
}
return n;
}

public int DeleteShangPin(ShangPin sp) throws ClassNotFoundException, SQLException{
int n = -1;
Class.forName(DRIVER);
Connection conn = DriverManager.getConnection(URL, USER, PWD);
java.sql.Statement st = conn.createStatement();
String sql = "delete from commodityinfo where CID="+sp.getId();
n = st.executeUpdate(sql);
if(n>=1){
JOptionPane.showMessageDialog(null, "删除商品成功");
}else{
JOptionPane.showMessageDialog(null, "删除商品失败");
}
return n;
}

public int ReplceShangPin(ShangPin sp) throws ClassNotFoundException, SQLException{
int n = -1;
Class.forName(DRIVER);
Connection conn = DriverManager.getConnection(URL, USER, PWD);
java.sql.Statement st = conn.createStatement();
String sql = "upDate commodityinfo set CName=‘"+sp.getName()+"‘, InputPrice="+sp.getBuyInPrice()+", "
+ "OutputPrice="+sp.getUnitPrice()+", Amount="+sp.getStock()+" where CID="+sp.getId();
n = st.executeUpdate(sql);
if(n>=1){
JOptionPane.showMessageDialog(null, "替换商品成功");
}else{
JOptionPane.showMessageDialog(null, "替换商品失败");
}
return n;
}
}

创建窗体并实例化对象:

package jdbc;

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import java.awt.GridLayout;
import java.awt.FlowLayout;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JLabel;
import java.awt.Font;
import javax.swing.UIManager;
import javax.swing.JTextField;
import javax.swing.LayoutStyle.ComponentPlacement;
import java.awt.Color;
import javax.swing.JButton;
import javax.swing.JTextArea;
import java.awt.event.ActionListener;
import java.sql.SQLException;
import java.awt.event.ActionEvent;
import javax.swing.ImageIcon;
import javax.swing.DropMode;
import javax.swing.SwingConstants;

public class Interface extends JFrame {

private static final long serialVersionUID = 1L;
private JPanel jp;
private JLabel labelID,labelName,labelBuyIn,labelPrice,labelStock;
private JButton buttonAdd,buttonReplce,buttonDelete;
private JTextField txtID,txtName,txtBuyIn,txtPrice,txtStock;
private ShangPinManager spm;
private JLabel lblNewLabel;
private JTextField textField;
private JTextField textField_1;
private JTextField textField_2;
private JTextField textField_3;

public static void main(String[] args) {
new Interface();
}

public Interface() {
super("商品管理系统");
jp = new JPanel();
jp.setBounds(0, 0, 375, 281);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 385, 309);
labelID = new JLabel("商品ID");
labelID.setBounds(10, 10, 64, 24);
labelID.setForeground(new Color(255, 0, 0));
labelID.setFont(new Font("楷体", Font.BOLD, 20));
txtID = new JTextField(10);
txtID.setBounds(92, 10, 141, 21);

labelName =new JLabel("商品名称");
labelName.setBounds(10, 44, 84, 24);
labelName.setForeground(new Color(255, 0, 0));
labelName.setFont(new Font("楷体", Font.BOLD, 20));
txtName = new JTextField(10);
txtName.setBounds(104, 44, 129, 21);

labelBuyIn =new JLabel("买入价格");
labelBuyIn.setBounds(10, 112, 84, 24);
labelBuyIn.setFont(new Font("楷体", Font.BOLD, 20));
labelBuyIn.setForeground(new Color(255, 0, 0));
txtBuyIn = new JTextField(10);
txtBuyIn.setBounds(104, 112, 129, 21);

labelPrice =new JLabel("商品单价");
labelPrice.setBounds(10, 78, 84, 24);
labelPrice.setFont(new Font("楷体", Font.BOLD, 20));
labelPrice.setForeground(new Color(255, 0, 0));
txtPrice = new JTextField(10);
txtPrice.setBounds(104, 78, 129, 21);

labelStock =new JLabel("商品库存");
labelStock.setBounds(10, 146, 84, 24);
labelStock.setFont(new Font("楷体", Font.BOLD, 20));
labelStock.setForeground(new Color(255, 0, 0));
txtStock = new JTextField(10);
txtStock.setBounds(104, 146, 129, 21);

spm = new ShangPinManager();

buttonAdd = new JButton("新增");
buttonAdd.setBounds(10, 206, 119, 43);
buttonAdd.setFont(new Font("黑体", Font.BOLD, 30));
buttonAdd.setForeground(new Color(255, 69, 0));
buttonAdd.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
// int id = Integer.valueOf(txtID.getText());
String name = txtName.getText();
int maiRu = Integer.valueOf(txtBuyIn.getText());
int danJia = Integer.valueOf(txtPrice.getText());
int kuCun = Integer.valueOf(txtStock.getText());

ShangPin sp = new ShangPin();
// sp.setId(id);
sp.setName(name);
sp.setBuyInPrice(maiRu);
sp.setUnitPrice(danJia);
sp.setStock(kuCun);
try {
int n = spm.AddShangPin(sp);
} catch (ClassNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
});

buttonDelete = new JButton("删除");
buttonDelete.setBounds(253, 206, 112, 43);
buttonDelete.setForeground(new Color(255, 255, 0));
buttonDelete.setFont(new Font("黑体", Font.BOLD, 30));
buttonDelete.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
int id = Integer.valueOf(txtID.getText());

ShangPin sp = new ShangPin();
sp.setId(id);
try {
int n = spm.DeleteShangPin(sp);
} catch (ClassNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
});

buttonReplce = new JButton("替换");
buttonReplce.setBounds(131, 206, 119, 43);
buttonReplce.setFont(new Font("黑体", Font.BOLD, 30));
buttonReplce.setForeground(new Color(255, 69, 0));
buttonReplce.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
int id = Integer.valueOf(txtID.getText());
String name = txtName.getText();
int maiRu = Integer.valueOf(txtBuyIn.getText());
int danJia = Integer.valueOf(txtPrice.getText());
int kuCun = Integer.valueOf(txtStock.getText());

ShangPin sp = new ShangPin();
sp.setId(id);
sp.setName(name);
sp.setBuyInPrice(maiRu);
sp.setUnitPrice(danJia);
sp.setStock(kuCun);
try {
int n = spm.ReplceShangPin(sp);
} catch (ClassNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
});

getContentPane().setLayout(null);
getContentPane().add(jp);
jp.setLayout(null);
jp.add(buttonAdd);
jp.add(buttonDelete);
jp.add(buttonReplce);
jp.add(labelPrice);
jp.add(txtPrice);
jp.add(labelName);
jp.add(txtName);
jp.add(labelID);
jp.add(txtID);
jp.add(labelBuyIn);
jp.add(txtStock);
jp.add(labelStock);
jp.add(txtBuyIn);

textField = new JTextField();
textField.setForeground(new Color(255, 20, 147));
textField.setFont(new Font("微软雅黑", Font.BOLD, 35));
textField.setText("\u5546");
textField.setBounds(301, 10, 41, 37);
jp.add(textField);
textField.setColumns(10);

textField_1 = new JTextField();
textField_1.setForeground(new Color(255, 20, 147));
textField_1.setFont(new Font("微软雅黑", Font.BOLD, 35));
textField_1.setText("\u54C1");
textField_1.setBounds(243, 44, 41, 37);
jp.add(textField_1);
textField_1.setColumns(10);

textField_2 = new JTextField();
textField_2.setForeground(new Color(255, 20, 147));
textField_2.setFont(new Font("微软雅黑", Font.BOLD, 35));
textField_2.setText("\u7BA1");
textField_2.setBounds(243, 99, 41, 37);
jp.add(textField_2);
textField_2.setColumns(10);

textField_3 = new JTextField();
textField_3.setForeground(new Color(255, 20, 147));
textField_3.setFont(new Font("微软雅黑", Font.BOLD, 35));
textField_3.setText("\u7406");
textField_3.setBounds(301, 126, 41, 43);
jp.add(textField_3);
textField_3.setColumns(10);

lblNewLabel = new JLabel("New label");
lblNewLabel.setIcon(new ImageIcon(Interface.class.getResource("/jdbc/2.jpg")));
lblNewLabel.setBounds(0, 0, 375, 271);
jp.add(lblNewLabel);
this.setVisible(true);
}
}

时间: 2024-12-20 14:53:47

作品(通过窗体实现数据库的增删改)的相关文章

【Visual Basic】vb6的ListView控件,对Access2003数据库的增删改查,判断是否有中文、多窗体操作

vb6对Access2003数据库的增删改查并不复杂,可以通过ado对象轻松完成,下面举个小例子,同时说明vb6中的ListView控件的使用.虽然在<[Visual Basic]列表控件ListView的增删改查.模态对话框.禁止窗口调整大小>曾经对VB.NET的ListView控件进行详细的说明,但是证明微软就是个坑爹货,vb6对于ListView实现的代码居然跟VB.NET有着彻底的不同,似乎换了一门语言似得的.改代码什么的最讨厌的. 首先,在vb6生成的工程文件夹中有着一个db1.md

C# winform窗体设计-对数据库执行增删改操作

对于学习数据库的人来说,数据库的增删改可谓是最基本的了(小编其实也只是一个小白=-=),这篇文章,小编将于大家讲解数据库增删改操作 在执行数据库增删改的时候主要使用的:Command 类       ExecuteNonQuery执行命令,此命令带有返回值(int) 下面介绍对数据库执行操作的具体步骤: (1)创建连接 (2)创建命令 (3)设置命令的连接和文本(sql语句) (4)打开连接 (5)执行命令 (6)使用命令返回的数据 (7)关闭连接 代码演示: 1 string s = "ser

C#中窗体间参数传递实现增删改的例子

此例子中传递的变量有string type,string text,储存在结构数组中:static int i储存在Sta类中(如果在外面声明,调用它老出错),里面有geti和seti函数来操纵它. 各个窗体实现功能及截图: 各窗体代码如下: using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using Syst

【Hibernate】Hibernate的在Eclipse+Mysql的配置、安装,纯Java,利用Annotation与HQL完成数据库的增删改查

这篇文章有很多槽点,在Hibernate4.x上面用着Hibernate3.x的写法去写.而且程序中放到Eclipse中会有一大堆警告,但是这好歹也在一定程度上完成了Hibernate的入门.毕竟现在很多介绍Hibernate的书籍都是用Hibernate3.x的写法去写.上次那篇<[Hibernate]最简单的Hibernate工程--账号注册系统>(点击打开链接)杂糅Struts的技术其实是不对的.因为Hibernate完成的是,从Java到数据库,从数据库到Java的任务.之后Java与

android中SQLite数据库的增删改查

1.数据库帮助类PersonSQLiteOpenHelper package com.wzw.sqllitedemo.db; import android.content.Context; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteDatabase.CursorFactory; import android.database.sqlite.SQLiteOpenHelper

Windows下安装MySQLdb, Python操作MySQL数据库的增删改查

这里的前提是windows上已经安装了MySQL数据库,且配置完毕,能正常建表能操作.在此基础上只需安装MySQL-python-1.2.4b4.win32-py2.7.exe就ok了,只有1M多.这个有点类似jdbc里的那个jar包. 下载链接:http://sourceforge.net/projects/mysql-python/ , 百度云盘 :http://pan.baidu.com/s/1dDgnfpR 密码:7bna 接着import MySQLdb就能使用了,下面给出测试代码:

TP框架中 数据库的增删改查

框架会用到数据库的内容,这一篇就是关于数据库的增删改查. 数据库的操作,无疑就是连接数据库,然后对数据库中的表进行各种查询,然后就是对数据的增删改的操作, 想要操作数据库,第一步必然是要:链接数据库 一.链接数据库 (1)找到模块文件夹中的Conf文件夹,然后进行编写config.php文件 我这里是这样的文件路径 (2)打开这个config.php文件,然后找到父类配置文件convention.php文件,将关于"数据库"的部分复制粘贴到config.php配置文件中(父类的conv

mysql笔记--数据库基本增删改查 修改表结构

数据库基本增删改查 1. 增-添加/插入数据,insert into 插入哪张表,那些列,什么值, 语句:insert into 表名(列1,列2,列3)values (值1,值2,值3): 可以不按原列的顺序插入,也可以插入部分列,但是值与列要一一对应,不能混乱!!! 一次插入多行数据 : Insert into 表名(列1,列2)values (值1,值2),(值1,值2): 2. 改-更新数据update 更新哪张表,哪些列,哪些值 语句:update 表名 set 列1=值1,列2=值2

java jdbc 连接mysql数据库 实现增删改查

好久没有写博文了,写个简单的东西热热身,分享给大家. jdbc相信大家都不陌生,只要是个搞java的,最初接触j2ee的时候都是要学习这么个东西的,谁叫程序得和数据库打交道呢!而jdbc就是和数据库打交道非常基础的一个知识,也是比较接近底层的,在实际的工作中大家用得更多的其实还是比较成熟的框架,例如Hibernate.Mybatis. 但是作为这些成熟框架的底层的jdbc却也是我们应该去掌握的,只有了解了jdbc的增删改查,这样在以后如果有兴趣去研究Hibernate或者Mybatis的源代码的