Java實(shí)戰(zhàn)項(xiàng)目(1):swing圖書(shū)管理系統(tǒng)的登錄,注冊(cè),找回密碼,增刪查,個(gè)人解析等

SQL部分:
drop table yonghu, shangpin,dingdanxiangqing,sp_Type
drop table shangpin
select * from yonghu;
select * from shangpin;
select * from sp_Type;
create table yonghu(--創(chuàng)建用戶表
? yh_ID int primary key identity(1,1),
? yh_Uname varchar(30) not null,
? yh_Pwd varchar(30) not null,
? yh_Name varchar(30) not null,
? yh_Age int,
? yh_Sex varchar(20),
? yh_Phone varchar(100),
? yh_Address varchar(200),
? yh_Jieshao varchar(500),
? ?yh_Q varchar(500),
? ? yh_AQ varchar(500)
);
insert into yonghu values('1','11','詩(shī)書(shū)畫(huà)唱1',21,'男','19999999999','江西','陽(yáng)光帥氣的男孩子','你愛(ài)好多嗎?','多');
insert into yonghu values('2','22','詩(shī)書(shū)畫(huà)唱2',22,'男','29999999999','北京','陽(yáng)光帥氣的男孩子','你看過(guò)的書(shū)多嗎?','多');
insert into yonghu values('3','33','詩(shī)書(shū)畫(huà)唱3',23,'男','39999999999','上海','陽(yáng)光帥氣的男孩子','你會(huì)的技能多嗎?','多');
insert into yonghu values('4','44','詩(shī)書(shū)畫(huà)唱4',24,'男','49999999999','湖南','陽(yáng)光帥氣的男孩子','你喜歡的動(dòng)漫嗎?','多');
insert into yonghu values('5','55','詩(shī)書(shū)畫(huà)唱5',25,'男','59999999999','浙江','陽(yáng)光帥氣的男孩子','你喜歡的歌曲多嗎?','多');
create table sp_Type(--商品類(lèi)型表
sp_TypeID int primary key identity(1,1),
sp_TypeName varchar(100) not null
);
insert into sp_Type values('水果');
insert into sp_Type values('零食');
insert into sp_Type values('小吃');
insert into sp_Type values('日常用品');
create table shangpin(--商品表
? sp_ID int primary key identity(1,1),
? sp_Name varchar(100) not null,
? sp_Price decimal(10,2) not null,
? sp_TypeID int,
? sp_Jieshao varchar(300)
);
insert into shangpin values('蘋(píng)果',12,1,'好吃的蘋(píng)果');
insert into shangpin values('香蕉',2,1,'好吃的香蕉');
insert into shangpin values('橘子',4,1,'好吃的橘子');
insert into shangpin values('娃哈哈',3,2,'好吃營(yíng)養(yǎng)好');
insert into shangpin values('牙刷',5,4,'全自動(dòng)牙刷');
drop table dingdan
create table dingdan(
? dingdan_ID int primary key identity(1,1),
? yh_ID int,
? dingdanxiangqing_ID int
)
insert into dingdan values(1,1);
insert into dingdan values(1,2);
insert into dingdan values(1,3);
insert into dingdan values(2,4);
insert into dingdan values(2,5);
insert into dingdan values(2,6);
insert into dingdan values(3,7);
create table dingdanxiangqing(
? dingdanxiangqing_ID int primary key identity(1,1),
? sp_ID int,
? sp_num int
)
insert into dingdanxiangqing values(1,3);
insert into dingdanxiangqing values(2,4);
insert into dingdanxiangqing values(1,5);
insert into dingdanxiangqing values(3,5);
insert into dingdanxiangqing values(4,2);
insert into dingdanxiangqing values(5,1);
insert into dingdanxiangqing values(3,3);
insert into dingdanxiangqing values(4,3);
select * from yonghu;
select * from shangpin;
select * from sp_Type;

結(jié)構(gòu):

(1到4)
1

