KnowledgeHub
Questions
Tags
Users
Search
Alex Rivera
|
Logout
Edit Question
Title
Body
hey all, I'm new to Java and was wondering if I define a method to return a database object like import java.sql.*; public class DbConn { public Connection getConn() { Connection conn; try { Class.forName("com.mysql.jdbc.Driver").newInstance(); if(System.getenv("MY_ENVIRONMENT") == "development") { String hostname = "localhost"; String username = "root"; String password = "root"; } conn = DriverManager.getConnection("jdbc:mysql:///mydb", username, password); return conn; } catch(Exception e) { throw new Exception(e.getMessage()); } } } if the connection fails when I try to create it what should I return? eclipse is telling me I have to return a Connection object but if it fails I'm not sure what to do. thanks! UPDATED CODE TO LET EXCEPTION BUBBLE: public class DbConn { public Connection getConn() throws SQLException { Connection conn; String hostname = "localhost"; String username = "root"; String password = "root"; Class.forName("com.mysql.jdbc.Driver").newInstance(); if(System.getenv("MY_ENVIRONMENT") != "development") { hostname = "localhost"; username = "produser"; password = "prodpass"; } conn = DriverManager.getConnection("jdbc:mysql:///mydb", username, password); return conn; } }
Tags (comma-separated)
Save Edits
Cancel