我试图想出一个简单的工具,它将数据库参数作为用户在JTextFields中的输入,当用户点击连接到数据库时,它使用输入连接到数据库。 UI部分编写在主类中的一个函数中,从该函数调用DBConnector类方法并通过ActionListener传递输入文本参数。我似乎无法使ActionListener部分工作。这是下面的主要类代码:
public class ExcelReportGenerator implements DocumentListener{
//MAIN METHOD
public static void main(String[] args) {
try {
createAndShowUI();
} catch (Exception e) {
e.printStackTrace();
}
}
public ExcelReportGenerator(JButton button1, JButton button2)
{
button1=this.button1;
button2=this.button2;
}
private JButton button1 ;
private JButton button2;
private List<JTextField> textFields = new ArrayList<JTextField>();
//public void DBCaller()
public void addTextField(JTextField textField)
{
textFields.add( textField );
textField.getDocument().addDocumentListener( this );
}
public boolean isDataEntered()
{
for (JTextField textField : textFields)
{
if (textField.getText().trim().length() == 0)
return false;
}
return true;
}
@Override
public void insertUpdate(DocumentEvent e)
{
checkData();
}
@Override
public void removeUpdate(DocumentEvent e)
{
checkData();
}
@Override
public void changedUpdate(DocumentEvent e) {}
private void checkData()
{
button1.setEnabled( isDataEntered() );
button2.setEnabled( isDataEntered() );
}
private static void createAndShowUI() throws RowsExceededException, ClassNotFoundException, WriteException, SQLException, IOException
{
//CREATING BUTTONS
JButton submit = new JButton( "Connect To DB" );
submit.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
DBConnector.DBConnection(textField_1.getText(),textField_2.getText(),
textField_3.getText(),textField_4.getText(),textField_5.getText());
//NEED TO ACCESS THE DBCONNECTOR CLASS METHOD HERE
}
});
submit.setEnabled(false);
submit.setBackground(UIManager.getColor("Button.disabledShadow"));
submit.setForeground(new Color(0, 0, 128));
submit.setBounds(282, 77, 192, 36);
JButton btnExportToExcel = new JButton("Export To Excel");
btnExportToExcel.setEnabled(false);
btnExportToExcel.setForeground(new Color(0, 0, 128));
btnExportToExcel.setBackground(UIManager.getColor("Button.background"));
submit.setBackground(UIManager.getColor("Button.disabledShadow"));
btnExportToExcel.setBounds(282, 178, 192, 36);
//DECLARING TEXTFIELDS
JTextField textField_1 = new JTextField();
textField_1.setBackground(new Color(255, 255, 224));
JTextField textField_2 = new JTextField();
textField_2.setBackground(new Color(255, 255, 224));
JTextField textField_3 = new JTextField();
textField_3.setBackground(new Color(255, 255, 224));
JTextField textField_4 = new JTextField();
textField_4.setBackground(new Color(255, 255, 224));
JTextField textField_5 = new JTextField();
textField_5.setBackground(new Color(255, 255, 224));
JTextField textField_6 = new JTextField();
textField_6.setBackground(new Color(255, 255, 224));
JTextField textField_7 = new JTextField();
textField_7.setBackground(new Color(255, 255, 224));
//ADDING TEXTFIELDS TO ARRAY THROUGH CONSTRUCTOR
ExcelReportGenerator de = new ExcelReportGenerator( submit , btnExportToExcel);
de.addTextField( textField_1 );
de.addTextField( textField_2 );
de.addTextField( textField_3 );
de.addTextField( textField_4 );
de.addTextField( textField_5 );
de.addTextField( textField_6 );
de.addTextField( textField_7 );
//CREATING THE FRAME
JFrame frame = new JFrame("Report Maker");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setPreferredSize(new Dimension(500,335));
frame.getContentPane().setLayout(null);
frame.getContentPane().add(submit);
frame.getContentPane().add(btnExportToExcel);
//ALL TEXTFIELDS
textField_1.setBounds(112, 62, 117, 20);
frame.getContentPane().add(textField_1);
textField_1.setColumns(10);
textField_2.setBounds(112, 93, 117, 20);
frame.getContentPane().add(textField_2);
textField_2.setColumns(10);
textField_3.setBounds(112, 124, 117, 20);
frame.getContentPane().add(textField_3);
textField_3.setColumns(10);
textField_4.setBounds(112, 155, 117, 20);
frame.getContentPane().add(textField_4);
textField_4.setColumns(10);
textField_5.setBounds(112, 186, 117, 20);
frame.getContentPane().add(textField_5);
textField_5.setColumns(10);
textField_6.setBounds(112, 219, 117, 20);
frame.getContentPane().add(textField_6);
textField_6.setColumns(10);
textField_7.setBounds(112, 250, 117, 20);
frame.getContentPane().add(textField_7);
textField_7.setColumns(10);
//DBConnector.DBConnection(username,password,hostname,port,sid,tablename,filename);
//ALL LABELS
JLabel lblDatabaseToExcel = new JLabel("Database To Excel Tool");
lblDatabaseToExcel.setForeground(new Color(0, 0, 128));
lblDatabaseToExcel.setFont(new Font("Georgia", Font.BOLD, 20));
lblDatabaseToExcel.setBounds(123, -11, 351, 50);
frame.getContentPane().add(lblDatabaseToExcel);
JLabel lblUsername = new JLabel("Username:");
lblUsername.setFont(new Font("Tahoma", Font.PLAIN, 10));
lblUsername.setForeground(new Color(0, 0, 128));
lblUsername.setBounds(20, 44, 200, 50);
frame.getContentPane().add(lblUsername);
JLabel lblPassword = new JLabel("Password:");
lblPassword.setFont(new Font("Tahoma", Font.PLAIN, 10));
lblPassword.setForeground(new Color(0, 0, 128));
lblPassword.setBounds(20, 78, 200, 50);
frame.getContentPane().add(lblPassword);
JLabel lblHostname = new JLabel("Hostname:");
lblHostname.setFont(new Font("Tahoma", Font.PLAIN, 10));
lblHostname.setForeground(new Color(0, 0, 128));
lblHostname.setBounds(20, 109, 200, 50);
frame.getContentPane().add(lblHostname);
JLabel lblPort = new JLabel("Port:");
lblPort.setFont(new Font("Tahoma", Font.PLAIN, 10));
lblPort.setForeground(new Color(0, 0, 128));
lblPort.setBounds(20, 140, 200, 50);
frame.getContentPane().add(lblPort);
JLabel lblSid = new JLabel("SID:");
lblSid.setFont(new Font("Tahoma", Font.PLAIN, 10));
lblSid.setForeground(new Color(0, 0, 128));
lblSid.setBounds(20, 171, 200, 50);
frame.getContentPane().add(lblSid);
JLabel lblEnterDbDetails = new JLabel("DB Details:");
lblEnterDbDetails.setForeground(new Color(0, 0, 128));
lblEnterDbDetails.setFont(new Font("Vrinda", Font.BOLD, 15));
lblEnterDbDetails.setBounds(20, 11, 200, 50);
frame.getContentPane().add(lblEnterDbDetails);
JLabel lblTableviewName = new JLabel("Table/View Name:");
lblTableviewName.setFont(new Font("Tahoma", Font.PLAIN, 10));
lblTableviewName.setForeground(new Color(0, 0, 128));
lblTableviewName.setBackground(new Color(255, 228, 225));
lblTableviewName.setBounds(20, 203, 200, 50);
frame.getContentPane().add(lblTableviewName);
JLabel lblEnterNameOf = new JLabel("Enter Name Of File:");
lblEnterNameOf.setForeground(new Color(0, 0, 128));
lblEnterNameOf.setFont(new Font("Tahoma", Font.PLAIN, 10));
lblEnterNameOf.setBounds(20, 235, 200, 50);
frame.getContentPane().add(lblEnterNameOf);
JLabel lblgeneratedFileLocated = new JLabel("(Generated File Located At C:\\)");
lblgeneratedFileLocated.setFont(new Font("Tahoma", Font.PLAIN, 11));
lblgeneratedFileLocated.setForeground(new Color(205, 92, 92));
lblgeneratedFileLocated.setBounds(305, 204, 200, 50);
frame.getContentPane().add(lblgeneratedFileLocated);
frame.pack();
frame.setLocationByPlatform( true );
frame.setVisible( true );
}
}
以下是DBConnector类代码:
public class DBConnector implements ActionListener
{
public static void DBConnection(String user,String pword, String host, String portnum, String sidValue, String table, String generatedfile) throws SQLException, ClassNotFoundException, RowsExceededException, WriteException, IOException
{
String username=user;
String password=pword;
String hostname=host;
String port=portnum;
String sid=sidValue;
String tablename=table;
String filename=generatedfile;
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
} catch (ClassNotFoundException e) {
System.out.println("Where is the fricking JDBC Driver?");
e.printStackTrace();
return;
}
System.out.println("Oracle JDBC Driver Found!");
Connection con;
try {
Properties props=new Properties();
props.put("user",username);
props.put("password",password);
props.setProperty("hostname",hostname);
props.setProperty("port",port);
props.setProperty("sid",sid);
String jdbcurl="jdbc:oracle:oci:@";
System.out.println();
con = DriverManager
.getConnection(jdbcurl,props);
}
catch (SQLException e)
{
JOptionPane.showMessageDialog(null, " DB Connection Failed,\n Make Sure All Details Entered Are Correct");
System.out.println("Connection Failed! Check output console");
e.printStackTrace();
return;
}
if (con != null)
{
JOptionPane.showMessageDialog(null, "Connected to Database\nClick on the Export To Excel Button");
System.out.println("You made it, take control your database now!");
ReportMaker.reportParameters(tablename,filename, con);
}
else
{
JOptionPane.showMessageDialog(null, " DB Connection Failed,\n Make Sure All Details Entered Are Correct");
}
}
}