1 package net.sf.jasperreports.mysql; 2 3 import java.sql.Connection ; 4 import java.sql.DriverManager ; 5 6 10 public class Database 11 { 12 13 private Connection connection; 14 String name = null; 16 String login = null; 17 String pass = null; 18 String url = null; 19 20 25 public Connection getConnection() 26 { 27 if (connection == null) 29 { 30 connection = connect(url, login, pass); 31 } 32 return connection; 33 } 34 35 44 private static Connection connect(String dbURL, String dbLogin, 45 String dbPass) 46 { 47 Connection con = null; 48 try 49 { 50 Class.forName("com.mysql.jdbc.Driver"); 52 con = DriverManager.getConnection(dbURL, dbLogin, dbPass); 54 } 55 catch (Exception e) 57 { 58 e.printStackTrace(); 59 System.out.println("Exception: " + e.getMessage()); 60 if (e.getMessage().indexOf("Unknown database") != -1) 62 { 63 System.err.println( 64 "\n\nI can't seem to find the database. Please create it"); 65 } 66 else 68 { 69 System.out.println( 70 "\n\nI can't connect to the database.\n" 71 + 72 "It is likely that the username or password was incorrect."); 73 } 74 System.exit(1); 75 } 76 System.err.println( 77 "Connected to database (" + dbURL + ") successfully!"); 78 return con; 79 } 80 } 81 | Popular Tags |