JComboBox中放入访问数据库所得内容

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Vector;
import javax.swing.ComboBoxModel;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;

import DBbean.DBcon;

public class ListPanel

 extends JPanel implements ActionListener{
 
 
 public static void main(String args[]){
  JFrame aframe = new JFrame();
  aframe.add(new ListPanel());
  aframe.setSize(1000,300);
  aframe.setVisible(true);
 }
 DBcon db;//下面会给出
 String section;
 JPanel p_north;
 JPanel p_center = new JPanel();
 JComboBox combo;
 JLabel l_section = new JLabel("按区域浏览:     ");
 
 JButton btn_hign = new JButton("高级查询");
 ComboBoxModel model;
 Vector<String> list;
 public ListPanel(){
  this.setBackground(Color.lightGray);
  
  FlowLayout flow = new FlowLayout(5);
  flow.setHgap(40);
  flow.setAlignment(FlowLayout.CENTER);
  p_north = new JPanel(flow);
  p_north.setOpaque(false);
  this.setLayout(new BorderLayout());
  this.add(p_north,BorderLayout.NORTH);
  this.add(p_center,BorderLayout.CENTER);
  
  db = new DBcon();
  list = new Vector<String>();
  this.fillComboBox();
  model = new DefaultComboBoxModel();
  combo = new JComboBox(list);
  p_north.add(l_section);
  p_north.add(combo);
  combo.addActionListener(this);
  p_north.add(btn_hign);
  
  
  
  
 }
 public ListPanel(String section){
  this.setBackground(Color.lightGray);
  
  FlowLayout flow = new FlowLayout(5);
  flow.setHgap(40);
  flow.setAlignment(FlowLayout.CENTER);
  p_north = new JPanel(flow);
  p_north.setOpaque(false);
  this.setLayout(new BorderLayout());
  this.add(p_north,BorderLayout.NORTH);
  this.add(p_center,BorderLayout.CENTER);
  
  db = new DBcon();
  list = new Vector<String>();
  this.fillComboBox();
  model = new DefaultComboBoxModel();
  combo = new JComboBox(list);
  p_north.add(l_section);
  p_north.add(combo);
  combo.addActionListener(this);
  p_north.add(btn_hign);
  combo.setSelectedItem(section);
  getBuildings(section);
  
  
 }
 
 public void fillComboBox(){
  
  String sql = "select * from section";
  ResultSet re;
  try {
   re = db.executeQuery(sql);
   
   while(re.next()){
    list.add(re.getString("name"));
   }
   
  } catch (SQLException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
 }
 public void actionPerformed(ActionEvent arg0) {
  section = combo.getSelectedItem().toString();
  getBuildings(section);
  
 }
 public void getBuildings(String section){
  JFrame aframe = new JFrame();
  JScrollPane jsp = new JScrollPane();
  Table.BuildingTable bt = new Table.BuildingTable(section);
  bt.table.setPreferredSize(new Dimension(800,600));
  jsp.setViewportView(bt.table);
  jsp.setPreferredSize(new Dimension(1200,600));
  this.p_center.removeAll();
  this.p_center.add(jsp,BorderLayout.CENTER);
  this.updateUI();
 }
}

 

 

 


import java.sql.*;
import java.util.logging.Level;
import java.util.logging.Logger;

public class DBcon {


  Connection conn = null;
  Statement stmt = null;
  public ResultSet rs = null;
  public DBcon() {

         try {
             Class.forName("com.mysql.jdbc.Driver");
         } catch (java.lang.ClassNotFoundException e) {
             System.err.println(e.getMessage());
         }
     }

  public ResultSet executeQuery(String sql) throws SQLException {
                if(conn==null)
         conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/housemanagement?useUnicode=true&characterEncoding=utf-8", "root", "root");

            stmt = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_READ_ONLY);
         rs = stmt.executeQuery(sql);

         return rs;
  }

  public int executeUpdate(final String SQL) throws Exception{
    
             int state = 0;
             if(conn==null)
             conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/housemanagement?useUnicode=true&characterEncoding=utf-8", "root", "rootf");
             stmt = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
             state = stmt.executeUpdate(SQL);
             return state;
       
     }

     public ResultSet executeUpdate(final String sql, int[] index) throws SQLException {
       if(conn==null)
         conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/housemanagement?useUnicode=true&characterEncoding=utf-8", "root", "root");
         stmt = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
         stmt.executeUpdate(sql, index);

         ResultSet rs = stmt.getGeneratedKeys();
         return rs;
     }
     public boolean isClosed(){
      if(conn==null)
       return true;
      else
       return false;
     }
  public void close() {
    
         try {
             if (stmt != null) {
                 stmt.close();
             }
         } catch (Exception e) {
             e.printStackTrace(System.err);
         }

         try {
             if (conn != null) {
                 conn.close();
             }
         } catch (Exception e) {
             e.printStackTrace(System.err);
         }
     }
}

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值