| 发表于:2007-10-10 11:17:391楼 得分:0 |
2.viewdb.java文件 /** @version 1.30 2004-08-05 @author cay horstmann */ import java.net.*; import java.sql.*; import java.awt.*; import java.awt.event.*; import java.io.*; import java.util.*; import javax.swing.*; /** this program uses metadata to display arbitrary tables in a database. */ public class viewdb { public static void main(string[] args) { jframe frame = new viewdbframe(); frame.setdefaultcloseoperation(jframe.exit_on_close); frame.setvisible(true); } } /** the frame that holds the data panel and the navigation buttons. */ class viewdbframe extends jframe { public viewdbframe() { settitle("viewdb"); setsize(default_width, default_height); tablenames = new jcombobox(); tablenames.addactionlistener(new actionlistener() { public void actionperformed(actionevent event) { showtable((string) tablenames.getselecteditem()); } }); add(tablenames, borderlayout.north); try { conn = getconnection(); meta = conn.getmetadata(); createstatement(); gettablenames(); } catch (sqlexception e) { joptionpane.showmessagedialog(this, e); } catch (ioexception e) { joptionpane.showmessagedialog(this, e); } jpanel buttonpanel = new jpanel(); add(buttonpanel, borderlayout.south); if (scrolling) { previousbutton = new jbutton("previous"); previousbutton.addactionlistener(new actionlistener() { public void actionperformed(actionevent event) { showpreviousrow(); } }); buttonpanel.add(previousbutton); } nextbutton = new jbutton("next"); nextbutton.addactionlistener(new actionlistener() { public void actionperformed(actionevent event) { shownextrow(); } }); buttonpanel.add(nextbutton); addwindowlistener(new windowadapter() { public void windowclosing(windowevent event) { try { if (conn != null) conn.close(); } catch (sqlexception e) { while (e != null) { e.printstacktrace(); e = e.getnextexception(); } } } }); } /** creates the statement object used for EXECuting queries. if the database supports scrolling cursors, the statement is created to yield them. */ public void createstatement() throws sqlexception { if (meta.supportsresultsettype( resultset.type_scroll_insensitive)) { stat = conn.createstatement( resultset.type_scroll_insensitive, resultset.concur_read_only); scrolling = true; } else { stat = conn.createstatement(); scrolling = false; } } /** gets all table names of this database and adds them to the combo box. */ public void gettablenames() throws sqlexception { resultset mrs = meta.gettables(null, null, null, new string[] { "table" }); while (mrs.next()) tablenames.additem(mrs.getstring(3)); mrs.close(); } /** prepares the text fields for showing a new table, and shows the first row. @param tablename the name of the table to display */ public void showtable(string tablename) { try { if (rs != null) rs.close(); rs = stat.EXECutequery("select * from " + tablename); if (scrollpane != null) remove(scrollpane); datapanel = new datapanel(rs); scrollpane = new jscrollpane(datapanel); add(scrollpane, borderlayout.center); validate(); shownextrow(); } catch (sqlexception e) { joptionpane.showmessagedialog(this, e); } } /** moves to the previous table row. */ public void showpreviousrow() { try { if (rs == null ¦ ¦ rs.isfirst()) return; rs.previous(); datapanel.showrow(rs); } catch (sqlexception e) { joptionpane.showmessagedialog(this, e); } } /** moves to the next table row. */ public void shownextrow() { try { if (rs == null ¦ ¦ scrolling && rs.islast()) return; if (!rs.next() && !scrolling) { rs.close(); rs = null; return; } datapanel.showrow(rs); } catch (sqlexception e) { joptionpane.showmessagedialog(this, e); } } /** gets a connection from the properties specified in the file database.properties @return the database connection */ public static connection getconnection() throws sqlexception, ioexception { properties props = new properties(); fileinputstream in = new fileinputstream("database.properties"); props.load(in); in.close(); string drivers = props.getproperty("jdbc.drivers"); if (drivers != null) system.setproperty("jdbc.drivers", drivers); string url = props.getproperty("jdbc.url"); string username = props.getproperty("jdbc.username"); string password = props.getproperty("jdbc.password"); return drivermanager.getconnection(url, username, password); } public static final int default_width = 300; public static final int default_height = 200; private jbutton previousbutton; private jbutton nextbutton; private datapanel datapanel; private component scrollpane; private jcombobox tablenames; private connection conn; private statement stat; private databasemetadata meta; private resultset rs; private boolean scrolling; } /** this panel displays the contents of a result set. */ class datapanel extends jpanel { /** constructs the data panel. @param rs the result set whose contents this panel displays */ public datapanel(resultset rs) throws sqlexception { fields = new arraylist <jtextfield> (); setlayout(new gridbaglayout()); gridbagconstraints gbc = new gridbagconstraints(); gbc.gridwidth = 1; gbc.gridheight = 1; resultsetmetadata rsmd = rs.getmetadata(); for (int i = 1; i <= rsmd.getcolumncount(); i++) { gbc.gridy = i - 1; string columnname = rsmd.getcolumnlabel(i); gbc.gridx = 0; gbc.anchor = gridbagconstraints.east; add(new jlabel(columnname), gbc); int columnwidth = rsmd.getcolumndisplaysize(i); jtextfield tb = new jtextfield(columnwidth); fields.add(tb); gbc.gridx = 1; gbc.anchor = gridbagconstraints.west; add(tb, gbc); } } /** shows a database row by populating all text fields with the column values. */ public void showrow(resultset rs) throws sqlexception { for (int i = 1; i <= fields.size(); i++) { string field = rs.getstring(i); jtextfield tb = (jtextfield) fields.get(i - 1); tb.settext(field); } } private arraylist <jtextfield> fields; } | | |
|