如何在netbeans中从控制台获取数据?

如何在netbeans中从控制台获取数据?,netbeans,console,sparql,Netbeans,Console,Sparql,对于我的数据检索,使用sparql的java程序在netbeans的输出区域中为我提供了一个表输出 ResultSetFormatter.out(System.out,results,query1) 以表格形式给我输出。现在我必须从输出区域获取此表格,并使用jframe将其显示在文本区域中。我如何实现它?您能告诉我如何从输出区获取数据吗?我认为您正试图全面实现您的目标。如果我理解正确,您正在尝试将结果集输出到JTable 请参见下面的示例: /* Java Swing,第二版 马克·罗伊、罗伯特

对于我的数据检索,使用sparql的java程序在netbeans的输出区域中为我提供了一个表输出

ResultSetFormatter.out(System.out,results,query1)


以表格形式给我输出。现在我必须从输出区域获取此表格,并使用jframe将其显示在文本区域中。我如何实现它?您能告诉我如何从输出区获取数据吗?

我认为您正试图全面实现您的目标。如果我理解正确,您正在尝试将结果集输出到JTable

请参见下面的示例:

/*
Java Swing,第二版
马克·罗伊、罗伯特·埃克斯坦、戴夫·伍德、詹姆斯·艾略特、布赖恩·科尔
ISBN:0-596-00408-7
出版商:O'Reilly
*/
//DatabaseTest.java
//让我们尝试使这些数据库中的一个与用于ouptut的JTable一起工作。
//
导入java.awt.BorderLayout;
导入java.awt.GridLayout;
导入java.awt.event.ActionEvent;
导入java.awt.event.ActionListener;
导入java.sql.Connection;
导入java.sql.DriverManager;
导入java.sql.ResultSet;
导入java.sql.ResultSetMetaData;
导入java.sql.Statement;
导入java.util.Vector;
导入javax.swing.JButton;
导入javax.swing.JFrame;
导入javax.swing.JLabel;
导入javax.swing.JPanel;
导入javax.swing.JScrollPane;
导入javax.swing.JTable;
导入javax.swing.JTextField;
导入javax.swing.table.AbstractTableModel;
公共类DatabaseTest扩展了JFrame{
JTextField-hostField;
JTextField-queryField;
查询表模型;
公共数据库测试(){
超级(“数据库测试框架”);
setDefaultCloseOperation(关闭时退出);
设置大小(350200);
qtm=新的QueryTableModel();
JTable table=新的JTable(qtm);
JScrollPane scrollpane=新的JScrollPane(表);
JPanel p1=新的JPanel();
p1.设置布局(新网格布局(3,2));
p1.添加(新的JLabel(“输入主机URL:”);
p1.add(hostField=newjtextfield());
p1.添加(新的JLabel(“输入您的查询:”);
p1.add(queryField=newjtextfield());
p1.添加(新JLabel(“单击此处发送:”);
JButton jb=新JButton(“搜索”);
jb.addActionListener(新ActionListener(){
已执行的公共无效操作(操作事件e){
setHostURL(hostField.getText().trim());
setQuery(queryField.getText().trim());
}
});
p1.添加(jb);
getContentPane().add(p1,BorderLayout.NORTH);
getContentPane().add(滚动窗格,BorderLayout.CENTER);
}
公共静态void main(字符串参数[]){
DatabaseTest tt=新的DatabaseTest();
tt.setVisible(真);
}
}
//QueryTableModel.java
//TableModel接口的基本实现,用于填充
//查询结果集中的字符串[]结构。
//
类QueryTableModel扩展了AbstractTableModel{
向量缓存;//将保存字符串[]对象。
int colCount;
字符串[]头;
连接数据库;
声明;
字符串currentURL;
公共查询表模型(){
cache=新向量();
新的gsl.sql.driv.Driver();
}
公共字符串getColumnName(int i){
返回标题[i];
}
public int getColumnCount(){
返回colCount;
}
public int getRowCount(){
返回cache.size();
}
公共对象getValueAt(整数行,整数列){
返回((字符串[])cache.elementAt(行))[col];
}
public void setHostURL(字符串url){
if(url.equals(currentURL)){
//相同的数据库,我们可以保持当前连接打开
返回;
}
//哦…需要新的连接
closeDB();
initDB(url);
currentURL=url;
}
//所有实际工作都发生在这里;在实际应用程序中,
//我们可能会在单独的线程中执行查询。
公共void setQuery(字符串q){
cache=新向量();
试一试{
//执行查询并存储结果集及其元数据
结果集rs=语句执行(q);
ResultSetMetaData meta=rs.getMetaData();
colCount=meta.getColumnCount();
//现在,我们必须用新的列名重建headers数组
headers=新字符串[colCount];
对于(int h=1;h
    /*
Java Swing, 2nd Edition
By Marc Loy, Robert Eckstein, Dave Wood, James Elliott, Brian Cole
ISBN: 0-596-00408-7
Publisher: O'Reilly 
*/
// DatabaseTest.java
//Let's try to make one of these databases work with a JTable for ouptut.
//

import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.Statement;
import java.util.Vector;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.table.AbstractTableModel;

public class DatabaseTest extends JFrame {

  JTextField hostField;

  JTextField queryField;

  QueryTableModel qtm;

  public DatabaseTest() {
    super("Database Test Frame");
    setDefaultCloseOperation(EXIT_ON_CLOSE);
    setSize(350, 200);

    qtm = new QueryTableModel();
    JTable table = new JTable(qtm);
    JScrollPane scrollpane = new JScrollPane(table);
    JPanel p1 = new JPanel();
    p1.setLayout(new GridLayout(3, 2));
    p1.add(new JLabel("Enter the Host URL: "));
    p1.add(hostField = new JTextField());
    p1.add(new JLabel("Enter your query: "));
    p1.add(queryField = new JTextField());
    p1.add(new JLabel("Click here to send: "));

    JButton jb = new JButton("Search");
    jb.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent e) {
        qtm.setHostURL(hostField.getText().trim());
        qtm.setQuery(queryField.getText().trim());
      }
    });
    p1.add(jb);
    getContentPane().add(p1, BorderLayout.NORTH);
    getContentPane().add(scrollpane, BorderLayout.CENTER);
  }

  public static void main(String args[]) {
    DatabaseTest tt = new DatabaseTest();
    tt.setVisible(true);
  }
}

//QueryTableModel.java
//A basic implementation of the TableModel interface that fills out a Vector of
//String[] structures from a query's result set.
//

class QueryTableModel extends AbstractTableModel {
  Vector cache; // will hold String[] objects . . .

  int colCount;

  String[] headers;

  Connection db;

  Statement statement;

  String currentURL;

  public QueryTableModel() {
    cache = new Vector();
    new gsl.sql.driv.Driver();
  }

  public String getColumnName(int i) {
    return headers[i];
  }

  public int getColumnCount() {
    return colCount;
  }

  public int getRowCount() {
    return cache.size();
  }

  public Object getValueAt(int row, int col) {
    return ((String[]) cache.elementAt(row))[col];
  }

  public void setHostURL(String url) {
    if (url.equals(currentURL)) {
      // same database, we can leave the current connection open
      return;
    }
    // Oops . . . new connection required
    closeDB();
    initDB(url);
    currentURL = url;
  }

  // All the real work happens here; in a real application,
  // we'd probably perform the query in a separate thread.
  public void setQuery(String q) {
    cache = new Vector();
    try {
      // Execute the query and store the result set and its metadata
      ResultSet rs = statement.executeQuery(q);
      ResultSetMetaData meta = rs.getMetaData();
      colCount = meta.getColumnCount();

      // Now we must rebuild the headers array with the new column names
      headers = new String[colCount];
      for (int h = 1; h <= colCount; h++) {
        headers[h - 1] = meta.getColumnName(h);
      }

      // and file the cache with the records from our query. This would
      // not be
      // practical if we were expecting a few million records in response
      // to our
      // query, but we aren't, so we can do this.
      while (rs.next()) {
        String[] record = new String[colCount];
        for (int i = 0; i < colCount; i++) {
          record[i] = rs.getString(i + 1);
        }
        cache.addElement(record);
      }
      fireTableChanged(null); // notify everyone that we have a new table.
    } catch (Exception e) {
      cache = new Vector(); // blank it out and keep going.
      e.printStackTrace();
    }
  }

  public void initDB(String url) {
    try {
      db = DriverManager.getConnection(url);
      statement = db.createStatement();
    } catch (Exception e) {
      System.out.println("Could not initialize the database.");
      e.printStackTrace();
    }
  }

  public void closeDB() {
    try {
      if (statement != null) {
        statement.close();
      }
      if (db != null) {
        db.close();
      }
    } catch (Exception e) {
      System.out.println("Could not close the current connection.");
      e.printStackTrace();
    }
  }
}