Java连接数据库增删改查.doc
《Java连接数据库增删改查.doc》由会员分享,可在线阅读,更多相关《Java连接数据库增删改查.doc(21页珍藏版)》请在三一办公上搜索。
1、Java连接sql2000数据库增删改查功能学院:仲恺农业工程学院 姓名:陈伟团 专业:计算机科学与技术 班级:084班1. 数据库连接package edu;import java.sql.Connection;import java.sql.DriverManager;import java.sql.SQLException;public class DBConnection public static Connection getConnection(Connection conn = null;String driver = com.microsoft.jdbc.sqlserver.S
2、QLServerDriver;String url = jdbc:microsoft:sqlserver:/localhost:1433;DatabaseName=shop;String username = sa;String password = sa;try Class.forName(driver;conn = DriverManager.getConnection(url,username,password; catch (ClassNotFoundException e e.printStackTrace(; catch (SQLException e e.printStackTr
3、ace(;return conn;2. 生成get set方法package edu;public class Country private String name;private String region;private String area;private String population;private String gdp;public String getName( return name;public void setName(String name this.name = name;public String getRegion( return region;public
4、 void setRegion(String region this.region = region;public String getArea( return area;public void setArea(String area this.area = area;public String getPopulation( return population;public void setPopulation(String population this.population = population;public String getGdp( return gdp;public void
5、setGdp(String gdp this.gdp = gdp;3实现方法package edu;import java.awt.Rectangle;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.util.List;import javax.swing.JButton;import javax.swing.JComboBox;import javax.swing.JFrame;import javax.swing.JLabel;import javax.swing.JOpt
6、ionPane;import javax.swing.JPanel;import javax.swing.JScrollPane;import javax.swing.JTable;import javax.swing.JTextField;import javax.swing.SwingUtilities;public class CountryInfoGui implements ActionListener private JFrame jFrame;private JPanel jContentPane;private JLabel lblName = null;private JTe
7、xtField tfName = null;private JLabel lblRegion = null;private JComboBox cbRegion = null;private JLabel lblArea = null;private JTextField tfArea = null;private JLabel lblPopu = null;private JTextField tfPopu = null;private JLabel lblGdp = null;private JTextField tfGDP = null;private JButton btnAdd =
8、null;private JButton btnDel = null;private JButton btnModify = null;private JButton btnQuery = null;private JButton btnUpPage = null;private JButton btnDownPage = null;private JTable table = null;TableModel tm = null;JScrollPane jsp = null;private JTextField getTfName( if (tfName = null tfName = new
9、 JTextField(;tfName.setBounds(new Rectangle(89, 17, 173, 27;return tfName;private JComboBox getCbRegion( if (cbRegion = null String str = 请选择, 亚洲, 欧洲, 非洲, 北美洲, 南美洲, 大洋洲,南极洲 ;cbRegion = new JComboBox(str;cbRegion.setBounds(new Rectangle(360, 17, 229, 28;return cbRegion;private JTextField getTfArea( i
10、f (tfArea = null tfArea = new JTextField(;tfArea.setBounds(new Rectangle(89, 61, 173, 27;return tfArea;private JTextField getTfPopu( if (tfPopu = null tfPopu = new JTextField(;tfPopu.setBounds(new Rectangle(360, 61, 229, 28;return tfPopu;private JTextField getTfGDP( if (tfGDP = null tfGDP = new JTex
11、tField(;tfGDP.setBounds(new Rectangle(89, 105, 173, 27;return tfGDP;private JButton getBtnAdd( if (btnAdd = null btnAdd = new JButton(;btnAdd.setBounds(new Rectangle(46, 152, 71, 30;btnAdd.addActionListener(this;btnAdd.setText(添加;return btnAdd;private JButton getBtnUpPage( if (btnUpPage = null btnUp
12、Page = new JButton(上一页;btnUpPage.addActionListener(this;btnUpPage.setBounds(200, 390, 80, 30;return btnUpPage;private JButton getBtnDownPage( if (btnDownPage = null btnDownPage = new JButton(下一页;btnDownPage.addActionListener(this;btnDownPage.setBounds(300, 390, 80, 30;return btnDownPage;private JBut
13、ton getBtnDel( if (btnDel = null btnDel = new JButton(;btnDel.setBounds(new Rectangle(189, 152, 71, 30;btnDel.addActionListener(this;btnDel.setText(删除;return btnDel;private JButton getBtnModify( if (btnModify = null btnModify = new JButton(;btnModify.setBounds(new Rectangle(342, 152, 71, 30;btnModif
14、y.addActionListener(this;btnModify.setText(更新;return btnModify;private JButton getBtnQuery( if (btnQuery = null btnQuery = new JButton(;btnQuery.setBounds(new Rectangle(484, 152, 71, 30;btnQuery.addActionListener(this;btnQuery.setText(查找;return btnQuery;public static void main(String args SwingUtili
15、ties.invokeLater(new Runnable( public void run( CountryInfoGui application = new CountryInfoGui(;application.getJFrame(.setVisible(true;private JFrame getJFrame( if (jFrame = null jFrame = new JFrame(;jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE;jFrame.setBounds(new Rectangle(450, 250, 610,
16、454;jFrame.setContentPane(getJContentPane(;jFrame.setTitle(国家信息管理;return jFrame;private JPanel getJContentPane( if (jContentPane = null lblGdp = new JLabel(;lblGdp.setBounds(new Rectangle(15, 106, 63, 27;lblGdp.setText(国家GDP;lblPopu = new JLabel(;lblPopu.setBounds(new Rectangle(285, 61, 63, 27;lblPo
17、pu.setText(人口数量;lblArea = new JLabel(;lblArea.setBounds(new Rectangle(15, 61, 63, 27;lblArea.setText(国家面积;lblRegion = new JLabel(;lblRegion.setBounds(new Rectangle(285, 17, 63, 27;lblRegion.setText(所属地区;lblName = new JLabel(;lblName.setBounds(new Rectangle(15, 17, 63, 27;lblName.setText(国家名称;jConten
18、tPane = new JPanel(;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(getTf
19、Popu(, 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;return j
20、ContentPane;public void actionPerformed(ActionEvent e if (e.getActionCommand(.equals(查找 selectAll(; else if (e.getActionCommand(.equals(下一页 selectDownPage(; else if (e.getActionCommand(.equals(上一页 selectUpPage(; else if (e.getActionCommand(.equals(添加 if (tfName.getText(.equals(| cbRegion.getSelected
21、Item(.equals(请选择| tfArea.getText(.equals(| tfPopu.getText(.equals(| tfGDP.getText(.equals( JOptionPane.showMessageDialog(jFrame, 请填写相关信息;else Country co = new Country(;co.setName(tfName.getText(;co.setRegion(String cbRegion.getSelectedItem(;co.setArea(tfArea.getText(;co.setPopulation(tfPopu.getText(
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- Java 连接 数据库 增删
![提示](https://www.31ppt.com/images/bang_tan.gif)
链接地址:https://www.31ppt.com/p-4111986.html