1 package org.apache.ojb.tools.mapping.reversedb.gui.actions; 2 3 17 18 import javax.swing.JOptionPane ; 19 20 25 public class DBConnectAction extends javax.swing.AbstractAction 26 { 27 private org.apache.ojb.tools.mapping.reversedb.gui.JFrmMainFrame mainFrame; 28 29 public DBConnectAction(org.apache.ojb.tools.mapping.reversedb.gui.JFrmMainFrame pmainFrame) 30 { 31 super(); 32 mainFrame = pmainFrame; 33 } 34 35 public void actionPerformed (java.awt.event.ActionEvent actionEvent) 36 { 37 java.sql.Connection conn = connectToDB(mainFrame.getProperty("JDBCDriver", ""), 38 mainFrame.getProperty("JDBCURL", ""), 39 mainFrame.getProperty("JDBCUsername", ""), 40 mainFrame.getProperty("JDBCPassword", "")) ; 41 if (conn != null) 42 mainFrame.setConnection(conn); 43 else 44 new org.apache.ojb.tools.mapping.reversedb.gui.JDlgDBConnection(mainFrame, false, mainFrame).show(); 45 } 46 47 private java.sql.Connection connectToDB(String strJDBCDriver, String strJDBCURL, 48 String strUsername, String strPassword) 49 { 50 try 51 { 52 Class.forName(strJDBCDriver); java.sql.Connection conn = 54 java.sql.DriverManager.getConnection(strJDBCURL, 55 strUsername, strPassword); return conn; 57 } 58 catch (java.sql.SQLException sqlEx) 59 { 60 java.sql.SQLException currentSqlEx = sqlEx; 61 System.out.println (sqlEx.getErrorCode() + ":" + sqlEx.getMessage()); 62 while (currentSqlEx.getNextException() != null) 63 { 64 currentSqlEx = currentSqlEx.getNextException(); 65 System.out.println (sqlEx.getErrorCode() + ":" + sqlEx.getMessage()); 66 } 67 JOptionPane.showMessageDialog(mainFrame, "Error connecting to database:\n" + sqlEx.getMessage(), "SQL Error", JOptionPane.ERROR_MESSAGE); 68 return null; 69 } 70 catch (java.lang.ClassNotFoundException clNotFoundEx) 71 { 72 clNotFoundEx.printStackTrace(); 73 JOptionPane.showMessageDialog(mainFrame, "Cannot find driver class:\n" + clNotFoundEx.getMessage(), "Class Not Found", JOptionPane.ERROR_MESSAGE); 74 return null; 75 } 76 catch (Throwable t) 77 { 78 t.printStackTrace(); 79 JOptionPane.showMessageDialog(mainFrame, "Unknown error:\n" + t.getMessage(), "Unknown Error", JOptionPane.ERROR_MESSAGE); 80 return null; 81 } 82 } 83 } 84 85 142 143 | Popular Tags |