Java连接sql数据库增删改查.docx
- 文档编号:8636321
- 上传时间:2023-02-01
- 格式:DOCX
- 页数:22
- 大小:100.12KB
Java连接sql数据库增删改查.docx
《Java连接sql数据库增删改查.docx》由会员分享,可在线阅读,更多相关《Java连接sql数据库增删改查.docx(22页珍藏版)》请在冰豆网上搜索。
Java连接sql数据库增删改查
Java连接sql2000数据库增删改查功能
1.数据库连接
packageedu;
importjava.sql.Connection;
importjava.sql.DriverManager;
importjava.sql.SQLException;
publicclassDBConnection{
publicstaticConnectiongetConnection(){
Connectionconn=null;
Stringdriver="com.microsoft.jdbc.sqlserver.SQLServerDriver";
Stringurl="jdbc:
microsoft:
sqlserver:
//localhost:
1433;DatabaseName=shop";
Stringusername="sa";
Stringpassword="sa";
try{
Class.forName(driver);
conn=DriverManager.getConnection(url,username,password);
}catch(ClassNotFoundExceptione){
e.printStackTrace();
}catch(SQLExceptione){
e.printStackTrace();
}
returnconn;
}
2.生成getset方法
packageedu;
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实现方法
packageedu;
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();
}elseif(e.getActionCommand().equals("上一页")){
selectUpPage();
}elseif(e.getActionCommand().equals("添加")){
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((String)cbRegion.getSelectedItem());
co.setArea(tfArea.getText());
co.setPopulation(tfPopu.getText());
co.setGdp(tfGDP.getText());
booleanflag=CountryOperate.add(co);
if(flag){
JOptionPane.showMessageDialog(jFrame,"添加成功");
}else{
JOptionPane.showMessageDialog(jFrame,"添加失败");
}
}
}elseif(e.getActionCommand().equals("删除")){
if(!
tfName.getText().equals("")){
Stringkey=tfName.getText();
booleanflag=CountryOperate.del(key);
if(flag){
JOptionPane.showMessageDialog(jFrame,"删除成功");
}else{
JOptionPane.showMessageDialog(jFrame,"删除失败");
}
}else{
JOptionPane.showMessageDialog(jFrame,"请填写一个国家名字");
}
}elseif(e.getActionCommand().equals("更新")){
if(!
tfName.getText().equals("")
&&!
cbRegion.getSelectedItem().equals("请选择")
&&!
tfArea.getText().equals("")
&&!
tfPopu.getText().equals("")
&&!
tfGDP.getText().equals("")){
Countrycou=newCountry();
cou.setName(tfName.getText());
cou.setRegion((String)cbRegion.getSelectedItem());
cou.setPopulation(tfPopu.getText());
cou.setArea(tfArea.getText());
cou.setGdp(tfGDP.getText());
booleanflag=CountryOperate.modify(cou);
if(flag){
JOptionPane.showMessageDialog(jFrame,"更新成功");
}else{
JOptionPane.showMessageDialog(jFrame,"更新失败");
}
}else{
JOptionPane.showMessageDialog(jFrame,"请填写相应的信息");
}
}
}
publicvoidsetValues(Object[][]obj){
this.tm.setDatavalues(obj);
}
publicvoidselectAll(){
List
Object[][]datavalues=newObject[list.size()][5];
for(inti=0;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(){ List if(list! =null){ Object[][]datavalues=newObject[list.size()][5]; for(inti=0;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(); } this.setValues(datavalues); table.updateUI(); }else{ JOptionPane.showMessageDialog(jFrame,"已经是最后一页"); } } publicvoidselectUpPage(){ List if(list! =null){ Ob
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- Java 连接 sql 数据库 增删
![提示](https://static.bdocx.com/images/bang_tan.gif)