Java购物车swing_JAVA课程设计--购物车

1.码云GIT提交

2.设计思路

1,使用数据库对购物车的数据进行处理

2.分别使用sql语句来实现对购物车和商城物品的增删改查。

3.代码

package Main;

import java.awt.BorderLayout;

import java.awt.EventQueue;

import javax.swing.JFrame;

import javax.swing.JPanel;

import javax.swing.border.EmptyBorder;

import java.awt.FlowLayout;

import javax.swing.JTextField;

import java.awt.Color;

import javax.swing.JButton;

import javax.swing.SwingConstants;

import javax.swing.JLabel;

import java.awt.Font;

import java.awt.Frame;

import javax.swing.BoxLayout;

import java.awt.GridBagLayout;

import java.awt.GridLayout;

import com.jgoodies.forms.layout.FormLayout;

import com.jgoodies.forms.layout.ColumnSpec;

import com.jgoodies.forms.layout.RowSpec;

import java.awt.event.ActionListener;

import java.awt.event.ActionEvent;

import java.awt.event.MouseAdapter;

import java.awt.event.MouseEvent;

import javax.swing.event.ChangeListener;

import javax.swing.event.ChangeEvent;

public class Fframe extends JFrame {

private JTextField textField;

private JTextField textField_1;

private JPanel contentPane;

private JPanel contentPane1;

/**

* Launch the application.

*/

public static void main(String[] args) {

EventQueue.invokeLater(new Runnable() {

public void run() {

try {

Fframe frame = new Fframe();

frame.setVisible(true);

} catch (Exception e) {

e.printStackTrace();

}

}

});

}

/**

* Create the frame.

*/

public Fframe() {

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

setBounds(100, 100, 432, 376);

contentPane = new JPanel();

contentPane.setForeground(Color.BLUE);

contentPane.setBorder(new EmptyBorder(5, 14, 5, 5));

setContentPane(contentPane);

contentPane.setLayout(null);

JLabel lblNewLabel = new JLabel("购物系统");

lblNewLabel.setForeground(Color.BLUE);

lblNewLabel.setFont(new Font("微软雅黑", Font.PLAIN, 25));

lblNewLabel.setBounds(165, 10, 100, 55);

contentPane.add(lblNewLabel);

JButton btnNewButton = new JButton("购买商品");

btnNewButton.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {

Frame1 d = new Frame1();

d.setVisible(true);

dispose();

}

});

btnNewButton.addMouseListener(new MouseAdapter() {

GetGoods s = new GetGoods();

Menu m = new Menu();

@Override

public void mouseClicked(MouseEvent e) {

m.Menu();

}

});

btnNewButton.setFont(new Font("微软雅黑", Font.PLAIN, 16));

btnNewButton.setForeground(Color.BLUE);

btnNewButton.setBounds(128, 64, 165, 36);

contentPane.add(btnNewButton);

JButton button = new JButton("查看购物车");

button.addMouseListener(new MouseAdapter() {

@Override

public void mouseClicked(MouseEvent e) {

CarFrame d = new CarFrame();

d.setVisible(true);

dispose();

}

});

button.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {

}

});

button.setForeground(Color.BLUE);

button.setFont(new Font("微软雅黑", Font.PLAIN, 16));

button.setBounds(128, 108, 165, 36);

contentPane.add(button);

JButton button_3 = new JButton("商家系统");

button_3.addMouseListener(new MouseAdapter() {

@Override

public void mouseClicked(MouseEvent e) {

BussinessFrame d = new BussinessFrame();

d.setVisible(true);

dispose();

}

});

button_3.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {

}

});

button_3.setForeground(Color.BLUE);

button_3.setFont(new Font("微软雅黑", Font.PLAIN, 16));

button_3.setBounds(128, 154, 165, 36);

contentPane.add(button_3);

JButton button_4 = new JButton("结账");

button_4.addMouseListener(new MouseAdapter() {

@Override

public void mouseClicked(MouseEvent e) {

dispose();

}

});

button_4.setForeground(Color.BLUE);

button_4.setFont(new Font("微软雅黑", Font.PLAIN, 16));

button_4.setBounds(128, 212, 165, 36);

