file 1 :
先把主程序的父类搬来!
/*
* 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 :
下面是主程序!
/*
* 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;//要所取的列
public void actionPerformed(ActionEvent e){
if(e.getSource() instanceof JButton){
sql=textsend.getText();
// row= Character.getNumericValue(sql.charAt(sql.length()-1));//得到要取的列数!
// initRsult();
Methods.ting(sql);//这个类集成了一些常用的方法!
rsult=Methods.rStr;
textreceive.setText(rsult);
}
}
public static void main(String[] args) {
Myframe mf=new Myframe();
mf.setVisible(true);
}
}
file 3 :
数据库连接类,并以Facade模式集成了相关方法!
/*
* date: 2004-11-17
* author:zhangyu6050;
* todo: 一个数据库封装类,作用是:
* 获取数据库连接
* 通过接受的SQL语句选择数据库并且返回结果
* 期间要作出以下判断:1 如果sql用来查询:、、、、
* 2 如果sql用来update:、、、、
*
* bugs:
* 改进:实现接口:sqlQuery;
* 主要继承一些比如ERROR,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) {
//连接发生错误!
System.out.print("连接发生错误!");
e.printStackTrace();
}
}
public Mysql(){
try {
getDataSource();
stmt=conn.createStatement();
} catch (SQLException e) {
System.out.print("conn.createStatement()发生错误!");
e.printStackTrace();
}
}
public Mysql(String aqurey){
try {
stmt=conn.createStatement();
prepareStatement(aqurey);
} catch (SQLException e) {
System.out.print("conn.createStatement()发生错误!");
e.printStackTrace();
}
}
public ResultSet find(String aquery) throws SQLException{
if(stmt==null) return null;
String query=aquery;
//最好经过一些防止sql注入的检测!
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())
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 :
处理数据库结果的方法集
/*
* date: 2004-11-18
* author:zhangyu6050;
* todo: 处理数据库结果的方法集!
* 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) {//根据sql按表的格式取出结果串
try {
//可以利用正则表达式来进行判断!
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\n";
while(r.next()){
for (int i = 1; i <columnCount+1; i++) {
rStr+=(r.getObject(i)+"\t\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);
}
*/
}
粗粗写来,没有完整。
请指教:)
本文地址:http://com.8s8s.com/it/it13317.htm