package denglu;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPasswordField;
public class chongzhimimajiemian extends JFrame {
static JButton JButton_QuRen = null;
static JLabel JLabel1, JLabel2 = null;
static JPasswordField JPasswordField_qslmm1, JPasswordField_qslmm2 = null;
public chongzhimimajiemian() {
this.setLayout(null);
this.setTitle("重置密碼頁(yè)面");
this.setSize(500, 500);
this.setLocationRelativeTo(null);
this.setVisible(true);
JLabel1 = new JLabel("請(qǐng)輸入密碼");
JLabel2 = new JLabel("請(qǐng)?jiān)俅屋斎朊艽a");
JLabel1.setBounds(100, 100, 100, 30);
this.add(JLabel1);
JLabel2 = new JLabel("請(qǐng)?jiān)俅屋斎朊艽a");
JLabel2.setBounds(100, 140, 100, 30);
this.add(JLabel2);
JButton_QuRen = new JButton("確認(rèn)修改");
JButton_QuRen.setBounds(100, 290, 150, 30);
this.add(JButton_QuRen);
JButton_QuRen.addActionListener(new shijian_queRenXiuGai(this));
JPasswordField_qslmm1 = new JPasswordField();
JPasswordField_qslmm2 = new JPasswordField();
JPasswordField_qslmm1.setBounds(280, 100, 100, 30);
JPasswordField_qslmm2.setBounds(280, 140, 100, 30);
this.add(JPasswordField_qslmm1);
this.add(JPasswordField_qslmm2);
}
}
class shijian_queRenXiuGai implements ActionListener {
static chongzhimimajiemian chuangKou = null;
public shijian_queRenXiuGai(chongzhimimajiemian chuanzhi) {
this.chuangKou = chuanzhi;
/*個(gè)人理解:先聲明空的窗口(chuangKou),之后屬性都儲(chǔ)存到窗口中,之后傳值(chuanzhi)賦值給窗口的所有屬性,
* 因?yàn)椤癱hongzhimimajiemian chuanzhi”的聲明,chuanzhi有chongzhimimajiemian中的所有內(nèi)容*/
}
@Override
public void actionPerformed(ActionEvent arg0) {
if (arg0.getActionCommand().equals("確認(rèn)修改")) {
JOptionPane.showMessageDialog(null, "點(diǎn)擊了修改密碼按鈕");
// 點(diǎn)擊了“確認(rèn)修改”之后要用if比較一下兩個(gè)密碼輸入的是否一致
String pwd1 = chuangKou.JPasswordField_qslmm1.getText();
String pwd2 = chuangKou.JPasswordField_qslmm2.getText();
if (pwd1.equals(pwd2)) {
// 如果相等就做進(jìn)一步的修改密碼
// SQL語(yǔ)句如何加條件:就是where后面加上zhmm(找回密碼)頁(yè)面的uname屬性,可以直接調(diào)用
// 聲明一個(gè)工具類(lèi),用來(lái)保存用戶要修改的用戶名,什么時(shí)候想要使用直接調(diào)用就好
String uname = gongjvClass.uname;// 獲取到用戶要修改的用戶名
String sql = "update yonghu set yh_pwd='" + pwd1
+ "' where yh_uname= '" + uname + "'";
if (DBUtils.ZSG(sql)) {
JOptionPane.showMessageDialog(null, "密碼重置成功");
} else {
JOptionPane.showMessageDialog(null, "出現(xiàn)了未知的錯(cuò)誤,請(qǐng)重試!");
}
} else {
//不相等就提示用戶兩次密碼輸入不一致
JOptionPane.showMessageDialog(null, "兩次密碼輸入不一致");
return;
}
}
}
}

2

