Java tutorial
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package SeedGenerator; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileWriter; import java.sql.DriverManager; import java.sql.Connection; import javax.swing.JOptionPane; import org.apache.commons.io.IOUtils; /** * * @author Semih */ public class FormDatabaseConfig extends javax.swing.JFrame { /** * Creates new form FormDatabaseConfig */ private String connectionString; public FormDatabaseConfig() { initComponents(); String fileString; try { FileInputStream inputStream = new FileInputStream("db.conf"); fileString = IOUtils.toString(inputStream); for (String s : fileString.split("\n")) { try { String label = s.split(" ")[0]; String text = s.split(" ")[1]; switch (label) { case "Hostname": txtHostName.setText(text); break; case "Port": txtPort.setText(text); break; case "Username": txtUsername.setText(text); break; case "Password": txtPassword.setText(text); break; case "Schema": txtSchema.setText(text); break; } } catch (Exception ex) { } } connectionString = "jdbc:mysql://" + txtHostName.getText() + ":" + txtPort.getText() + "/" + txtSchema.getText() + "?user" + txtUsername.getText() + "&password=" + txtPassword.getText(); inputStream.close(); } catch (Exception ex) { } finally { } } /** * 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">//GEN-BEGIN:initComponents private void initComponents() { btnSave = new javax.swing.JButton(); btnTest = new javax.swing.JButton(); txtHostName = new javax.swing.JTextField(); txtUsername = new javax.swing.JTextField(); jLabel1 = new javax.swing.JLabel(); jLabel2 = new javax.swing.JLabel(); jLabel3 = new javax.swing.JLabel(); jLabel4 = new javax.swing.JLabel(); txtPort = new javax.swing.JTextField(); jLabel5 = new javax.swing.JLabel(); txtPassword = new javax.swing.JTextField(); txtSchema = new javax.swing.JTextField(); btnSave.setText("Save"); btnSave.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { btnSaveActionPerformed(evt); } }); btnTest.setText("Test"); btnTest.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { btnTestActionPerformed(evt); } }); txtHostName.setText("localhost"); jLabel1.setText("Hostname"); jLabel2.setText("Username"); jLabel3.setText("Password"); jLabel4.setText("Schema"); txtPort.setText("3306"); jLabel5.setText("Port"); javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); getContentPane().setLayout(layout); layout.setHorizontalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup().addContainerGap() .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(jLabel1).addComponent(jLabel2).addComponent(jLabel3) .addComponent(jLabel4)) .addGap(19, 19, 19) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false) .addComponent(txtSchema) .addComponent(txtHostName, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 121, Short.MAX_VALUE) .addComponent(txtUsername, javax.swing.GroupLayout.Alignment.LEADING) .addComponent(txtPassword, javax.swing.GroupLayout.Alignment.LEADING)) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(jLabel5).addGap(18, 18, 18).addComponent(txtPort, javax.swing.GroupLayout.PREFERRED_SIZE, 63, javax.swing.GroupLayout.PREFERRED_SIZE)) .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup().addGap(20, 20, 20).addComponent(btnTest) .addPreferredGap( javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addComponent(btnSave))) .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))); layout.setVerticalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup().addContainerGap().addGroup(layout .createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(txtHostName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(jLabel1) .addComponent(txtPort, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(jLabel5)).addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(txtUsername, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(jLabel2)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jLabel3).addComponent(txtPassword, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(jLabel4) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(btnSave).addComponent(btnTest)) .addComponent(txtSchema, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))); pack(); }// </editor-fold>//GEN-END:initComponents private void btnTestActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnTestActionPerformed connectionString = "jdbc:mysql://" + txtHostName.getText() + ":" + txtPort.getText() + "/" + txtSchema.getText() + "?user=" + txtUsername.getText() + "&password=" + txtPassword.getText(); try { Class.forName("com.mysql.jdbc.Driver");//.newInstance(); Connection con = DriverManager.getConnection(connectionString); con.createStatement(); JOptionPane.showMessageDialog(this, "OK"); } catch (Exception ex) { JOptionPane.showMessageDialog(this, "Fail"); } // TODO add your handling code here: }//GEN-LAST:event_btnTestActionPerformed private void btnSaveActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSaveActionPerformed try { File newTextFile = new File("db.conf"); FileWriter fw = new FileWriter(newTextFile); fw.write("Hostname " + txtHostName.getText() + "\n"); fw.append("Port " + txtPort.getText() + "\n"); fw.append("Username " + txtUsername.getText() + "\n"); fw.append("Password " + txtPassword.getText() + "\n"); fw.write("Schema " + txtSchema.getText() + "\n"); fw.close(); JOptionPane.showMessageDialog(this, "Saved!"); } catch (Exception ex) { JOptionPane.showMessageDialog(this, "Error, not saved!"); } // TODO add your handling code here: }//GEN-LAST:event_btnSaveActionPerformed /** * @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(FormDatabaseConfig.class.getName()) .log(java.util.logging.Level.SEVERE, null, ex); } catch (InstantiationException ex) { java.util.logging.Logger.getLogger(FormDatabaseConfig.class.getName()) .log(java.util.logging.Level.SEVERE, null, ex); } catch (IllegalAccessException ex) { java.util.logging.Logger.getLogger(FormDatabaseConfig.class.getName()) .log(java.util.logging.Level.SEVERE, null, ex); } catch (javax.swing.UnsupportedLookAndFeelException ex) { java.util.logging.Logger.getLogger(FormDatabaseConfig.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 FormDatabaseConfig().setVisible(true); } }); } // Variables declaration - do not modify//GEN-BEGIN:variables private javax.swing.JButton btnSave; private javax.swing.JButton btnTest; private javax.swing.JLabel jLabel1; private javax.swing.JLabel jLabel2; private javax.swing.JLabel jLabel3; private javax.swing.JLabel jLabel4; private javax.swing.JLabel jLabel5; private javax.swing.JTextField txtHostName; private javax.swing.JTextField txtPassword; private javax.swing.JTextField txtPort; private javax.swing.JTextField txtSchema; private javax.swing.JTextField txtUsername; // End of variables declaration//GEN-END:variables }