2013-10-03 38 views
-1

我已經知道顯示select * from和bla bla這樣的表的結果的代碼。 但我想顯示任何從MySQL到NetBeans的結果。 例如 - 如果用戶說「顯示數據庫」,它應該在textarea中顯示數據庫,對於表格也是如此。 我的意思是,無論用戶想看什麼,無論是查看錶或數據庫還是列或什麼,結果都應該顯示在textarea中。 而且我可能無法繼續爲這些行爲編寫命令,因爲有這麼多的可能性。 那麼請,有沒有什麼辦法可以將所有的結果從mysql顯示到java中的textarea中? 謝謝。如何顯示mysql中的任何結果到TextArea中Java

編輯: 那麼......我netbeans(是的,我對這個東西有點新),到目前爲止,這是我的代碼。我希望結果顯示在文本區域中。下面是編碼在textarea的框架存在,我想結果出現─>

package MYSQL_GUI; 
import static MYSQL_GUI.Frame1.j; 
import static MYSQL_GUI.Frame1.k; 
import com.mysql.jdbc.Connection; 
import com.mysql.jdbc.Statement; 
import java.awt.Dimension; 
import java.awt.Toolkit; 
import java.sql.DriverManager; 
import java.sql.ResultSet; 
import java.util.Scanner; 
import javax.swing.JOptionPane; 
public class CustComm extends javax.swing.JFrame { 

    /** 
    * Creates new form CustComm 
    */ 
    public CustComm() { 
     initComponents(); 
     Dimension dim = Toolkit.getDefaultToolkit().getScreenSize(); 
     this.setLocation(dim.width/2-this.getSize().width/2, dim.height/2-this.getSize().height/2); 
} 

    /** 
    * This method is called from within the constructor to initialize the form. 
    * WARNING: Do NOT modify this code. The content of this method is always 
    * regenerated by the Form Editor. 
    */ 
    @SuppressWarnings("unchecked") 
    // <editor-fold defaultstate="collapsed" desc="Generated Code">       
    private void initComponents() { 

     jLabel1 = new javax.swing.JLabel(); 
     jScrollPane1 = new javax.swing.JScrollPane(); 
     jTextArea1 = new javax.swing.JTextArea(); 
     jButton1 = new javax.swing.JButton(); 
     jButton2 = new javax.swing.JButton(); 
     jScrollPane2 = new javax.swing.JScrollPane(); 
     jTextArea2 = new javax.swing.JTextArea(); 
     jLabel2 = new javax.swing.JLabel(); 
     jScrollPane3 = new javax.swing.JScrollPane(); 
     jTextArea3 = new javax.swing.JTextArea(); 
     jLabel3 = new javax.swing.JLabel(); 

     setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE); 
     setTitle("MySQL Custom Command Editor"); 

     jLabel1.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N 
     jLabel1.setText("Enter Custom Commands Below"); 

     jTextArea1.setColumns(20); 
     jTextArea1.setRows(5); 
     jScrollPane1.setViewportView(jTextArea1); 

     jButton1.setText("Execute Command(s)"); 
     jButton1.addActionListener(new java.awt.event.ActionListener() { 
      public void actionPerformed(java.awt.event.ActionEvent evt) { 
       jButton1ActionPerformed(evt); 
      } 
     }); 

     jButton2.setText("Cancel"); 
     jButton2.addActionListener(new java.awt.event.ActionListener() { 
      public void actionPerformed(java.awt.event.ActionEvent evt) { 
       jButton2ActionPerformed(evt); 
      } 
     }); 

     jTextArea2.setEditable(false); 
     jTextArea2.setColumns(20); 
     jTextArea2.setLineWrap(true); 
     jTextArea2.setRows(2); 
     jTextArea2.setText("!!Separate commands by writing each command in new line with proper syntax just like in MySQL."); 
     jTextArea2.setFocusable(false); 
     jTextArea2.setHighlighter(null); 
     jTextArea2.setOpaque(false); 
     jScrollPane2.setViewportView(jTextArea2); 

     jLabel2.setText("Enter Command(s) below:"); 

     jTextArea3.setEditable(false); 
     jTextArea3.setColumns(10); 
     jTextArea3.setRows(5); 
     jScrollPane3.setViewportView(jTextArea3); 

     jLabel3.setText("Command Outputs:"); 

     javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); 
     getContentPane().setLayout(layout); 
     layout.setHorizontalGroup(
      layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
      .addGroup(layout.createSequentialGroup() 
       .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
        .addGroup(layout.createSequentialGroup() 
         .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
          .addGroup(layout.createSequentialGroup() 
           .addContainerGap() 
           .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false) 
            .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 600, Short.MAX_VALUE) 
            .addComponent(jScrollPane1))) 
          .addGroup(layout.createSequentialGroup() 
           .addGap(247, 247, 247) 
           .addComponent(jLabel2))) 
         .addGap(18, 18, 18) 
         .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
          .addComponent(jScrollPane3, javax.swing.GroupLayout.PREFERRED_SIZE, 164, javax.swing.GroupLayout.PREFERRED_SIZE) 
          .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup() 
           .addComponent(jLabel3) 
           .addGap(40, 40, 40)))) 
        .addGroup(layout.createSequentialGroup() 
         .addGap(222, 222, 222) 
         .addComponent(jLabel1)) 
        .addGroup(layout.createSequentialGroup() 
         .addGap(122, 122, 122) 
         .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 135, javax.swing.GroupLayout.PREFERRED_SIZE) 
         .addGap(95, 95, 95) 
         .addComponent(jButton2, javax.swing.GroupLayout.PREFERRED_SIZE, 135, javax.swing.GroupLayout.PREFERRED_SIZE))) 
       .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)) 
     ); 
     layout.setVerticalGroup(
      layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
      .addGroup(layout.createSequentialGroup() 
       .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 26, javax.swing.GroupLayout.PREFERRED_SIZE) 
       .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) 
       .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) 
       .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) 
       .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) 
        .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 21, javax.swing.GroupLayout.PREFERRED_SIZE) 
        .addComponent(jLabel3)) 
       .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) 
       .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
        .addComponent(jScrollPane3, javax.swing.GroupLayout.DEFAULT_SIZE, 240, Short.MAX_VALUE) 
        .addComponent(jScrollPane1)) 
       .addGap(18, 18, 18) 
       .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) 
        .addComponent(jButton1) 
        .addComponent(jButton2)) 
       .addContainerGap()) 
     ); 

     pack(); 
    }// </editor-fold>       

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {           
     String x = jTextArea1.getText(); 
     Scanner scanner = new Scanner(x); 
while (scanner.hasNextLine()) { 
    String line = scanner.nextLine(); 
    if(line.contains("use ")){ 
     if(line.contains("use ")&&!GlobalParams.usedb){ 
     JOptionPane.showMessageDialog(this, "Please use the Program to select database."); 
     } 
     else{ 
     try{ 
     Class.forName("java.sql.DriverManager"); 
     Connection con = (Connection) 
     DriverManager.getConnection("jdbc:mysql://localhost:"+GlobalParams.portvar+"/",GlobalParams.uservar, GlobalParams.passvar); 
     Statement stmnt = (Statement) con.createStatement(); 
     String query = line; 
     stmnt.executeUpdate(query); 
     JOptionPane.showMessageDialog(this, "Commands Executed Successfully"); 
     this.dispose();} 
     catch(Exception e){ 
     JOptionPane.showMessageDialog(this,e.getMessage()); 
    } 
    }} 
    if(line.contains("create table ")){ 
     if(line.contains("create table ")&&!GlobalParams.usedb){ 
     JOptionPane.showMessageDialog(this, "You can only create tables and manipulate them in next section"); 
     } 
     else{ 
     try{ 
     Class.forName("java.sql.DriverManager"); 
     Connection con = (Connection) 
     DriverManager.getConnection("jdbc:mysql://localhost:"+GlobalParams.portvar+"/",GlobalParams.uservar, GlobalParams.passvar); 
     Statement stmnt = (Statement) con.createStatement(); 
     String query = line; 
     stmnt.executeQuery("use "+GlobalParams.dbvar); 
     stmnt.executeUpdate(query); 
     JOptionPane.showMessageDialog(this, "Commands Executed Successfully"); 
     this.dispose();} 
     catch(Exception e){ 
     JOptionPane.showMessageDialog(this,e.getMessage()); 
    } 
    }} 
     else{ 
    try{ 
     Class.forName("java.sql.DriverManager"); 
     Connection con = (Connection) 
     DriverManager.getConnection("jdbc:mysql://localhost:"+GlobalParams.portvar+"/",GlobalParams.uservar, GlobalParams.passvar); 
     Statement stmnt = (Statement) con.createStatement(); 
     String query = line; 
     stmnt.executeUpdate(query); 
     JOptionPane.showMessageDialog(this, "Commands Executed Successfully"); 
     this.dispose();} 

    catch(Exception e){ 
     JOptionPane.showMessageDialog(this,e.getMessage()); 
    }}} 
try{ 
     Class.forName("java.sql.DriverManager"); 
     Connection con = (Connection) 
     DriverManager.getConnection("jdbc:mysql://localhost:"+GlobalParams.portvar+"/",""+k,""+j); 
     Statement stmnt = (Statement) con.createStatement(); 
     String query = "show databases"; 
     ResultSet rs = stmnt.executeQuery(query); 
     while (rs.next()){ 
     String db = rs.getString("Database"); 
     } 
} 
    catch(Exception e){ 
     JOptionPane.showMessageDialog(this,e.getMessage()); 
    } 
     GlobalParams.usedb=false; 
    }           

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {           
int dialogResult = JOptionPane.showConfirmDialog (this, "You may loose your commands if you did not execute them. Are you sure you want to Proceed?"); 
if(dialogResult == JOptionPane.YES_OPTION){dispose();} 
else{}; 
    }           

    /** 
    * @param args the command line arguments 
    */ 
    public static void main(String args[]) { 
     /* Set the Nimbus look and feel */ 
     //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) "> 
     /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel. 
     * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
     */ 
     try { 
      for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) { 
       if ("Nimbus".equals(info.getName())) { 
        javax.swing.UIManager.setLookAndFeel(info.getClassName()); 
        break; 
       } 
      } 
     } catch (ClassNotFoundException ex) { 
      java.util.logging.Logger.getLogger(CustComm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); 
     } catch (InstantiationException ex) { 
      java.util.logging.Logger.getLogger(CustComm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); 
     } catch (IllegalAccessException ex) { 
      java.util.logging.Logger.getLogger(CustComm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); 
     } catch (javax.swing.UnsupportedLookAndFeelException ex) { 
      java.util.logging.Logger.getLogger(CustComm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); 
     } 
     //</editor-fold> 

     /* Create and display the form */ 
     java.awt.EventQueue.invokeLater(new Runnable() { 
      public void run() { 
       new CustComm().setVisible(true); 
      } 
     }); 
    } 
    // Variables declaration - do not modify      
    private javax.swing.JButton jButton1; 
    private javax.swing.JButton jButton2; 
    private javax.swing.JLabel jLabel1; 
    private javax.swing.JLabel jLabel2; 
    private javax.swing.JLabel jLabel3; 
    private javax.swing.JScrollPane jScrollPane1; 
    private javax.swing.JScrollPane jScrollPane2; 
    private javax.swing.JScrollPane jScrollPane3; 
    public static javax.swing.JTextArea jTextArea1; 
    private javax.swing.JTextArea jTextArea2; 
    private javax.swing.JTextArea jTextArea3; 
    // End of variables declaration     
} 