contentPane.add(button_4);

}

}

package Main;

import java.awt.BorderLayout;

import java.awt.EventQueue;

import javax.swing.JFrame;

import javax.swing.JPanel;

import javax.swing.border.EmptyBorder;

import javax.swing.text.BadLocationException;

import com.mysql.jdbc.Statement;

import javax.swing.JLabel;

import java.awt.Color;

import java.awt.Font;

import java.awt.List;

import javax.swing.JTextField;

import javax.swing.JButton;

import java.awt.event.MouseAdapter;

import java.awt.event.MouseEvent;

import java.io.IOException;

import java.sql.Connection;

import java.sql.DriverManager;

import java.sql.ResultSet;

import java.sql.SQLException;

import java.util.ArrayList;

import java.util.Scanner;

import java.awt.event.ActionListener;

import java.awt.event.ActionEvent;

import javax.swing.JTextArea;

public class Frame1 extends JFrame {

private JPanel contentPane;

private JTextField textField;

/**

* Launch the application.

*/

public static void main(String[] args) {

EventQueue.invokeLater(new Runnable() {

public void run() {

try {

Frame1 frame = new Frame1();

frame.setVisible(true);

} catch (Exception e) {

e.printStackTrace();

}

}

});

}

/**

* Create the frame.

*/

public Frame1() {

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

setBounds(100, 100, 450, 461);

contentPane = new JPanel();

contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));

setContentPane(contentPane);

contentPane.setLayout(null);

JLabel lblNewLabel = new JLabel("请输入您要购买的商品id:");

lblNewLabel.setFont(new Font("微软雅黑", Font.PLAIN, 22));

lblNewLabel.setForeground(Color.BLUE);

lblNewLabel.setBounds(10, 280, 273, 59);

contentPane.add(lblNewLabel);

JTextField textField_1 = new JTextField();

String value = textField_1.getText().trim();

textField = new JTextField();

Menu m = new Menu();

textField.setBounds(10, 10, 414, 59);

contentPane.add(textField);

textField.setColumns(10);

System.out.println(value);

textField_1.setBounds(278, 295, 73, 38);

contentPane.add(textField_1);

textField_1.setColumns(10);

JButton btnNewButton = new JButton("购买");

AddCarGoods g = new AddCarGoods();

btnNewButton.addMouseListener(new MouseAdapter() {

@Override

public void mouseClicked(MouseEvent e) {

g.Addcargoods(Integer.parseInt(textField_1.getText().trim()));

}

});

btnNewButton.setBounds(359, 274, 65, 59);

contentPane.add(btnNewButton);

JTextArea textArea = new JTextArea();

textArea.setBounds(10, 79, 414, 156);

contentPane.add(textArea);

JButton btnNewButton_1 = new JButton("查询可购买商品");

btnNewButton_1.addMouseListener(new MouseAdapter() {

String url = "jdbc:mysql://localhost:3306/goods";

String userName = "root";

String password = "kamisama";

String driverName = "com.mysql.jdbc.Driver";

String strSql = "select * from good";

Scanner sc = new Scanner(System.in);

Connection con = null;

Statement pstmt = null;

ResultSet rs = null;

String driverName1 = "com.mysql.jdbc.Driver";

@Override

public void mouseClicked(MouseEvent e) {

try {

Class.forName(driverName);

con = (Connection) DriverManager.getConnection(url, userName, password);

pstmt = (Statement) con.createStatement();

rs = pstmt.executeQuery(strSql);

while (rs.next()) {

int id = rs.getInt("id");

String name = rs.getString("name");

Double price = rs.getDouble("price");

int num = rs.getInt("num");

String description = rs.getString("description");

System.out.print("id=" + id + " name="+name+ " price="+price+" num=" + num+" description="+description+"\n");

String str = "id=" + id + " name="+name+ " price="+price+" num=" + num+" description="+description+"\n";

textArea.append(str);

}

} catch (ClassNotFoundException e1) {

// TODO Auto-generated catch block

e1.printStackTrace();

}catch (SQLException e1){

e1.printStackTrace();

}finally {

if (rs != null)

try {

rs.close();

} catch (Exception e1) {

e1.printStackTrace();

}

if (pstmt != null){

try {

pstmt.close();// 关闭语句

} catch (Exception e1) {

e1.printStackTrace();

}

}

if (con != null) {

try {

con.close();// 关闭连接

} catch (Exception e1) {

e1.printStackTrace();

}

}

}

}

});

