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); } } }
|