我已经知道从表中显示结果的代码,例如 select * from 和 bla bla。但我想显示从 mysql 到 netbeans 的任何结果。例如-> 如果用户说“显示数据库”,它应该在 textarea 中显示数据库,表格也是如此。我的意思是,无论用户想看到什么,无论是看到表、数据库或列还是什么,结果都应该出现在 textarea 中。而且我可能无法继续为这些操作中的每一个编写命令,因为有很多可能性。那么请问,有没有办法将mysql中的所有结果显示到java中的textarea中?谢谢。
编辑:嗯......我得到了netbeans(是的,我对这些东西有点陌生),到目前为止这是我的代码。我希望结果显示在文本区域中。这是我希望结果出现的文本区域所在的框架的编码->
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#它太长了,所以我不能在这里发布。