package connmysql;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.sql.*;
import com.borland.jbcl.layout.*;
import javax.swing.event.*;
//import org.gjt.mm.mysql.*;
/**
* <p>Title: Conn MySQL</p>
* <p>Description: 连接MySql数据库</p>
* <p>Copyright: Copyright (c) 2004</p>
* <p>Company: 塞北的雪国际科技有限责任公司</p>
* @author 塞北的雪
* @version 1.0
*/
public class ConnMySQL extends JFrame {
public static void main( String argc[]){
ConnMySQL tmp= new ConnMySQL();
tmp.pack() ;
tmp.show() ;
}
JPanel contentPane;
JLabel jLabel1 = new JLabel();
XYLayout xYLayout1 = new XYLayout();
JButton jButton1 = new JButton();
JTextArea jTextArea1 = new JTextArea();
JTextField jTextField1 = new JTextField();
JButton jButton3 = new JButton();
JButton jButton2 = new JButton();
Button TextClear = new Button();
//定义全局变量
//定义连接数据库时候的一些属性
public String DBUser = "root";
public String DBPassword = "";
public String DBName = "sbdx";
public Connection conn;
public Statement sqlStmt;
public ResultSet sqlRst;
//End
//Construct the frame
public ConnMySQL() {
enableEvents(AWTEvent.WINDOW_EVENT_MASK);
try {
jbInit();
jButton3.setEnabled( false);
}
catch (Exception e) {
e.printStackTrace();
}
}
//Component initialization
private void jbInit() throws Exception {
contentPane = (JPanel)this.getContentPane();
jLabel1.setFont( new java.awt.Font( "SansSerif", 0, 20));
jLabel1.setText( "本程序由塞北的雪编写");
jLabel1.addAncestorListener( new ConnMySQL_jLabel1_ancestorAdapter( this));
contentPane.setLayout(xYLayout1);
this.setSize( new Dimension(534, 419));
this.setTitle( "连接MySQL数据库");
jButton1.setText( "连接MySQL服务器");
jButton1.addActionListener( new ConnMySQL_jButton1_actionAdapter( this));
jTextArea1.setText( "");
jButton3.setDoubleBuffered( false);
jButton3.setText( "执行");
jButton3.addActionListener( new ConnMySQL_jButton3_actionAdapter( this));
jButton2.setText( "退出");
jButton2.addActionListener( new ConnMySQL_jButton2_actionAdapter( this));
jTextField1.setText( "select * from pwd where id<=10");
jTextArea1.setAutoscrolls( true); //设置自动显示滚动条,但是好象没用!
TextClear.setLabel( "Clear");
TextClear.addActionListener( new ConnMySQL_TextClear_actionAdapter( this));
contentPane.add(jTextArea1, new XYConstraints(-1, 66, 535, 354));
contentPane.add(jButton3, new XYConstraints(469, 34, 65, 30));
contentPane.add(jTextField1, new XYConstraints(2, 34, 465, 29));
contentPane.add(jButton1, new XYConstraints(0, 0, -1, 29));
contentPane.add(jButton2, new XYConstraints(450, 0, 84, 34));
contentPane.add(TextClear, new XYConstraints(377, 0, 71, 34));
contentPane.add(jLabel1, new XYConstraints(147, 2, -1, -1));
}
//Overridden so we can exit when window is closed
protected void processWindowEvent(WindowEvent e) {
super.processWindowEvent(e);
if (e.getID() == WindowEvent.WINDOW_CLOSING) {
System.exit(0);
}
}
void jButton1_actionPerformed(ActionEvent e) {
String url= "jdbc:mysql://localhost:3306/"+DBName+ "?useUnicode=true&characterEncoding=gb2312";
try {
Class.forName( "org.gjt.mm.mysql.Driver").newInstance();
conn=java.sql.DriverManager.getConnection(url, DBUser,DBPassword);
jButton3.setEnabled( true);
Statement sqlStmt=conn.createStatement() ;
String sql=jTextField1.getText();
ResultSet sqlRst=sqlStmt.executeQuery(sql) ;
ResultSetMetaData rsm=sqlRst.getMetaData();
sql= "";
//循环显示执行SQL后的结果
for( int Count_i=1;Count_i<=rsm.getColumnCount();Count_i++){ //显示表的字段
sql += rsm.getColumnName(Count_i) + "/t";
}
sql+= "/n";
while(sqlRst.next()){
for( int j=1;j<=rsm.getColumnCount();j++){
sql+=sqlRst.getString(j)+ "/t";
}
sql+= "/n";
}
//结束循环
//追加文字
jTextArea1.append(sql) ;
//End
sqlRst.close() ;
sqlStmt.close() ;
// conn.close() ;
}
catch(SQLException sqlerr){
System.out.println( "连接MySql服务器出错!");
sqlerr.printStackTrace() ;
}
catch (Exception Conne){
Conne.printStackTrace() ;
}
}
void jButton2_actionPerformed(ActionEvent e) {
System.exit(0);
}
void TextClear_actionPerformed(ActionEvent e) {
jTextArea1.setText( "");
}
void jButton3_actionPerformed(ActionEvent e) {
jTextArea1.setText( "");
String sql=jTextField1.getText();
try{
sqlStmt = conn.createStatement();
sqlRst = sqlStmt.executeQuery(sql);
ResultSetMetaData rsm = sqlRst.getMetaData();
sql= "";
//循环显示执行SQL后的结果
for ( int Count_i = 1; Count_i <= rsm.getColumnCount(); Count_i++) { //显示表的字段
sql += rsm.getColumnName(Count_i) + "/t";
}
sql += "/n";
while (sqlRst.next()) {
for ( int j = 1; j <= rsm.getColumnCount(); j++) {
sql += sqlRst.getString(j) + "/t";
}
sql += "/n";
} //结束循环
jTextArea1.setText( "");
jTextArea1.append(sql);
}
catch(SQLException sqlerr){
System.out.println( "连接MySql服务器出错!");
sqlerr.printStackTrace() ;
}
catch (Exception Conne){
Conne.printStackTrace() ;
}
}
class ConnMySQL_jButton1_actionAdapter
implements java.awt.event.ActionListener {
ConnMySQL adaptee;
ConnMySQL_jButton1_actionAdapter(ConnMySQL adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e) {
adaptee.jButton1_actionPerformed(e);
}
}
class ConnMySQL_jButton2_actionAdapter implements java.awt.event.ActionListener {
ConnMySQL adaptee;
ConnMySQL_jButton2_actionAdapter(ConnMySQL adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e) {
adaptee.jButton2_actionPerformed(e);
}
}
class ConnMySQL_jLabel1_ancestorAdapter implements javax.swing.event.AncestorListener {
ConnMySQL adaptee;
ConnMySQL_jLabel1_ancestorAdapter(ConnMySQL adaptee) {
this.adaptee = adaptee;
}
public void ancestorAdded(AncestorEvent e) {
}
public void ancestorRemoved(AncestorEvent e) {
}
public void ancestorMoved(AncestorEvent e) {
}
}
class ConnMySQL_TextClear_actionAdapter implements java.awt.event.ActionListener {
ConnMySQL adaptee;
ConnMySQL_TextClear_actionAdapter(ConnMySQL adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e) {
adaptee.TextClear_actionPerformed(e);
}
}
class ConnMySQL_jButton3_actionAdapter implements java.awt.event.ActionListener {
ConnMySQL adaptee;
ConnMySQL_jButton3_actionAdapter(ConnMySQL adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e) {
adaptee.jButton3_actionPerformed(e);
}
}
}
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.sql.*;
import com.borland.jbcl.layout.*;
import javax.swing.event.*;
//import org.gjt.mm.mysql.*;
/**
* <p>Title: Conn MySQL</p>
* <p>Description: 连接MySql数据库</p>
* <p>Copyright: Copyright (c) 2004</p>
* <p>Company: 塞北的雪国际科技有限责任公司</p>
* @author 塞北的雪
* @version 1.0
*/
public class ConnMySQL extends JFrame {
public static void main( String argc[]){
ConnMySQL tmp= new ConnMySQL();
tmp.pack() ;
tmp.show() ;
}
JPanel contentPane;
JLabel jLabel1 = new JLabel();
XYLayout xYLayout1 = new XYLayout();
JButton jButton1 = new JButton();
JTextArea jTextArea1 = new JTextArea();
JTextField jTextField1 = new JTextField();
JButton jButton3 = new JButton();
JButton jButton2 = new JButton();
Button TextClear = new Button();
//定义全局变量
//定义连接数据库时候的一些属性
public String DBUser = "root";
public String DBPassword = "";
public String DBName = "sbdx";
public Connection conn;
public Statement sqlStmt;
public ResultSet sqlRst;
//End
//Construct the frame
public ConnMySQL() {
enableEvents(AWTEvent.WINDOW_EVENT_MASK);
try {
jbInit();
jButton3.setEnabled( false);
}
catch (Exception e) {
e.printStackTrace();
}
}
//Component initialization
private void jbInit() throws Exception {
contentPane = (JPanel)this.getContentPane();
jLabel1.setFont( new java.awt.Font( "SansSerif", 0, 20));
jLabel1.setText( "本程序由塞北的雪编写");
jLabel1.addAncestorListener( new ConnMySQL_jLabel1_ancestorAdapter( this));
contentPane.setLayout(xYLayout1);
this.setSize( new Dimension(534, 419));
this.setTitle( "连接MySQL数据库");
jButton1.setText( "连接MySQL服务器");
jButton1.addActionListener( new ConnMySQL_jButton1_actionAdapter( this));
jTextArea1.setText( "");
jButton3.setDoubleBuffered( false);
jButton3.setText( "执行");
jButton3.addActionListener( new ConnMySQL_jButton3_actionAdapter( this));
jButton2.setText( "退出");
jButton2.addActionListener( new ConnMySQL_jButton2_actionAdapter( this));
jTextField1.setText( "select * from pwd where id<=10");
jTextArea1.setAutoscrolls( true); //设置自动显示滚动条,但是好象没用!
TextClear.setLabel( "Clear");
TextClear.addActionListener( new ConnMySQL_TextClear_actionAdapter( this));
contentPane.add(jTextArea1, new XYConstraints(-1, 66, 535, 354));
contentPane.add(jButton3, new XYConstraints(469, 34, 65, 30));
contentPane.add(jTextField1, new XYConstraints(2, 34, 465, 29));
contentPane.add(jButton1, new XYConstraints(0, 0, -1, 29));
contentPane.add(jButton2, new XYConstraints(450, 0, 84, 34));
contentPane.add(TextClear, new XYConstraints(377, 0, 71, 34));
contentPane.add(jLabel1, new XYConstraints(147, 2, -1, -1));
}
//Overridden so we can exit when window is closed
protected void processWindowEvent(WindowEvent e) {
super.processWindowEvent(e);
if (e.getID() == WindowEvent.WINDOW_CLOSING) {
System.exit(0);
}
}
void jButton1_actionPerformed(ActionEvent e) {
String url= "jdbc:mysql://localhost:3306/"+DBName+ "?useUnicode=true&characterEncoding=gb2312";
try {
Class.forName( "org.gjt.mm.mysql.Driver").newInstance();
conn=java.sql.DriverManager.getConnection(url, DBUser,DBPassword);
jButton3.setEnabled( true);
Statement sqlStmt=conn.createStatement() ;
String sql=jTextField1.getText();
ResultSet sqlRst=sqlStmt.executeQuery(sql) ;
ResultSetMetaData rsm=sqlRst.getMetaData();
sql= "";
//循环显示执行SQL后的结果
for( int Count_i=1;Count_i<=rsm.getColumnCount();Count_i++){ //显示表的字段
sql += rsm.getColumnName(Count_i) + "/t";
}
sql+= "/n";
while(sqlRst.next()){
for( int j=1;j<=rsm.getColumnCount();j++){
sql+=sqlRst.getString(j)+ "/t";
}
sql+= "/n";
}
//结束循环
//追加文字
jTextArea1.append(sql) ;
//End
sqlRst.close() ;
sqlStmt.close() ;
// conn.close() ;
}
catch(SQLException sqlerr){
System.out.println( "连接MySql服务器出错!");
sqlerr.printStackTrace() ;
}
catch (Exception Conne){
Conne.printStackTrace() ;
}
}
void jButton2_actionPerformed(ActionEvent e) {
System.exit(0);
}
void TextClear_actionPerformed(ActionEvent e) {
jTextArea1.setText( "");
}
void jButton3_actionPerformed(ActionEvent e) {
jTextArea1.setText( "");
String sql=jTextField1.getText();
try{
sqlStmt = conn.createStatement();
sqlRst = sqlStmt.executeQuery(sql);
ResultSetMetaData rsm = sqlRst.getMetaData();
sql= "";
//循环显示执行SQL后的结果
for ( int Count_i = 1; Count_i <= rsm.getColumnCount(); Count_i++) { //显示表的字段
sql += rsm.getColumnName(Count_i) + "/t";
}
sql += "/n";
while (sqlRst.next()) {
for ( int j = 1; j <= rsm.getColumnCount(); j++) {
sql += sqlRst.getString(j) + "/t";
}
sql += "/n";
} //结束循环
jTextArea1.setText( "");
jTextArea1.append(sql);
}
catch(SQLException sqlerr){
System.out.println( "连接MySql服务器出错!");
sqlerr.printStackTrace() ;
}
catch (Exception Conne){
Conne.printStackTrace() ;
}
}
class ConnMySQL_jButton1_actionAdapter
implements java.awt.event.ActionListener {
ConnMySQL adaptee;
ConnMySQL_jButton1_actionAdapter(ConnMySQL adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e) {
adaptee.jButton1_actionPerformed(e);
}
}
class ConnMySQL_jButton2_actionAdapter implements java.awt.event.ActionListener {
ConnMySQL adaptee;
ConnMySQL_jButton2_actionAdapter(ConnMySQL adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e) {
adaptee.jButton2_actionPerformed(e);
}
}
class ConnMySQL_jLabel1_ancestorAdapter implements javax.swing.event.AncestorListener {
ConnMySQL adaptee;
ConnMySQL_jLabel1_ancestorAdapter(ConnMySQL adaptee) {
this.adaptee = adaptee;
}
public void ancestorAdded(AncestorEvent e) {
}
public void ancestorRemoved(AncestorEvent e) {
}
public void ancestorMoved(AncestorEvent e) {
}
}
class ConnMySQL_TextClear_actionAdapter implements java.awt.event.ActionListener {
ConnMySQL adaptee;
ConnMySQL_TextClear_actionAdapter(ConnMySQL adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e) {
adaptee.TextClear_actionPerformed(e);
}
}
class ConnMySQL_jButton3_actionAdapter implements java.awt.event.ActionListener {
ConnMySQL adaptee;
ConnMySQL_jButton3_actionAdapter(ConnMySQL adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e) {
adaptee.jButton3_actionPerformed(e);
}
}
}