btnNewButton_1.setBounds(20, 342, 279, 49);

contentPane.add(btnNewButton_1);

JButton btnNewButton_2 = new JButton("返回");

btnNewButton_2.addMouseListener(new MouseAdapter() {

@Override

public void mouseClicked(MouseEvent e) {

Fframe f = new Fframe();

f.setVisible(true);

dispose();

}

});

btnNewButton_2.setBounds(309, 343, 93, 48);

contentPane.add(btnNewButton_2);

}

}

package Main;

import java.awt.BorderLayout;

import java.awt.EventQueue;

import javax.swing.JFrame;

import javax.swing.JPanel;

import javax.swing.border.EmptyBorder;

import com.mysql.jdbc.Statement;

import javax.swing.JTextField;

import javax.swing.JButton;

import java.awt.event.ActionListener;

import java.awt.event.MouseAdapter;

import java.awt.event.MouseEvent;

import java.sql.Connection;

import java.sql.DriverManager;

import java.sql.ResultSet;

import java.sql.SQLException;

import java.util.Scanner;

import java.awt.event.ActionEvent;

import javax.swing.JLabel;

import java.awt.Color;

import java.awt.Font;

import javax.swing.JTextArea;

public class CarFrame extends JFrame {

private JPanel contentPane;

private JTextField textField;

private JTextField textField_1;

private JTextField textField_2;

private JTextField textField_3;

/**

* Launch the application.

*/

public static void main(String[] args) {

EventQueue.invokeLater(new Runnable() {

public void run() {

try {

CarFrame frame = new CarFrame();

frame.setVisible(true);

} catch (Exception e) {

e.printStackTrace();

}

}

});

}

/**

* Create the frame.

*/

public CarFrame() {

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

setBounds(100, 100, 450, 420);

contentPane = new JPanel();

contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));

setContentPane(contentPane);

contentPane.setLayout(null);

textField = new JTextField();

textField.setBounds(33, 10, 369, 32);

contentPane.add(textField);

textField.setColumns(10);

JTextArea textArea = new JTextArea();

textArea.setBounds(10, 58, 424, 110);

contentPane.add(textArea);

JButton btnNewButton = new JButton("查看购物车商品");

btnNewButton.addMouseListener(new MouseAdapter() {

String url = "jdbc:mysql://localhost:3306/goods";

String userName = "root";

String password = "kamisama";

String driverName = "com.mysql.jdbc.Driver";

String strSql = "select * from cars";

Scanner sc = new Scanner(System.in);

Connection con = null;

Statement pstmt = null;

ResultSet rs = null;

String driverName1 = "com.mysql.jdbc.Driver";

@Override

public void mouseClicked(MouseEvent e) {

try {

Class.forName(driverName);

con = (Connection) DriverManager.getConnection(url, userName, password);

pstmt = (Statement) con.createStatement();

rs = pstmt.executeQuery(strSql);

while (rs.next()) {

int id = rs.getInt("id");

String name = rs.getString("name");

Double price = rs.getDouble("price");

int num = rs.getInt("num");

String description = rs.getString("description");

double sum = rs.getDouble("sum");

System.out.print("id=" + id + " name="+name+ " price="+price+" num=" + num+" description="+description+" sum="+sum+"\n");

String str = "id=" + id + " name="+name+ " price="+price+" num=" + num+" description="+description+" sum="+sum+"\n";

textArea.append(str);

}

} catch (ClassNotFoundException e1) {

// TODO Auto-generated catch block

e1.printStackTrace();

}catch (SQLException e1){

e1.printStackTrace();

}finally {

if (rs != null)

try {

rs.close();

} catch (Exception e1) {

e1.printStackTrace();

}

if (pstmt != null){

try {

pstmt.close();// 关闭语句

} catch (Exception e1) {

e1.printStackTrace();

}

}

if (con != null) {

try {

con.close();// 关闭连接

} catch (Exception e1) {

e1.printStackTrace();

}

}

}

}

});

