Java与MYSQL连接的精典例子,用Swing显示

javacoffe 2012-10-26

import java.sql.*;

importjavax.swing.*;

importjava.awt.*;

importjava.awt.event.*;

import java.util.*;

public class inensshow extends JFrame {

privateConnectionconnection;

privateStatementstatement;

privateResultSetresultSet;

private ResultSetMetaData rsMetaData;

//GUI变量定义

privateJTabletable;

privateJTextAreainputQuery;

private JButton submitQuery;

public inensshow()

{

//Form的标题

super( "输入SQL语句,按查询按钮查看结果。" );

String url = "jdbc:mysql://localhost:3306/test";

Stringusername="root";

Stringpassword="000000";

//加载驱动程序以连接数据库

try{

Class.forName("org.gjt.mm.mysql.Driver");

connection=DriverManager.getConnection(

url,username,password);

}

//捕获加载驱动程序异常

catch(ClassNotFoundExceptioncnfex){

System.err.println(

"装载JDBC/ODBC驱动程序失败。");

cnfex.printStackTrace();

System.exit(1);//terminateprogram

}

//捕获连接数据库异常

catch(SQLExceptionsqlex){

System.err.println("无法连接数据库");

sqlex.printStackTrace();

System.exit(1);//terminateprogram

}

//如果数据库连接成功,则建立GUI

//SQL语句

Stringtest="SELECT*FROMabout";

inputQuery=newJTextArea(test,4,30);

submitQuery=newJButton("查询");

//Button事件

submitQuery.addActionListener(

newActionListener(){

publicvoidactionPerformed(ActionEvente)

{

getTable();

}

}

);

JPanel topPanel = new JPanel();

topPanel.setLayout(newBorderLayout());

//将"输入查询"编辑框布置到"CENTER"

topPanel.add(newJScrollPane(inputQuery),BorderLayout.CENTER);

//将"提交查询"按钮布置到"SOUTH"

topPanel.add(submitQuery,BorderLayout.SOUTH);

table=newJTable();

Containerc=getContentPane();

c.setLayout(newBorderLayout());

//将"topPanel"编辑框布置到"NORTH"

c.add(topPanel,BorderLayout.NORTH);

//将"table"编辑框布置到"CENTER"

c.add(table,BorderLayout.CENTER);

getTable();

setSize(500,300);

//显示Form

show();

}

private void getTable()

{

try{

//执行SQL语句

Stringquery=inputQuery.getText();

statement=connection.createStatement();

resultSet=statement.executeQuery(query);

//在表格中显示查询结果

displayResultSet(resultSet);

}

catch(SQLExceptionsqlex){

sqlex.printStackTrace();

}

}

private void displayResultSet( ResultSet rs )

throwsSQLException

{

//定位到达第一条记录

booleanmoreRecords=rs.next();

//如果没有记录,则提示一条消息

if(!moreRecords){

JOptionPane.showMessageDialog(this,

"结果集中无记录");

setTitle("无记录显示");

return;

}

VectorcolumnHeads=newVector();

Vectorrows=newVector();

try{

//获取字段的名称

ResultSetMetaDatarsmd=rs.getMetaData();

for(inti=1;i<=rsmd.getColumnCount();++i)

columnHeads.addElement(rsmd.getColumnName(i));

//获取记录集

do{

rows.addElement(getNextRow(rs,rsmd));

}while(rs.next());

//在表格中显示查询结果

table=newJTable(rows,columnHeads);

JScrollPanescroller=newJScrollPane(table);

Containerc=getContentPane();

c.remove(1);

c.add(scroller,BorderLayout.CENTER);

//刷新Table

c.validate();

}

catch(SQLExceptionsqlex){

sqlex.printStackTrace();

}

}

privateVectorgetNextRow(ResultSetrs,

ResultSetMetaDatarsmd)

throwsSQLException

{

VectorcurrentRow=newVector();

for(inti=1;i<=rsmd.getColumnCount();++i)

currentRow.addElement(rs.getString(i));

//返回一条记录

returncurrentRow;

}

public void shutDown()

{

try{

//断开数据库连接

connection.close();

}

catch(SQLExceptionsqlex){

System.err.println("Unabletodisconnect");

sqlex.printStackTrace();

}

}

public static void main( String args[] )

{

finalinensshowapp=newinensshow();

app.addWindowListener(newWindowAdapter(){publicvoidwindowClosing(WindowEvente)

{

app.shutDown();

System.exit(0);

}

}

);

}

}

相关推荐