FILE 1: First move the parent class of the main program! / * * Date: 2004-11-20 * author: zhangyu6050; * Todo: * bugs: * / package sd; import javax.swing. *; Import java.awt. *; Import java.awt.event. *;
public abstract class MyQQPan extends JFrame implements ActionListener {JTextArea textreceive = new JTextArea (); JTextArea textsend = new JTextArea ( "select * from Employees"); JButton button = new JButton ( "Send"); public MyQQPan () {// init Controls settitle; setbounds (50, 50, 500, 400); getContentPane (). setLayout (null);
getContentPane () add (textreceive);.. getContentPane () add (textsend);. getContentPane () add (button); button.addActionListener (this); textreceive.setBounds (0,0,450,300); textsend.setBounds (10,320,350,60 ); Button.SetBounds (370, 320, 70, 30);} public abstract void actionPerformed (ActionEvent Event);} file 2: Below is the main program! / * * Date: 2004-11-19 * author: zhangyu6050; * Todo: * bugs: * / package sd; // textareademo.java import java.awt. *; Import java.awt.event.ActionEvent; import Java. AWT.Event.ActionListener; import java.sql.resultset; import java.sql.resultsetmetadata; import java.sql.sqlexception;
import javax.swing *;. import sd.Mysql; public class Myframe extends MyQQPan {private String sql; private String rsult; // private int row; // be taken columns public void actionPerformed (ActionEvent e) {if (e .getsource () instanceof jbutton) {sql = textsend.getText (); // row = character.getNumericValue (SQL.Charat (Sql.Length () - 1)); // Get the number of columns to be taken! // initrsult (); methods.ting (sql); // This class integrates some common methods! Rsult = methods.rs.rst;}}}} public static void main (string [] args) {myframe mf = new myframe (); mf.setvisible (TRUE);}}
File 3:
Database connection class and integrate related methods in a FACADE mode!
/ * * Date: zhangyu6050; * Todo: A database package class, the role is: * Get database connection * Select the database by accepting the SQL statement and returns the result * The following judgment is made: 1 If SQL is used to query :, * / Package sd; import java.util.List; import java.sql *; public class Mysql {private String dirverName = "com.microsoft.jdbc.sqlserver.SQLServerDriver"; private String URL = "jdbc:. Microsoft: sqlserver: // localhost: 1433; DatabaseName = Northwind "; private String password =" sa "; private String userName =" sa "; private Connection conn = null; private Statement stmt = null; private PreparedStatement prepstmt = null; public void getDataSource () {Try {class.Forname (DirverName); conn = drivermanager.getConnection (URL, UserName, Password);
} Catch (exception e) {// Connection error! System.out.print ("Connection error!"); E.PrintStackTrace ();}} public mysql ()}} public mySQL ()} {getDataSource (); stmt = conn.createstatement ();} catch (sqlexception e) {system. Out.print ("Conn.createStatement () Errors!"); E.PrintStackTrace ();}} public mysql (string aqurey) {try {stmt = conn.createstatement (); prepareStatement (aqurey);} catch (SQLException e) {system.out.print ("Conn.createStatement () Errors!"); E.PrintStackTrace ();}} PUBLIC RESULTSET FIND (STRING AQUERY) THROWS SQLEXCEPTION {IF (STMT == Null) Return Null; string Query = AQUERY; / / It is best to have some detection to prevent SQL injection! return stmt.executeQuery (query);} public ResultSet find () throws SQLException {if (prepstmt == null) return null; return prepstmt.executeQuery ();} public String find (String aquery, int row) throws SQLException {ResultSet r = this.Find (AQuery); String RS = ""; while (r.next ()) R.GetObject (Row) "/ n"; return} public void preparestatement (String SQL) throws SQLException {prepstmt = conn.prepareStatement (sql);} public void close () throws SQLException {if (prepstmt = null!) prepstmt.close (); if (stmt = null!) prepstmt.close ();}
} file 4: Method of processing database results / * * Date: 2004-11-18 * Author: zhangyu6050; * Todo: Handling the method set of database results! * Bugs: * / package sd;
import java.sql.ResultSet; import java.sql.ResultSetMetaData; import java.sql.SQLException; public class Methods {static Mysql q1 = new Mysql (); static ResultSet r; static String rStr = ""; public static void ting ( String SQL) {// Remove the result string try {// according to the format of SQL, can use the regular expression to make judgments! IF (SQL == NULL || SQL.Equals ("" "Select Employeeid, Lastname from Employees"); Else R = Q1.Find ("Select Employeeid, Lastname from Employees"; ResultSetMetadata RSMD = R.getMetadata (); int columncount = rsmd.getColumncount (); for (int i = 1; i <= color = = ^ ) {RSTR = rsmd.getColumnName (i) "/ t";} RSTR = " / N / N "; while (R.Next ()) {for (INT i = 1; i Rough written, no intact. Please advise:)