A simple database access application

Source: Internet
Author: User
Tags string find

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 ("query framework ");
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 actionreceivmed (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; // The column to be retrieved
Public void actionreceivmed (ActionEvent e ){
If (e. getSource () instanceof JButton ){
SQL = textsend. getText ();
// Row = Character. getNumericValue (SQL. charAt (SQL. length ()-1); // obtain the number of columns to be retrieved!
// InitRsult ();
Methods. ting (SQL); // This class integrates some common Methods!
Rsult = Methods. rStr;
Textreceive. setText (rsult );
}
}

Public static void main (String [] args ){
Myframe mf = new Myframe ();
Mf. setVisible (true );
}

}

 
File 3:

Database Connection class, and integrated related methods in Facade mode!

/*
* Date: 2004-11-17
* Author: zhangyu6050;
* Todo: a database encapsulation class used:
* Getting database connections
* Select the database through the accepted SQL statement and return the result
* The following judgment should be made during the period: 1 If SQL is used for query :,,,,
* 2 If SQL is used to update :,,,,
*
* Bugs:
* Improvement: Implementation interface: sqlQuery;
* It mainly inherits constants such as ERROR and SUCCESS!
*/
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 (){

Try {
Getdatasource ();
Stmt = conn. createstatement ();
} Catch (sqlexception e ){
System. Out. Print ("conn. createstatement () error! ");
E. printstacktrace ();
}
}

Public MySQL (string aqurey ){
Try {
Stmt = conn. createstatement ();
Preparestatement (aqurey );
} Catch (sqlexception e ){
System. Out. Print ("conn. createstatement () error! ");
E. printstacktrace ();
}

}
Public resultset find (string aquery) throws sqlexception {
If (stmt = NULL) return NULL;
String query = aquery;
// It is best to pass some checks to prevent SQL injection!
Return stmt.exe cuteQuery (query );
}
Public ResultSet find () throws SQLException {
If (prepstmt = null) return null;
Return prepstmt.exe cuteQuery ();
}
Public String find (String aquery, int row) throws SQLException {
ResultSet r = this. find (aquery );
String rs = "";
While (r. next ())
Rs + = (String) r. getObject (row) + "/n ";
Return rs;

}
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 set for processing database results
/*
* Date: 2004-11-18
* Author: zhangyu6050;
* Todo: method set for processing 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) {// retrieves the result String based on SQL in table format

Try {
// You can use a regular expression to determine!
If (SQL = null | SQL. equals ("") r = q1.find ("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 <= columnCount; I ++ ){
RStr + = rsmd. getColumnName (I) + "/t ";
}
RStr + = "/n ";
While (R. Next ()){
For (INT I = 1; I <columncount + 1; I ++ ){
Rstr + = (R. GetObject (I) + "/T ");
}
Rstr + = "/N ";
}
} Catch (sqlexception e ){
E. printstacktrace ();
}

}
/* Public static void main (string [] ARGs ){
String S = "123 ";
Int I = character. getnumericvalue (S. charat (2 ));
Ting (null );

System. Out. println (rstr );
}
*/
}

Not complete.
Please advise :)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.