btnNewButton.setBounds(10, 178, 170, 32);

contentPane.add(btnNewButton);

JButton btnNewButton_1 = new JButton("返回");

btnNewButton_1.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {

Fframe f = new Fframe();

f.setVisible(true);

dispose();

}

});

btnNewButton_1.setBounds(227, 178, 170, 32);

contentPane.add(btnNewButton_1);

JLabel lblNewLabel = new JLabel("请输入您要删除的购物车商品id");

lblNewLabel.setFont(new Font("微软雅黑", Font.PLAIN, 14));

lblNewLabel.setForeground(Color.BLUE);

lblNewLabel.setBounds(20, 220, 214, 31);

contentPane.add(lblNewLabel);

textField_1 = new JTextField();

JTextField textField_1 = new JTextField();

String value = textField_1.getText().trim();

textField_1.setBounds(237, 220, 85, 31);

contentPane.add(textField_1);

textField_1.setColumns(10);

JButton btnNewButton_2 = new JButton("确认删除");

DeleteCarGoods d = new DeleteCarGoods();

btnNewButton_2.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {

d.DeleteCargoods(Integer.parseInt(textField_1.getText().trim()));

}

});

btnNewButton_2.setBounds(331, 221, 93, 31);

contentPane.add(btnNewButton_2);

JLabel lblid = new JLabel("请输入您要修改数量的购物车商品id");

lblid.setForeground(Color.BLUE);

lblid.setFont(new Font("微软雅黑", Font.PLAIN, 14));

lblid.setBounds(10, 285, 233, 31);

contentPane.add(lblid);

textField_2 = new JTextField();

textField_2.setColumns(10);

textField_2.setBounds(237, 286, 85, 31);

contentPane.add(textField_2);

JLabel label = new JLabel("请输入您要修改数量的购物车商品数量");

label.setForeground(Color.BLUE);

label.setFont(new Font("微软雅黑", Font.PLAIN, 14));

label.setBounds(10, 340, 244, 31);

contentPane.add(label);

textField_3 = new JTextField();

textField_3.setColumns(10);

textField_3.setBounds(251, 346, 85, 31);

contentPane.add(textField_3);

UpdataNum u = new UpdataNum();

JButton button_1 = new JButton("确认");

button_1.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {

String value1 = textField_2.getText().trim();

String value2 = textField_3.getText().trim();

u.updata(Integer.parseInt(value1), Integer.parseInt(value2));

}

});

button_1.setBounds(341, 345, 93, 31);

contentPane.add(button_1);

}

}

package Main;

import java.awt.BorderLayout;

import java.awt.EventQueue;

import javax.swing.JFrame;

import javax.swing.JPanel;

import javax.swing.border.EmptyBorder;

import javax.swing.JButton;

import java.awt.Color;

import java.awt.Font;

import java.awt.event.ActionListener;

import java.awt.event.ActionEvent;

public class BussinessFrame extends JFrame {

private JPanel contentPane;

/**

* Launch the application.

*/

public static void main(String[] args) {

EventQueue.invokeLater(new Runnable() {

public void run() {

try {

BussinessFrame frame = new BussinessFrame();

frame.setVisible(true);

} catch (Exception e) {

e.printStackTrace();

}

}

});

}

/**

* Create the frame.

*/

public BussinessFrame() {

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

setBounds(100, 100, 450, 300);

contentPane = new JPanel();

contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));

setContentPane(contentPane);

contentPane.setLayout(null);

JButton btnNewButton = new JButton("上架商品");

btnNewButton.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {

SellFrame f = new SellFrame();

f.setVisible(true);

dispose();

}

});

btnNewButton.setFont(new Font("微软雅黑", Font.PLAIN, 24));

btnNewButton.setForeground(Color.BLUE);

btnNewButton.setBounds(70, 62, 299, 52);

contentPane.add(btnNewButton);

JButton button = new JButton("下架商品");

button.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {

DownGoodsFrame f = new DownGoodsFrame();

f.setVisible(true);

dispose();

}

});

