Ho unito le due classi per poter avere tutto in un unica classe:
Rispetto a prima ora c'è anche il metodo private void jButton1ActionPerformed dove salvo i dati nel file config.propertiescodice:import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.util.Properties; import javax.swing.JOptionPane; import java.sql.*; import javax.swing.*; public class DbConnes extends javax.swing.JFrame { Connection conn = null; public DbConnes() { initComponents(); } static String url_database; static String username; static String password; public void OutProp (){ Properties prop = new Properties(); try { prop.load(new FileInputStream("config.properties")); url_database = prop.getProperty("Url"); username = prop.getProperty("Username"); password = prop.getProperty("Password"); } catch (IOException ex) { ex.printStackTrace(); } } public static Connection ConnectDb(){ try{ Class.forName ("com.mysql.jdbc.Driver"); Connection conn = DriverManager.getConnection (url_database,username,password); return conn; } catch (Exception e) { JOptionPane.showMessageDialog(null, e); return null; } } private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) { Properties prop = new Properties(); String value1 = jTextField1.getText(); String value2 = jTextField2.getText(); String value3 = jTextField3.getText(); try { prop.setProperty("Url", value1); prop.setProperty("Username", value2); prop.setProperty("Password", value3); prop.store(new FileOutputStream("config.properties"), null); JOptionPane.showMessageDialog(null, "Salvato"); } catch (IOException ex) { ex.printStackTrace(); } } // Variables declaration - do not modify private javax.swing.JButton jButton1; private javax.swing.JLabel jLabel1; private javax.swing.JLabel jLabel2; private javax.swing.JLabel jLabel3; private javax.swing.JPanel jPanel1; private javax.swing.JTextField jTextField1; private javax.swing.JTextField jTextField2; private javax.swing.JTextField jTextField3; // End of variables declaration }
Riposto quello che è salvato in questo file:
#Fri Oct 05 14:22:41 CEST 2012
Username=gabri
Password=edp
Url=jdbc:mysql://localhost/databaseprogetto
Aggiungo che ieri ho provato anche a inserire in questa classe 3 jtextfiled e a far caricare in quest'ultime i valori e tutto funziona perfettamente.

Rispondi quotando