我使用Java制作类似MySql控制台的东西。
我有两个JtextField。一个用于主命令,另一个用于命令参数。
我试图创建一个命令select
,它将从给定的SQL表中选择给定的数据并将其打印在JTextArea中。
我已经这样做了,但对于每张桌子,我都要写一个public void
。
我的问题是,我可以让虚空从JtextField获取第二个单词。
例如:如果我有命令select test
,我希望publc void
执行TheSQL命令:SELECT * FROM test
。但是如果我用其他东西替换test
,我想要getText()但是在空格之后,所以它必须在select之后得到文本。
JTextDield2
不可用,因为我必须输入字符串进行打印。
我的代码:
panel.java
package test;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.sql.*;
@SuppressWarnings("serial")
public class panel extends JPanel implements ActionListener {
protected JTextField textField, textField2;
protected static JTextArea textArea;
private final static String newline = "\n";
public panel() {
super(new GridBagLayout());
textField = new JTextField(30);
textField.addActionListener(this);
textField.setBackground(Color.LIGHT_GRAY);
textField2 = new JTextField(30);
textField2.addActionListener(this);
textField2.setEnabled(false);
textArea = new MyTextArea(30, 100);
textArea.setEditable(false);
// textArea.setBackground(new Color(1,1,1, (float) 0.01));
JScrollPane scrollPane = new JScrollPane(textArea);
//Add Components to this panel.
GridBagConstraints c = new GridBagConstraints();
c.gridwidth = GridBagConstraints.REMAINDER;
c.fill = GridBagConstraints.HORIZONTAL;
add(textField, c);
add(textField2, c);
c.fill = GridBagConstraints.BOTH;
c.weightx = 1.0;
c.weighty = 1.0;
add(scrollPane, c);
}
public void actionPerformed(ActionEvent evt) {
if(textField.getText().equalsIgnoreCase("connect")){
textArea.setForeground(Color.BLACK);
textField.setText("");
}else if(textField.getText().equalsIgnoreCase("select test")){
textArea.setForeground(Color.BLACK);
viewTest(conn);
textField.setText("");;
}else if(textField.getText().equalsIgnoreCase("clear")){
textArea.setForeground(Color.BLACK);
textField.setText("");
clear();
}else if(textField.getText().equalsIgnoreCase("commands")){
textArea.setForeground(Color.BLACK);
commandsmenu();
textField.setText("");
}else if(textField.getText().equalsIgnoreCase("insertinto")){
textField2.setEnabled(true);
if(textField2.getText().isEmpty()){
textArea.append("Please add the VALUES of the table on the second textfield! Syntax: 'Agevaulue', namevalue, adressvalue !" + newline);
}else{
textArea.setForeground(Color.BLACK);
InsertInto(conn);
textField2.setText("");
textField2.setEnabled(false);
}
}else if(textField.getText().equalsIgnoreCase("createtable")){
if(textField2.getText().isEmpty()){
textArea.append("Please add the Command parameters on the Secont textField!" + newline + "SYNTAX: tablename(c, c, c)VALUES('num', ''text''");
textField2.setEnabled(true);
}else {
textArea.setForeground(Color.BLACK);
CreateTable(conn);
textField2.setEnabled(false);
textField2.setText("");
}
}else if(textField.getText().equalsIgnoreCase("1")){
textField.setVisible(true);
}
else {
clear();
textArea.setForeground(Color.RED);
textArea.append("Uknown Command -- use: commands -- to see all commands!" + newline);
textField.selectAll();
}
//Make sure the new text is visible, even if there
//was a selection in the text area.
textArea.setCaretPosition(textArea.getDocument().getLength());
}
/**
* Create the GUI and show it. For thread safety,
* this method should be invoked from the
* event dispatch thread.
*/
private static void createAndShowGUI() {
//Create and set up the window.
JFrame frame = new JFrame("Java + MySQL Example");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//Add contents to the window.
frame.add(new panel());
//Display the window.
frame.pack();
frame.setVisible(true);
}
public Connection conn;{
try
{
String url = "jdbc:mysql://sql5.freemysqlhosting.net/sql555081";
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection(url, "sql555081", "yT6%qQ7%");
}
catch (Exception e){
e.printStackTrace();
}
}
public void viewTest(Connection conn)
{
try{
String query = "SELECT * FROM test";
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery(query);
while (rs.next()){
int age = rs.getInt("age");
String name = rs.getString("name");
String adress = rs.getString("adress");
textArea.append("AGE: " + age + " |Name: " + name + " |Adress: " + adress + newline);
}
}catch(Exception e){
textArea.setForeground(Color.RED);
textArea.append("Got an Exception!" + newline);
textArea.append(e.getMessage());
}
}public void InsertInto(Connection conn) {
try{
Statement stmt = conn.createStatement();
stmt.executeUpdate("INSERT INTO " + textField2.getText());
textArea.append("Data Imported!" + newline);
}catch(Exception e){
textArea.setForeground(Color.RED);
textArea.append("Got an Exception" + newline);
textArea.append(e.getMessage());
}
}public void CreateTable(Connection conn) {
try{
Statement stmt = conn.createStatement();
stmt.executeUpdate("CREATE TABLE" + textField2.getText());
textArea.append( newline + "Table created!" + newline);
}catch(Exception e){
textArea.setForeground(Color.RED);
textArea.append( newline + "Got an Exception" + newline);
textArea.append(e.getMessage());
}
}
public void clear(){
textArea.setText("");
}
public void commandsmenu() {
textArea.append("select <table> - read a <table>" + newline);
textArea.append("clear - clear output" + newline);
textArea.append("commands - see Commands" + newline);
textArea.append("insertinto <table> - insert data into <table>" + newline);
textArea.append(newline);
}
public static void main(String[] args) {
//Schedule a job for the event dispatch thread:
//creating and showing this application's GUI.
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
}
MyTextArea.java
package test;
import java.awt.Graphics;
import java.awt.Image;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.JTextArea;
@SuppressWarnings("serial")
public class MyTextArea extends JTextArea {
private Image img;
public MyTextArea(int a, int b) {
super(a,b);
try{
img = ImageIO.read(new File("background.jpg"));
} catch(IOException e) {
System.out.println(e.toString());
}
}
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawImage(img,550,200,null);
}
}