button.setFont(new Font("微软雅黑", Font.PLAIN, 24));

button.setForeground(Color.BLUE);

button.setBounds(70, 124, 299, 60);

contentPane.add(button);

JButton btnNewButton_1 = new JButton("返回");

btnNewButton_1.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {

Fframe f = new Fframe();

f.setVisible(true);

dispose();

}

});

btnNewButton_1.setFont(new Font("微软雅黑", Font.PLAIN, 24));

btnNewButton_1.setForeground(Color.BLUE);

btnNewButton_1.setBounds(309, 194, 103, 43);

contentPane.add(btnNewButton_1);

}

}

package Main;

import java.awt.BorderLayout;

import java.awt.EventQueue;

import javax.swing.JFrame;

import javax.swing.JPanel;

import javax.swing.border.EmptyBorder;

import javax.swing.JLabel;

import java.awt.Color;

import java.awt.Font;

import javax.swing.JTextField;

import javax.swing.JButton;

import java.awt.event.MouseAdapter;

import java.awt.event.MouseEvent;

import java.awt.event.ActionListener;

import java.awt.event.ActionEvent;

public class SellFrame extends JFrame {

private JPanel contentPane;

private JTextField textField;

private JTextField textField_1;

private JTextField textField_2;

private JTextField textField_3;

/**

* Launch the application.

*/

public static void main(String[] args) {

EventQueue.invokeLater(new Runnable() {

public void run() {

try {

SellFrame frame = new SellFrame();

frame.setVisible(true);

} catch (Exception e) {

e.printStackTrace();

}

}

});

}

/**

* Create the frame.

*/

public SellFrame() {

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

setBounds(100, 100, 450, 300);

contentPane = new JPanel();

contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));

setContentPane(contentPane);

contentPane.setLayout(null);

JLabel lblNewLabel = new JLabel("请输入您的商品名称");

lblNewLabel.setFont(new Font("微软雅黑", Font.PLAIN, 24));

lblNewLabel.setForeground(Color.BLUE);

lblNewLabel.setBounds(22, 31, 224, 36);

contentPane.add(lblNewLabel);

JLabel label = new JLabel("请输入您的商品单价");

label.setForeground(Color.BLUE);

label.setFont(new Font("微软雅黑", Font.PLAIN, 24));

label.setBounds(22, 73, 224, 36);

contentPane.add(label);

JLabel label_1 = new JLabel("请输入您的商品数量");

label_1.setForeground(Color.BLUE);

label_1.setFont(new Font("微软雅黑", Font.PLAIN, 24));

label_1.setBounds(22, 116, 224, 36);

contentPane.add(label_1);

JLabel label_2 = new JLabel("请输入您的商品描述");

label_2.setForeground(Color.BLUE);

label_2.setFont(new Font("微软雅黑", Font.PLAIN, 24));

label_2.setBounds(22, 162, 224, 36);

contentPane.add(label_2);

textField = new JTextField();

textField.setBounds(267, 31, 157, 34);

contentPane.add(textField);

textField.setColumns(10);

textField_1 = new JTextField();

textField_1.setColumns(10);

textField_1.setBounds(267, 73, 157, 34);

contentPane.add(textField_1);

textField_2 = new JTextField();

textField_2.setColumns(10);

textField_2.setBounds(267, 116, 157, 34);

contentPane.add(textField_2);

textField_3 = new JTextField();

textField_3.setColumns(10);

textField_3.setBounds(267, 162, 157, 34);

contentPane.add(textField_3);

JButton btnNewButton = new JButton("返回");

btnNewButton.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {

Fframe f = new Fframe();

f.setVisible(true);

dispose();

}

});

btnNewButton.setFont(new Font("微软雅黑", Font.PLAIN, 24));

btnNewButton.setForeground(Color.BLUE);

btnNewButton.setBounds(293, 206, 117, 45);

contentPane.add(btnNewButton);

JButton button = new JButton("确认");

button.addMouseListener(new MouseAdapter() {

GoodsSQL s = new GoodsSQL();

@Override

public void mouseClicked(MouseEvent e) {

String value1 = textField.getText().trim();

String value2 = textField_1.getText().trim();

String value3 = textField_2.getText().trim();

String value4 = textField_3.getText().trim();

s.Sell(value1, Double.parseDouble(value2), Integer.parseInt(value3), value4);

}

});

