Java Connection MySQL

xiaoxiao2021-03-06  104

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. *;

/ ** *

Title: conn mysql *

Description: Connect MySQL Database *

Copyright: Copyright (C) 2004 *

Company: Saichi Snow International Technology Co., Ltd. * @Author Saichi Snow * @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 ();

/ / Define global variables

/ / Define some properties when connecting the database

public

String dbuser =

"root";

public

String dbpassword =

""

public

String DBNAME =

"sbdx";

Public connect 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

"This procedure is written by Symbing Snow");

Jlabel1.AddanceStorListener

New connmysql_jlabel1_ancestoradapter

THIS));

Contentpane.setLayout (xylayout1);

THIS.SETSIZE

New Dimension (534, 419)); this.SetTitle

"Connect the MySQL Database");

JButton1.setText

"Connect MySQL Server");

JButton1.AddActionListener

New connmysql_jbutton1_actionadapter

THIS));

Jtextarea1.settext

"" "

JButton3.SetdoubleBuffered (

FALSE);

JButton3.setText

"carried out");

JButton3.AddActionListener

New connmysql_jbutton3_actionadapter

THIS));

JButton2.Settext

"drop out");

JButton2.AddActionListener

New connmysql_jbutton2_actionadapter

THIS));

JtextField1.setText (

"SELECT * from PWD where id <= 10");

Jtextarea1.setautoscrolls (

True);

/ / Set the automatic display of the scroll bar, but it seems that it is useless!

TextClear.SetLabel

"Clean");

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_CLOSIING) {

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 =

""

// loop display the result after executing SQL

FOR

INT count_i = 1; count_i <= rsm.getColumnCount (); count_i ) {

// Display the field of the table

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";

}

// End cycle

// Add a text

Jtextarea1.append (sql);

// end

Sqlrst.close ();

SQLSTMT.CLOSE ();

// conn.close ();

}

Catch (SQLException SQLERR) {

System.out.println (

"Connect the MySQL server error!");

SQLERR.PRINTSTACKTRACE ();

}

Catch (Exception Conne) {

Conne.PrintStackTrace ();

}

}

Void JButton2_ActionPerformed (ActionEvent E) {

System.exit (0);

}

Void textclear_actionperperformed (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 =

""

// loop display the result after executing SQL

FOR

INT count_i = 1; count_i <= rsm.getColumnCount (); count_i ) {

// Display the field of the table

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";

}

// End cycle

Jtextarea1.settext

"" "

Jtextarea1.append (sql);

}

Catch (SQLException SQLERR) {

System.out.println (

"Connect the MySQL server error!");

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_ACTIONADAPTEE (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 Annestorremoved (Anchestorevent E) {

}

public

Void Ancestormoved (Anchestorevent E) {

}

}

Class connmysql_textclear_actionadapter import.Ava.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);

}

}

}

转载请注明原文地址:https://www.9cbs.com/read-125187.html

New Post(0)