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();
}
}
}
}
}

posted @ 2018-01-26 14:09  网络1612林裕恒  阅读(403)  评论(0编辑  收藏  举报