button.setForeground(Color.BLUE);

button.setFont(new Font("微软雅黑", Font.PLAIN, 24));

button.setBounds(22, 206, 117, 45);

contentPane.add(button);

}

}

package Main;

import java.sql.Connection;

import java.sql.DriverManager;

import java.sql.ResultSet;

import java.sql.SQLException;

import java.util.Scanner;

public class AddCarGoods {

String url = "jdbc:mysql://localhost:3306/goods";

String userName = "root";

String password = "kamisama";

String driverName = "com.mysql.jdbc.Driver";

String strSql1 = "insert into cars(name,price,num,description) select name,price,num,description from good where id = ?";

String strSql2 = "updates cars et sum=numprice where id = ?";

Connection con1 = null;

java.sql.PreparedStatement pstmt1 = null;

java.sql.PreparedStatement pstmt2 = null;

ResultSet rs = null;

String driverName1 = "com.mysql.jdbc.Driver";

Scanner sc = new Scanner(System.in);

public void Addcargoods(int i){

try {

Class.forName(driverName);// jdbc4.0 后无需使用这句进行驱动注册操作

con1 =DriverManager.getConnection(url, userName, password); // 根据参数的插入数据

pstmt1 = con1.prepareStatement(strSql1);

pstmt1.setInt(1, i);

pstmt2 = con1.prepareStatement(strSql2);

pstmt2.setInt(1, i);

pstmt1.executeUpdate();

pstmt2.executeUpdate();

} catch (SQLException e) {

e.printStackTrace();

} catch (ClassNotFoundException e) {

e.printStackTrace();

} finally {/下面资源释放,可以使用try..with..resources语法简化*/

if (rs != null) {

try {

rs.close();

rs.close();

} catch (SQLException e) {

e.printStackTrace();

}

}

if (pstmt1 != null) {

try {

pstmt1.close();

pstmt1 = null;

} catch (SQLException e) {

e.printStackTrace();

}

}

if (con1 != null)

try {

con1.close();

con1 = null;

} catch (SQLException e) {

e.printStackTrace();

}

}

}

}

package Main;

import java.sql.Connection;

import java.sql.DriverManager;

import java.sql.ResultSet;

import java.sql.SQLException;

import java.util.Scanner;

import com.mysql.jdbc.PreparedStatement;

public class GoodsSQL{

String url = "jdbc:mysql://localhost:3306/goods";

String userName = "root";

String password = "kamisama";

String driverName = "com.mysql.jdbc.Driver";

String strSql = "insert into good(name,price,num,description) values(?,?,?,?)";

Scanner sc = new Scanner(System.in);

Connection con = null;

PreparedStatement pstmt = null;

ResultSet rs = null;

String driverName1 = "com.mysql.jdbc.Driver";

public void Sell(String name,double price,int num,String description){ //上架商品

try {

Class.forName(driverName1);// 加载MySql的驱动程序

con = (Connection) DriverManager.getConnection(url, userName, password); // 根据参数的插入数据

pstmt = (PreparedStatement) con.prepareStatement(strSql);

pstmt.setString(1, name);

pstmt.setDouble(2,price);

pstmt.setInt(3, num);

pstmt.setString(4, description);

pstmt.executeUpdate();

} catch (ClassNotFoundException e) {

// TODO Auto-generated catch block

e.printStackTrace();

} catch (SQLException e){

e.printStackTrace();

}finally {

if (rs != null)

try {

rs.close();

} catch (Exception e) {

e.printStackTrace();

}

if (pstmt != null){

try {

pstmt.close();// 关闭语句*/

} catch (Exception e) {

e.printStackTrace();

}

}

if (con != null) {

try {

con.close();// 关闭连接

} catch (Exception e) {

e.printStackTrace();

}

}

}

}

}

package Main;

import java.sql.Connection;

import java.sql.DriverManager;

import java.sql.ResultSet;

import java.sql.SQLException;

import java.util.Scanner;

