当前位置: 技术问答>java相关
师傅们帮我看看这段代码?谢谢了
来源: 互联网 发布时间:2015-10-30
本文导语: //InteractiveSQL.java我不明白的是第二个文件。谢谢了 import javax.swing.*; import java.awt.*; import javax.swing.table.*; import java.sql.*; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; import java.awt.event.ActionListener; import java....
//InteractiveSQL.java我不明白的是第二个文件。谢谢了
import javax.swing.*;
import java.awt.*;
import javax.swing.table.*;
import java.sql.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
/**
*
*
*
*
* @author unascribed
* @version 1.0
*/
public class InteractiveSQL extends JFrame implements ActionListener{
public static void main(String[] args){
//Create the application object
//Set default values for th command line args
String user="guest";
String password="guest";
String url="jdbc:odbc:technical_library";
String driver="sun.jdbc.odbc.JdbcOdbcDriver";
//Up to 4 arguments in the sequence database url,dreiver url,user ID, password
switch(args.length){
case 4:
password=args[3];
case 3:
user=args[2];
case 2:
driver=args[1];
case 1:
url=args[0];
}
InteractiveSQL theApp=new InteractiveSQL(driver,url,user,password);
}
public InteractiveSQL(String driver,String url,String user,String password){
super("InteractiveSql");
setBounds(0,0,400,300);
setDefaultCloseOperation(DISPOSE_ON_CLOSE);
addWindowListener(new WindowHandler());
//add the input for sql statements at the top
command.setToolTipText("Key SQL command and press Enter");
//add the input for for SQL statements at the top
command.addActionListener(this);
getContentPane().add(command,BorderLayout.NORTH);
//add the status reporting area at the botton
status.setLineWrap(true);
status.setWrapStyleWord(true);
getContentPane().add(status,BorderLayout.SOUTH);
//create the menubar from the menu items
JMenu fileMenu=new JMenu("File");
fileMenu.setMnemonic('F');
clearQueryItem.addActionListener(this);
exitItem.addActionListener(this);
fileMenu.add(clearQueryItem);
fileMenu.add(exitItem);
menuBar.add(fileMenu);
setJMenuBar(menuBar);
//establish a database connection and set up the table
try{
Class.forName(driver);
connection=DriverManager.getConnection(url,user,password);
statement=connection.createStatement();
model=new ResultModel();
JTable table=new JTable(model);
table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
resultsPane=new JScrollPane(table);
getContentPane().add(resultsPane,BorderLayout.CENTER);
}
catch(ClassNotFoundException cnfe){
System.err.println(cnfe);
}
catch(SQLException sqle){
System.err.println(sqle);
}
pack();
setVisible(true);
}
class WindowHandler extends WindowAdapter{
//Handler for window closing event
public void windowClosing(WindowEvent e){
dispose();
System.exit(0);
}
}
public void actionPerformed(ActionEvent e){
Object source=e.getSource();
if(source==command)
executeSQL();
else if(source==clearQueryItem)
command.setText("");
else if(source==exitItem){
dispose();
System.exit(0);
}
}
public void executeSQL(){
String query=command.getText();
if(query==null)
return;
try{
model.setResultSet(statement.executeQuery(query));
status.setText("Resultset has "+model.getRowCount()+" rows");
}
catch(SQLException sqle){
status.setText(sqle.getMessage());
}
}
JTextField command=new JTextField();
JTextArea status=new JTextArea(3,1);
JScrollPane resultsPane;
JMenuBar menuBar=new JMenuBar();
JMenuItem clearQueryItem=new JMenuItem("Clear query");
JMenuItem exitItem=new JMenuItem("Exit");
Connection connection;
Statement statement;
ResultModel model;
}
//ResultModel.java关键是这个文件!!!!!
import java.sql.*;
import javax.swing.table.*;
import java.util.Vector;
/**
*
*
*
*
* @author unascribed
* @version 1.0
*/
public class ResultModel extends AbstractTableModel {
String[] columnNames=new String[0];
Vector dataRows;
public void setResultSet(ResultSet results){
//Make the data in the resultset available through the TableModel interface...
try{
ResultSetMetaData metadata=results.getMetaData();
int columns=metadata.getColumnCount();
columnNames=new String[columns];/**@主要是这里。为什么我把这里改成String[] columnNames=new String[columns];之后就不能用了呢?Jtable组建就不能显示元数据了。!!!!?????谢谢了*/
//get the column names
for(int i=0;i
import javax.swing.*;
import java.awt.*;
import javax.swing.table.*;
import java.sql.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
/**
*
Title:
*
Description:
*
Copyright: Copyright (c) 2002
*
Company:
* @author unascribed
* @version 1.0
*/
public class InteractiveSQL extends JFrame implements ActionListener{
public static void main(String[] args){
//Create the application object
//Set default values for th command line args
String user="guest";
String password="guest";
String url="jdbc:odbc:technical_library";
String driver="sun.jdbc.odbc.JdbcOdbcDriver";
//Up to 4 arguments in the sequence database url,dreiver url,user ID, password
switch(args.length){
case 4:
password=args[3];
case 3:
user=args[2];
case 2:
driver=args[1];
case 1:
url=args[0];
}
InteractiveSQL theApp=new InteractiveSQL(driver,url,user,password);
}
public InteractiveSQL(String driver,String url,String user,String password){
super("InteractiveSql");
setBounds(0,0,400,300);
setDefaultCloseOperation(DISPOSE_ON_CLOSE);
addWindowListener(new WindowHandler());
//add the input for sql statements at the top
command.setToolTipText("Key SQL command and press Enter");
//add the input for for SQL statements at the top
command.addActionListener(this);
getContentPane().add(command,BorderLayout.NORTH);
//add the status reporting area at the botton
status.setLineWrap(true);
status.setWrapStyleWord(true);
getContentPane().add(status,BorderLayout.SOUTH);
//create the menubar from the menu items
JMenu fileMenu=new JMenu("File");
fileMenu.setMnemonic('F');
clearQueryItem.addActionListener(this);
exitItem.addActionListener(this);
fileMenu.add(clearQueryItem);
fileMenu.add(exitItem);
menuBar.add(fileMenu);
setJMenuBar(menuBar);
//establish a database connection and set up the table
try{
Class.forName(driver);
connection=DriverManager.getConnection(url,user,password);
statement=connection.createStatement();
model=new ResultModel();
JTable table=new JTable(model);
table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
resultsPane=new JScrollPane(table);
getContentPane().add(resultsPane,BorderLayout.CENTER);
}
catch(ClassNotFoundException cnfe){
System.err.println(cnfe);
}
catch(SQLException sqle){
System.err.println(sqle);
}
pack();
setVisible(true);
}
class WindowHandler extends WindowAdapter{
//Handler for window closing event
public void windowClosing(WindowEvent e){
dispose();
System.exit(0);
}
}
public void actionPerformed(ActionEvent e){
Object source=e.getSource();
if(source==command)
executeSQL();
else if(source==clearQueryItem)
command.setText("");
else if(source==exitItem){
dispose();
System.exit(0);
}
}
public void executeSQL(){
String query=command.getText();
if(query==null)
return;
try{
model.setResultSet(statement.executeQuery(query));
status.setText("Resultset has "+model.getRowCount()+" rows");
}
catch(SQLException sqle){
status.setText(sqle.getMessage());
}
}
JTextField command=new JTextField();
JTextArea status=new JTextArea(3,1);
JScrollPane resultsPane;
JMenuBar menuBar=new JMenuBar();
JMenuItem clearQueryItem=new JMenuItem("Clear query");
JMenuItem exitItem=new JMenuItem("Exit");
Connection connection;
Statement statement;
ResultModel model;
}
//ResultModel.java关键是这个文件!!!!!
import java.sql.*;
import javax.swing.table.*;
import java.util.Vector;
/**
*
Title:
*
Description:
*
Copyright: Copyright (c) 2002
*
Company:
* @author unascribed
* @version 1.0
*/
public class ResultModel extends AbstractTableModel {
String[] columnNames=new String[0];
Vector dataRows;
public void setResultSet(ResultSet results){
//Make the data in the resultset available through the TableModel interface...
try{
ResultSetMetaData metadata=results.getMetaData();
int columns=metadata.getColumnCount();
columnNames=new String[columns];/**@主要是这里。为什么我把这里改成String[] columnNames=new String[columns];之后就不能用了呢?Jtable组建就不能显示元数据了。!!!!?????谢谢了*/
//get the column names
for(int i=0;i