Java连接数据库增删改查精Word格式.docx
《Java连接数据库增删改查精Word格式.docx》由会员分享,可在线阅读,更多相关《Java连接数据库增删改查精Word格式.docx(21页珍藏版)》请在冰豆网上搜索。
e.printStackTrace(;
}catch(SQLExceptione{
}
returnconn;
2.生成getset方法
publicclassCountry{
privateStringname;
privateStringregion;
privateStringarea;
privateStringpopulation;
privateStringgdp;
publicStringgetName({
returnname;
publicvoidsetName(Stringname{
this.name=name;
publicStringgetRegion({
returnregion;
publicvoidsetRegion(Stringregion{
this.region=region;
publicStringgetArea({
returnarea;
publicvoidsetArea(Stringarea{
this.area=area;
publicStringgetPopulation({
returnpopulation;
publicvoidsetPopulation(Stringpopulation{
this.population=population;
publicStringgetGdp({
returngdp;
publicvoidsetGdp(Stringgdp{
this.gdp=gdp;
3实现方法
importjava.awt.Rectangle;
importjava.awt.event.ActionEvent;
importjava.awt.event.ActionListener;
importjava.util.List;
importjavax.swing.JButton;
importjavax.swing.JComboBox;
importjavax.swing.JFrame;
importjavax.swing.JLabel;
importjavax.swing.JOptionPane;
importjavax.swing.JPanel;
importjavax.swing.JScrollPane;
importjavax.swing.JTable;
importjavax.swing.JTextField;
importjavax.swing.SwingUtilities;
publicclassCountryInfoGuiimplementsActionListener{
privateJFramejFrame;
privateJPaneljContentPane;
privateJLabellblName=null;
privateJTextFieldtfName=null;
privateJLabellblRegion=null;
privateJComboBoxcbRegion=null;
privateJLabellblArea=null;
privateJTextFieldtfArea=null;
privateJLabellblPopu=null;
privateJTextFieldtfPopu=null;
privateJLabellblGdp=null;
privateJTextFieldtfGDP=null;
privateJButtonbtnAdd=null;
privateJButtonbtnDel=null;
privateJButtonbtnModify=null;
privateJButtonbtnQuery=null;
privateJButtonbtnUpPage=null;
privateJButtonbtnDownPage=null;
privateJTabletable=null;
TableModeltm=null;
JScrollPanejsp=null;
privateJTextFieldgetTfName({
if(tfName==null{
tfName=newJTextField(;
tfName.setBounds(newRectangle(89,17,173,27;
returntfName;
privateJComboBoxgetCbRegion({
if(cbRegion==null{
String[]str={"
请选择"
"
亚洲"
欧洲"
非洲"
北美洲"
南美洲"
大洋洲"
"
南极洲"
};
cbRegion=newJComboBox(str;
cbRegion.setBounds(newRectangle(360,17,229,28;
returncbRegion;
privateJTextFieldgetTfArea({
if(tfArea==null{
tfArea=newJTextField(;
tfArea.setBounds(newRectangle(89,61,173,27;
returntfArea;
privateJTextFieldgetTfPopu({
if(tfPopu==null{
tfPopu=newJTextField(;
tfPopu.setBounds(newRectangle(360,61,229,28;
returntfPopu;
privateJTextFieldgetTfGDP({
if(tfGDP==null{
tfGDP=newJTextField(;
tfGDP.setBounds(newRectangle(89,105,173,27;
returntfGDP;
privateJButtongetBtnAdd({
if(btnAdd==null{
btnAdd=newJButton(;
btnAdd.setBounds(newRectangle(46,152,71,30;
btnAdd.addActionListener(this;
btnAdd.setText("
添加"
returnbtnAdd;
privateJButtongetBtnUpPage({
if(btnUpPage==null{
btnUpPage=newJButton("
上一页"
btnUpPage.addActionListener(this;
btnUpPage.setBounds(200,390,80,30;
returnbtnUpPage;
privateJButtongetBtnDownPage({
if(btnDownPage==null{
btnDownPage=newJButton("
下一页"
btnDownPage.addActionListener(this;
btnDownPage.setBounds(300,390,80,30;
returnbtnDownPage;
privateJButtongetBtnDel({
if(btnDel==null{
btnDel=newJButton(;
btnDel.setBounds(newRectangle(189,152,71,30;
btnDel.addActionListener(this;
btnDel.setText("
删除"
returnbtnDel;
privateJButtongetBtnModify({
if(btnModify==null{
btnModify=newJButton(;
btnModify.setBounds(newRectangle(342,152,71,30;
btnModify.addActionListener(this;
btnModify.setText("
更新"
returnbtnModify;
privateJButtongetBtnQuery({
if(btnQuery==null{
btnQuery=newJButton(;
btnQuery.setBounds(newRectangle(484,152,71,30;
btnQuery.addActionListener(this;
btnQuery.setText("
查找"
returnbtnQuery;
publicstaticvoidmain(String[]args{
SwingUtilities.invokeLater(newRunnable({
publicvoidrun({
CountryInfoGuiapplication=newCountryInfoGui(;
application.getJFrame(.setVisible(true;
};
privateJFramegetJFrame({
if(jFrame==null{
jFrame=newJFrame(;
jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE;
jFrame.setBounds(newRectangle(450,250,610,454;
jFrame.setContentPane(getJContentPane(;
jFrame.setTitle("
国家信息管理"
returnjFrame;
privateJPanelgetJContentPane({
if(jContentPane==null{
lblGdp=newJLabel(;
lblGdp.setBounds(newRectangle(15,106,63,27;
lblGdp.setText("
国家GDP"
lblPopu=newJLabel(;
lblPopu.setBounds(newRectangle(285,61,63,27;
lblPopu.setText("
人口数量"
lblArea=newJLabel(;
lblArea.setBounds(newRectangle(15,61,63,27;
lblArea.setText("
国家面积"
lblRegion=newJLabel(;
lblRegion.setBounds(newRectangle(285,17,63,27;
lblRegion.setText("
所属地区"
lblName=newJLabel(;
lblName.setBounds(newRectangle(15,17,63,27;
lblName.setText("
国家名称"
jContentPane=newJPanel(;
jContentPane.setLayout(null;
jContentPane.add(lblName,null;
jContentPane.add(getTfName(,null;
jContentPane.add(lblRegion,null;
jContentPane.add(getCbRegion(,null;
jContentPane.add(lblArea,null;
jContentPane.add(getTfArea(,null;
jContentPane.add(lblPopu,null;
jContentPane.add(getTfPopu(,null;
jContentPane.add(lblGdp,null;
jContentPane.add(getTfGDP(,null;
jContentPane.add(getBtnAdd(,null;
jContentPane.add(getBtnDel(,null;
jContentPane.add(getBtnModify(,null;
jContentPane.add(getBtnQuery(,null;
jContentPane.add(getBtnUpPage(,null;
jContentPane.add(getBtnDownPage(,null;
returnjContentPane;
publicvoidactionPerformed(ActionEvente{
if(e.getActionCommand(.equals("
{
selectAll(;
}elseif(e.getActionCommand(.equals("
selectDownPage(;
selectUpPage(;
if(tfName.getText(.equals("
||cbRegion.getSelectedItem(.equals("
||tfArea.getText(.equals("
||tfPopu.getText(.equals("
||tfGDP.getText(.equals("
JOptionPane.showMessageDialog(jFrame,"
请填写相关信息"
}else{
Countryco=newCountry(;
co.setName(tfName.getText(;
co.setRegion((StringcbRegion.getSelectedItem(;
co.setArea(tfArea.getText(;
co.setPopulation(tfPopu.getText(;
co.setGdp(tfGDP.getText(;
booleanflag=CountryOperate.add(co;
if(flag{
添加成功"
}else{
添加失败"
if(!
tfName.getText(.equals("
{
Stringkey=tfName.getText(;
booleanflag=CountryOperate.del(key;
删除成功"
删除失败"
请填写一个国家名字"
if(!
&
!
cbRegion.getSelectedItem(.equals("
tfArea.getText(.equals("
tfPopu.getText(.equals("
tfGDP.getText(.equals("
Countrycou=newCountry(;
cou.setName(tfName.getText(;
cou.setRegion((StringcbRegion.getSelectedItem(;
cou.setPopulation(tfPopu.getText(;
cou.setArea(tfArea.getText(;
cou.setGdp(tfGDP.getText(;
booleanflag=CountryOperate.modify(cou;
if(flag{
更新成功"
更新失败"
请填写相应的信息"
publicvoidsetValues(Object[][]obj{
this.tm.setDatavalues(obj;
publicvoidselectAll({
Listlist=CountryOperate.queryFirst(;
Object[][]datavalues=newObject[list.size(][5];
for(inti=0;
i<
list.size(;
i++{
Countryco=list.get(i;
datavalues[i][0]=co.getName(;
datavalues[i][1]=co.getRegion(;
datavalues[i][2]=co.getArea(;
datavalues[i][3]=co.getRegion(;
datavalues[i][4]=co.getGdp(;
tm=newTableModel(;
this.setValues(datavalues;
table=newJTable(tm;
table.updateUI(;
JScrollPanejsp=newJScrollPane(table;
jsp.setBounds(0,200,600,182;
jContentPane.add(jsp;
publicvoidselectDownPage({
Listlist=CountryOperate.downPage(;
if(list!
=null{
已经是最后一页"
publicvoidselectUpPage({
Listlist=CountryOperate.upPage(;
已经是第一页"
4操作方法
importjavax.swing.table.AbstractTableModel;
publicclassTableModelextendsAbstractTableModel{
publicfinalstaticString[]columnHeaders={
"
国家面