public class DeleteGoods{

String url = "jdbc:mysql://localhost:3306/goods";

String userName = "root";

String password = "kamisama";

String driverName = "com.mysql.jdbc.Driver";

String strSql = "delete from good where id = ?";

Scanner sc = new Scanner(System.in);

Connection con1 = null;

java.sql.PreparedStatement pstmt1 = null;

ResultSet rs1 = null;

String driverName2 = "com.mysql.jdbc.Driver";

public void delete(int i){

try {

Class.forName(driverName2);// 加载MySql的驱动程序

con1 = (Connection) DriverManager.getConnection(url, userName, password); // 根据参数的插入数据

pstmt1 = con1.prepareStatement(strSql);

pstmt1.setInt(1, i);

pstmt1.executeUpdate();

} catch (ClassNotFoundException e) {

// TODO Auto-generated catch block

e.printStackTrace();

} catch (SQLException e){

e.printStackTrace();

}finally {

if (rs1 != null)

try {

rs1.close();

} catch (Exception e) {

e.printStackTrace();

}

if (pstmt1 != null){

try {

pstmt1.close();// 关闭语句*/

} catch (Exception e) {

e.printStackTrace();

}

}

if (con1 != null) {

try {

con1.close();// 关闭连接

} catch (Exception e) {

e.printStackTrace();

}

}

}

}

}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/352995.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

[python] 列表解析式的高效与简洁

方法一(列表解析式): list1 ["abc","efg","hij"] list2 [i[0] for i in list1] print list2方法二(普通写法): list1 ["abc","efg","hij"…

c++异常处理机制示例及讲解

这两天我写了一个测试c异常处理机制的例子,感觉有很好的示范作用,在此贴出来,给c异常处理的初学者入门。本文后附有c异常的知识普及,有兴趣者也可以看看。 下面的代码直接贴到你的console工程中&am…

mysql数据库char类型长度_mysql数据库设计字符类型及长度

1.数字类型小数的我就不聊了,因为有小数点的一般都是用字符串保存。关于整数,有几种可以选TINYINT、SMALLINT、MEDIUMINT、INT和BIGINT,分别占1、2、4、8字节。如果无符号,则其最大为255、65535、16777215、4294967295和184467440…

隐藏响应的server,X-Powered-By

隐藏X-Powered-By 修改 php.ini 文件 设置 expose_php Off apache 隐藏 server 修改httpd.conf 设置 ServerSignature Off ServerTokens Prod nginx 隐藏 server 修改nginx.conf 在http里面设置 server_tokens off;转载于:https://www.cnblogs.com/ouruola863/p/9519500.ht…

javaone_JavaOne 2012:掌握Java部署

javaone在为另一场JavaOne 2012午餐吃了意大利经典组合后,我前往希尔顿帝国宴会厅B观看了演示“掌握Java部署”。 来自Oracle的发言人是Mark Howe和Igor Nekrestyano Howe表示,部署团队的目标是帮助Java开发人员将其应用程序部署到所选平台。 他首先讨论…

C++异常处理机制详解

异常处理是一种允许两个独立开发的程序组件在程序执行期间遇到程序不正常的情况(异常exception)时相互通信的机制。本文总结了19个C异常处理中的常见问题,基本涵盖了一般C程序开发所需的关于异常处理部分的细节。 1. throw可以抛出哪些种类…

Github pull request 工作流总结

github 上面有很多非常不错的开源项目,我们也可以向其贡献自己的代码,那么我们如何提交自己的代码给开源项目呢?这里就要用到 pull request 的提交方式。当然,基于 git 的其他平台也是类似的用法。 假设源仓库为:https…

[MEGA DEAL] Java编程-硕士课程(85%折扣)

获得有关Java所有事物的高级分步指导 嘿,怪胎, 本周,在我们的JCG Deals商店中 ,我们提供了一个极端的报价 。 我们提供的Java编程–硕士课程 仅售29美元,而不是原始价格149美元 ,是的,可享受…

java中对象别名使用_JAVA中的别名现象

问题的提出:在java中,对基本数据类型的赋值时,是将数据从一个地方复制到另外一个地方,当ab时,将b的内容复制给a,若修改a时,b并不会受到这种修改的影响。在对对象进行赋值时,当我们对一个对象进行…

C/C++中的运算符优先级总结

C语言中的运算符 说明 运算符 结合性 初等运算符 () [] -> . -> 单目运算符 ! ~ -- - (类型) * & sizeof <- 算术运算符 * / % -> 算术运算符 - -> 移位运算符 << >> -> 关系运算符 > > < < -> 关系运算符 ! -> 按位与…

nginx 直接在配置文章中设置日志分割

直接在nginx配置文件中&#xff0c;配置日志循环&#xff0c;而不需使用logrotate或配置cron任务。需要使用到$time_iso8601 内嵌变量来获取时间。$time_iso8601格式如下&#xff1a;2015-08-07T18:12:0202:00。然后使用正则表达式来获取所需时间的数据。 按天分割日志 使用下面…

javaone_JavaOne 2012:JavaOne技术主题演讲

javaoneMark Reinhold从JavaOne 2012技术主题演讲开始。 他说&#xff0c;今年的版本将有所不同&#xff0c;因为它将使用大致相同的示例来说明Java的各个方面&#xff0c;而不是对Java的每个组件进行单独的单独介绍。 JavaFX团队的Richard Bair和Jasper Potts &#xff08;并与…

java sqlserver 死锁_sqlserver数据库发生死锁处理

SQLSERVER数据库锁表1. 查看被锁的表select request_session_id spid,OBJECT_NAME(resource_associated_entity_id) tableNamefrom sys.dm_tran_locks where resource_typeOBJECT2. 解除表的锁定declare spid intSet spid 57 --锁表进程declare sql varchar(1000)set sqlk…

setjump和longjump

int setjmp( jmp_buf env );void longjmp( jmp_buf env, int value );# setjmp(j)设置“jump”点&#xff0c;用正确的程序上下文填充jmp_buf 对象j。这个上下文包括程序存放位置、栈和框架指针&#xff0c;其它重要的寄存器和内存数据。当初始化完jump 的上下文&#xff0c;se…

jmeter操作数据库

1) jmeter不能直接连数据库&#xff0c;需要先添加jar包。 然后将jar包的路径添加到下图&#xff1a; 2) 操作数据库之前要知道数据库的信息&#xff08;ip、端口号、账号、密码&#xff09;&#xff0c;操作哪个数据库就连哪个&#xff1a; 在配置元件-JDBC Connectio…