package denglu;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
/*DBUtis封裝了對(duì)JDBC的操作,簡(jiǎn)化了JDBC操作??梢陨賹?xiě)代碼。
?1對(duì)于數(shù)據(jù)表的讀操作,他可以把結(jié)果轉(zhuǎn)換成List,Aray, Set等java集合, 便于程序員操作:
?2.對(duì)于數(shù)據(jù)表的寫(xiě)操作,也變得很簡(jiǎn)單(只需寫(xiě)sql語(yǔ)句)
?3.可以使用數(shù)據(jù)源,使用JNDI, 數(shù)據(jù)庫(kù)連接池等技術(shù)來(lái)優(yōu)化性能-重用已經(jīng)構(gòu)建好的數(shù)據(jù)庫(kù)連
?注意,在使用DBUtis過(guò)程中,需要在C3P0Utils中添加getDateSource方法。*/
public class DBUtils {
private static Connection con = null;
private static ResultSet res = null;
private static Statement sta = null;
static {
try {
Class.forName("com.microsoft.sqlserver." + "jdbc.SQLServerDriver");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
public static Connection getCon() {
if (con == null) {
try {
con = DriverManager.getConnection(
"jdbc:sqlserver://DESKTOP-49FTFSP;"
+ "databaseName=yonghu", "sa", "1234abcd");
} catch (SQLException e) {
e.printStackTrace();
}
}
return con;
}
public static ResultSet Select(String sql) {
/* Select為用于查找的靜態(tài)方法 */
con = getCon();
try {
sta = con.createStatement();
res = sta.executeQuery(sql);
} catch (SQLException e) {
e.printStackTrace();
}
return res;
}
public static boolean ZSG(String sql) {
/* ZXG為用于增加(Z)或修改(G)或刪除(S)的靜態(tài)方法,命名不可太長(zhǎng)不然這里會(huì)不管用 */
boolean b = false;
con = getCon();
try {
sta = con.createStatement();
int num = sta.executeUpdate(sql);
if (num > 0) {
b = true;
}
} catch (SQLException e) {
e.printStackTrace();
}
return b;
}
}

3

package denglu;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.ResultSet;
import java.sql.SQLException;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
public class denglu extends JFrame {
public static JButton JButton_dl, JButton_qx, JButton_zc,
JButton_zhmm = null;
public static JLabel JLabel_uname, JLabel_pwd = null;
public static JPasswordField JPasswordField_pwd1 = null;
public static JTextField JTextField_unametxt1 = null;
public denglu() {
this.setLayout(null);
this.setSize(640, 400);
this.setLocationRelativeTo(null);
JLabel_uname = new JLabel("用戶名");
JLabel_pwd = new JLabel("密碼");
JLabel_uname.setBounds(100, 100, 70, 30);
JLabel_pwd.setBounds(100, 140, 70, 30);
this.add(JLabel_uname);
this.add(JLabel_pwd);
JTextField_unametxt1 = new JTextField();
JPasswordField_pwd1 = new JPasswordField();
JTextField_unametxt1.setBounds(180, 100, 140, 30);
JPasswordField_pwd1.setBounds(180, 140, 140, 30);
this.add(JTextField_unametxt1);
this.add(JPasswordField_pwd1);
JButton_dl = new JButton("登錄");
JButton_dl.setBounds(140, 180, 70, 35);
JButton_dl.addActionListener(new shijian_denglu(this));
JButton_qx = new JButton("取消");
JButton_qx.setBounds(230, 180, 70, 35);
// JButton_qx.setBounds(arg0(下標(biāo)為0的值), arg1, arg2, arg3);
JButton_qx.addActionListener(new shijian_denglu(this));
JButton_zc = new JButton("注冊(cè)");
JButton_zc.setBounds(330, 180, 70, 35);
JButton_zc.addActionListener(new shijian_denglu(this));
JButton_zhmm = new JButton("找回密碼");
JButton_zhmm.setBounds(430, 180, 90, 35);
JButton_zhmm.addActionListener(new shijian_denglu(this));
this.add(JButton_zc);
this.add(JButton_dl);
this.add(JButton_qx);
this.add(JButton_zhmm);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
}
}
/*
?* 點(diǎn)擊按鈕是觸發(fā)事件,所以加點(diǎn)擊事件(ActionEvent) 在事件中使用窗體,將窗體傳過(guò)來(lái)
?*/
class shijian_denglu implements ActionListener {
public denglu shijian_dengLu1 = null;
public shijian_denglu(denglu shijian_dengLu2) {
shijian_dengLu1 = shijian_dengLu2;
}
@Override
public void actionPerformed(ActionEvent arg0) {
// JOptionPane.showMessageDialog(null, "點(diǎn)擊");
if (arg0.getActionCommand().equals("登錄")) {
String uname = shijian_dengLu1.JTextField_unametxt1.getText()
.trim();// 從shijian_dengLu1中的JTextField_unametxt1獲取用戶輸入的用戶名文本內(nèi)容
String pwd = shijian_dengLu1.JPasswordField_pwd1.getText().trim();// 獲取密碼
String sql = "select * from yonghu where yh_uname='" + uname
+ "'and yh_pwd='" + pwd + "'";
ResultSet res = DBUtils.Select(sql);
try {// res.next()表明尋找res中的內(nèi)容,如果res中有數(shù)據(jù),登錄成功,否則登錄失敗
if (res.next()) {
JOptionPane.showMessageDialog(null, "登錄成功");
new dengLuHouJieMian();
shijian_dengLu1.setVisible(false);
} else {
JOptionPane.showMessageDialog(null, "用戶名或密碼錯(cuò)誤");
}
} catch (SQLException e) {
e.printStackTrace();
}
// 點(diǎn)擊按鈕后會(huì)做登錄,首先要獲取窗體上的用戶名和密碼,
// 去數(shù)據(jù)庫(kù)里查一下用戶輸入的用戶名和密碼數(shù)據(jù)庫(kù)中存在情況,
// 如果數(shù)據(jù)庫(kù)中存在和用戶輸入的用戶名和密碼一致
// 說(shuō)明數(shù)據(jù)庫(kù)有這條數(shù)據(jù)庫(kù),用戶就可以登錄
} else if (arg0.getActionCommand().equals("注冊(cè)")) {
JOptionPane.showMessageDialog(null, "點(diǎn)擊了注冊(cè)按鈕");
new zhuCe();
// dd. setVisible(false);(這里dd. setVisible(false);表明視圖隱藏)
} else if (arg0.getActionCommand().equals("找回密碼")) {
JOptionPane.showMessageDialog(null, "點(diǎn)擊了找回密碼按鈕");
new zhaoHuiMiMa();
// dd. setVisible(false);
} else if (arg0.getActionCommand().equals("取消")) {
JOptionPane.showMessageDialog(null, "點(diǎn)擊了取消按鈕");
// dd. setVisible(false);
}
}
}

4

package denglu;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Vector;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.table.DefaultTableModel;
public class dengLuHouJieMian extends JFrame {
class shijian_table implements MouseListener, ActionListener {
public dengLuHouJieMian HanDengLuHouJieMianNeiRong = null;
public shijian_table(dengLuHouJieMian dengLuHouJieMianNeiRong) {
this.HanDengLuHouJieMianNeiRong = dengLuHouJieMianNeiRong;
}
// HanDengLuHouJieMianNeiRong(含登錄后界面內(nèi)容)
// DengLuHouJieMianNeiRong(登錄后界面內(nèi)容)
// 把窗體等內(nèi)容都賦值傳給HanDengLuHouJieMianNeiRong是為了方便下面如
// HanDengLuHouJieMianNeiRong.txt_name.getText();的“點(diǎn)(.)式引用,由“.”
// 后面的精確后綴條件提示等引用精確內(nèi)容”
@Override
public void actionPerformed(ActionEvent arg0) {
String name = HanDengLuHouJieMianNeiRong.wenBenKuang_name.getText();
String price = HanDengLuHouJieMianNeiRong.wenBenKuang_price.getText();
String type = HanDengLuHouJieMianNeiRong.wenBenKuang_typeid.getText();
// int type = HanDengLuHouJieMianNeiRong.com1.getSelectedIndex();
String jieshao = HanDengLuHouJieMianNeiRong.wenBenKuang_spjieshao.getText();
String sql = "insert into shangpin values('" + name + "'" + ", "
+ price + "," + type + ",'" + jieshao + "')";
if (DBUtils.ZSG(sql)) {
JOptionPane.showMessageDialog(null, "增加成功");
HanDengLuHouJieMianNeiRong.chaxunchushihua();
} else {
JOptionPane.showMessageDialog(null, "出現(xiàn)了未知的錯(cuò)誤,增加失敗");
}
}
@Override
public void mouseClicked(MouseEvent arg0) {
if (arg0.getClickCount() == 2) {
int row = HanDengLuHouJieMianNeiRong.biaoGe1.getSelectedRow();
HanDengLuHouJieMianNeiRong.wenBenKuang_Bianhao
.setText(HanDengLuHouJieMianNeiRong.biaoGe1.getValueAt(
row, 0).toString());
HanDengLuHouJieMianNeiRong.wenBenKuang_name
.setText(HanDengLuHouJieMianNeiRong.biaoGe1.getValueAt(
row, 1).toString());
HanDengLuHouJieMianNeiRong.wenBenKuang_price
.setText(HanDengLuHouJieMianNeiRong.biaoGe1.getValueAt(
row, 2).toString());
HanDengLuHouJieMianNeiRong.wenBenKuang_typeid
.setText(HanDengLuHouJieMianNeiRong.biaoGe1.getValueAt(
row, 3).toString());
HanDengLuHouJieMianNeiRong.wenBenKuang_spjieshao
.setText(HanDengLuHouJieMianNeiRong.biaoGe1.getValueAt(
row, 4).toString());
}
if (arg0.isMetaDown()) {
int num = JOptionPane.showConfirmDialog(null, "是否確認(rèn)刪除這條信息?");
if (num == 0) {
int row = HanDengLuHouJieMianNeiRong.biaoGe1
.getSelectedRow();
String sql = "delete shangpin where sp_id="
+ HanDengLuHouJieMianNeiRong.biaoGe1.getValueAt(
row, 0) + "";
if (DBUtils.ZSG(sql)) {
JOptionPane.showMessageDialog(null, "冊(cè)除成功");
HanDengLuHouJieMianNeiRong.chaxunchushihua();
// 刪除成功再查詢一遍表格就可以實(shí)現(xiàn)表格的刷新,但是我們的查詢寫(xiě)在了構(gòu)造方法里,內(nèi)容很多,
// 這里想要使用又要在寫(xiě)一遍,很麻煩,那么現(xiàn)在我們寫(xiě)一個(gè)方法 將查詢內(nèi)容封裝進(jìn)去,再想查詢的
// 時(shí)候只要調(diào)用查詢的方法即可
} else {
JOptionPane.showMessageDialog(null, "出現(xiàn)了未知的錯(cuò)誤,請(qǐng)重試");
}
}
}
}
@Override
public void mouseEntered(MouseEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void mouseExited(MouseEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void mousePressed(MouseEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void mouseReleased(MouseEvent arg0) {
// TODO Auto-generated method stub
}
}
static JButton anNiu_zengJia = null;
// anNiu_zengJia按鈕_增加
public static JComboBox xiaLaKuang = null;
// xiaLaKuang(下拉框)
static DefaultTableModel biaoGeMoXing1 = null;
// biaoGeMoXing1(表格模型1)
static JScrollPane gunDongTiao? = null;
// gunDongTiao(滾動(dòng)條)
static JTable biaoGe1 = null;
// biaoGe1(表格1)
static JLabel wenZi_Bianhao, wenZi_name, wenZi_price, wenZi_typeid,
wenZi_tjtypeid, wenZi_spjieshao;
// 文字(wenZi)
static JTextField wenBenKuang_Bianhao, wenBenKuang_name, wenBenKuang_price,
wenBenKuang_typeid,
wenBenKuang_spjieshao;
// wenBenKuang(文本框)
static Vector<Object> Vector_jiHe_biaoti = null;
// 集合(jiHe),標(biāo)題(biaoti)
static Vector<Vector<Object>> Vector_jiHe_biaoGeNeiRong = null;
// biaoGeNeiRong(表格內(nèi)容)
JPanel mianBan1, mianBan2 = null;
// mianBan(面板)
public dengLuHouJieMian() {
this.setTitle("登錄后的界面");
this.setSize(800, 600);
this.setLayout(null);
this.setLocationRelativeTo(null);
wenZi_Bianhao = new JLabel("編號(hào)");
wenZi_name = new JLabel("名稱(chēng)");
wenZi_price = new JLabel("價(jià)格");
wenZi_tjtypeid = new JLabel("類(lèi)型名稱(chēng)和ID");
wenZi_typeid = new JLabel("類(lèi)型ID");
wenZi_spjieshao = new JLabel("介紹");
anNiu_zengJia = new JButton("添加數(shù)據(jù)");
anNiu_zengJia.setBounds(530, 390, 100, 30);
anNiu_zengJia.addActionListener(new shijian_table(this));
this.add(anNiu_zengJia);
wenZi_Bianhao.setBounds(560, 100, 70, 30);
wenZi_name.setBounds(560, 140, 70, 30);
wenZi_price.setBounds(560, 180, 70, 30);
wenZi_typeid.setBounds(560, 220, 70, 30);
wenZi_spjieshao.setBounds(560, 260, 70, 30);
wenZi_tjtypeid.setBounds(560, 320, 170, 30);
this.add(wenZi_Bianhao);
this.add(wenZi_name);
this.add(wenZi_price);
this.add(wenZi_typeid);
this.add(wenZi_spjieshao);
this.add(wenZi_tjtypeid);
wenBenKuang_Bianhao = new JTextField();// 畫(huà)框框的界面
wenBenKuang_Bianhao.setEditable(false);
wenBenKuang_name = new JTextField();
wenBenKuang_price = new JTextField();
wenBenKuang_typeid = new JTextField();
wenBenKuang_spjieshao = new JTextField();
wenBenKuang_Bianhao.setBounds(640, 100, 130, 30);
wenBenKuang_name.setBounds(640, 140, 130, 30);
wenBenKuang_price.setBounds(640, 180, 130, 30);
wenBenKuang_typeid.setBounds(640, 220, 130, 30);
wenBenKuang_spjieshao.setBounds(640, 260, 130, 30);
this.add(wenBenKuang_Bianhao);
this.add(wenBenKuang_name);
this.add(wenBenKuang_price);
this.add(wenBenKuang_typeid);
this.add(wenBenKuang_spjieshao);
Vector_jiHe_biaoti = new Vector<Object>();
Vector_jiHe_biaoti.add("編號(hào)");
Vector_jiHe_biaoti.add("名稱(chēng)");
Vector_jiHe_biaoti.add("價(jià)格");
Vector_jiHe_biaoti.add("類(lèi)型");
Vector_jiHe_biaoti.add("介紹");
String sql = "select * from shangpin";
ResultSet res = DBUtils.Select(sql);
try {
Vector_jiHe_biaoGeNeiRong = new Vector<Vector<Object>>();
while (res.next()) {
Vector<Object> v = new Vector<Object>();
v.add(res.getInt("sp_ID"));
v.add(res.getString("sp_Name"));
v.add(res.getDouble("sp_price"));
v.add(res.getInt("sp_TypeID"));
v.add(res.getString("sp_Jieshao"));
Vector_jiHe_biaoGeNeiRong.add(v);
biaoGeMoXing1 = new DefaultTableModel(Vector_jiHe_biaoGeNeiRong,
Vector_jiHe_biaoti);
}
biaoGeMoXing1 = new DefaultTableModel(Vector_jiHe_biaoGeNeiRong,
Vector_jiHe_biaoti) {
@Override
public boolean isCellEditable(int a, int b) {
return false;
}
};
biaoGe1 = new JTable(biaoGeMoXing1);
biaoGe1.addMouseListener(new shijian_table(this));
biaoGe1.setBounds(0, 0, 500, 500);
gunDongTiao? = new JScrollPane(biaoGe1);
gunDongTiao .setBounds(0, 0, 550, 150);
mianBan1 = new JPanel();/*
* JPanel1 = new JPanel(); 不可在gundong = new
* JScrollPane(JTable1);上面
*/
mianBan1.add(gunDongTiao );
// 加this.add(gundong);刪mianBan1.add(gunDongTiao );會(huì)有直接的滾動(dòng)條,但變只可刪一次
mianBan1.setBounds(0, 0, 550, 250);
this.add(mianBan1);
} catch (SQLException e) {
e.printStackTrace();
}
shangPinLeiXingXiaLaKuang();
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
// 這里引用chaxunchushihua();沒(méi)作用
}
// public void XXX() 格式就是封裝查詢的方法,可調(diào)用,無(wú)加靜態(tài)屬性的單詞
public void chaxunchushihua() {
// 判斷JPanel是不是為空
if (this.mianBan1 != null) {
// 面板1不為空說(shuō)明面板1中有內(nèi)容,就把面板1內(nèi)容用從this中用remove移除,下面再加新內(nèi)容
this.remove(mianBan1);
}
Vector_jiHe_biaoti = new Vector<Object>();
Vector_jiHe_biaoti.add("編號(hào)");
Vector_jiHe_biaoti.add("名稱(chēng)");
Vector_jiHe_biaoti.add("價(jià)格");
Vector_jiHe_biaoti.add("類(lèi)型");
Vector_jiHe_biaoti.add("介紹");
String sql = "select * from shangpin";
ResultSet res = DBUtils.Select(sql);
try {
Vector_jiHe_biaoGeNeiRong = new Vector<Vector<Object>>();
while (res.next()) {
Vector<Object> v = new Vector<Object>();
v.add(res.getInt("sp_ID"));
v.add(res.getString("sp_Name"));
v.add(res.getDouble("sp_price"));
v.add(res.getInt("sp_TypeID"));
v.add(res.getString("sp_Jieshao"));
Vector_jiHe_biaoGeNeiRong.add(v);
biaoGeMoXing1 = new DefaultTableModel(Vector_jiHe_biaoGeNeiRong,
Vector_jiHe_biaoti);
}
biaoGeMoXing1 = new DefaultTableModel(Vector_jiHe_biaoGeNeiRong,
Vector_jiHe_biaoti) {
@Override
public boolean isCellEditable(int a, int b) {
return false;
}
};
biaoGe1 = new JTable(biaoGeMoXing1);
// XXX.addMouseListener(new XXX(this));為給表格添加鼠標(biāo)點(diǎn)擊事件的格式,多總結(jié)和記錄格式等法
biaoGe1.addMouseListener(new shijian_table(this));
gunDongTiao? = new JScrollPane(biaoGe1);
gunDongTiao .setBounds(0, 0, 550, 150);
mianBan1 = new JPanel();
mianBan1.add(gunDongTiao );
mianBan1.setBounds(0, 0, 550, 250);
this.add(mianBan1);
} catch (SQLException e) {
e.printStackTrace();
}
// 釋放資源:this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
}
// shangPinLeiXingXiaLaKuang(商品類(lèi)型下拉框)
public void shangPinLeiXingXiaLaKuang() {
String sql = "select * from sp_type";
ResultSet res = DBUtils.Select(sql);
xiaLaKuang = new JComboBox();
try {
while (res.next()) {
// com1. setSelectedIndex(Integer.parseInt(res. getObject(1).
// toString()));
xiaLaKuang.addItem(res.getObject(2).toString());
xiaLaKuang.addItem(res.getObject(1).toString());
}
} catch (SQLException e) {
e.printStackTrace();
}
xiaLaKuang.setBounds(640, 320, 130, 30);
this.add(xiaLaKuang);
}
}