這是編碼只有一個框架在那裏我希望有我的目的。 這裏是我完整的主框架編碼,以防萬一你需要它(我不認爲你會但仍然....) - >http://pastebin.com/ewnEpt8P#它太長,所以我不能在這裏發表。

+2

簡答:是的。長答案:是的,但你必須做一些事情。你有沒有爲此目標而做過的事情? – Makoto

+0

我準備好做「我需要的東西」。我搜索谷歌,但沒有太大的幫助。所以請給我一個解決方案。 – Hoobla

+1

我不會直接寫給你。你有沒有執行過的任何代碼?把這個問題放在你的問題上會有很大的幫助,因爲我們可以爲你提供一個起點。 – Makoto

回答

1

雖然不重要,但通常的方法是諮詢您的驅動程序的實現ResultSetMetaData的實例。你可以search for a tutorial,你喜歡,但我發現JDBCXYDataset,引用here,可以相當訪問。

補充:我想創建一個虛擬的MySQL。

如果沒有相當數量的代碼使用DatabaseMetaData來詢問數據庫的數據字典,沒有簡單的方法來做到這一點。一些例子被引用爲here

+0

它不是我正在尋找的東西,但我學到了很多新東西,所以謝謝。現在我正在尋找 - >您的鏈接只提供瞭如何從表格中獲取結果(通過使用select * from和bla bla)以及其他一些東西。但是我想要的是,不管命令是什麼,輸出都應該顯示在文本框中。這不僅意味着數據庫相關的命令,而且還包含表格操作命令。由於我不能爲每個命令指定「ifs」,所以我需要一些代碼來自動獲取由mysql給出的任何結果或輸出,即使它的錯誤。簡單地說,我想創建一個虛擬的mysql。 – Hoobla

+0

我試着在上面詳細說明。 – trashgod

+0

好的謝謝你給我提供的基礎。將工作。 – Hoobla