使用Hystrix DSL创建弹性骆驼应用程序

Apache Camel是一个成熟的集成库&#xff08;到现在已有9年的历史了&#xff09;&#xff0c;它实现了Enterprise Integration Patterns一书中的所有模式。 但是Camel不仅是EIP实现库&#xff0c;它还是一个不断发展&#xff0c;添加新模式并适应行业变化的现代框架。 除了在每…

php7 对象转数组,php7中为对象/关联数组进行解构赋值

在CoffeeScript&#xff0c;Clojure&#xff0c;ES6和许多其他语言中&#xff0c;我们对对象/贴图/等进行了解构&#xff0c;如下所示&#xff1a;obj {keyA: Hello from A, keyB: Hello from B}{keyA, keyB} obj我在php中找到了这个list函数&#xff0c;可以让你像这样构造数…

虚函数表

虚函数   C中的虚函数的实现一般是通过虚函数表(C规范并没有规定具体用哪种方法&#xff0c;但大部分的编译器厂商都选择此方法)。 类的虚函数表是一块连续的内存&#xff0c;每个内存单元中记录一个JMP指令的地址。 注意的是&#xff0c;编译器会为每个有虚函数的类创建一个…

linux bash tutorial

bash read-special-keys-in-bash xdotool linux 登录启动顺序转载于:https://www.cnblogs.com/shaohef/p/9528927.html

centos7安装php8,centos8安装php7.4

一&#xff0c;下载php7.41&#xff0c;官方网站:https://www.php.net/2,下载[rootyjweb source]# wget https://www.php.net/distributions/php-7.4.2.tar.gz说明&#xff1a;在linux上以编译方式安装软件时&#xff0c;多数人都习惯把软件安装到 /usr/local目录下&#xff0c…