Using Configuration file for JDBC example
In previous blog, we have written on how to connect to DB using JDBC,
but in that example, we have hard-coded our DB setting in the variables in the code, which could be very inconvenient in the future development,
SRE实战 互联网时代守护先锋,助力企业售后服务体系运筹帷幄!一键直达领取阿里云限量特价优惠。what if the user is using DB2 or SQL Server or other database instead of MySQL?
in that way , we have to change the code each time upon the change of the DB configuration.
Actually, we could use Configuaration files to define the parameter values for our DB,
and in that way, we don't need to change our code whenever there is change in the setting files.
As could see, we have created an instance of ConfigManager,
and the values are fetched through the file "database.properties".
ConfigManager m = new ConfigManager(); JDBC_Driver = m.getString("jdbc.driver"); url = m.getString("jdbc.connection.url"); UserName = m.getString("jdbc.connection.username"); Password = m.getString("jdbc.connection.password");
the running result output is as expected.
1. database.properties file.
jdbc.driver = com.mysql.jdbc.Driver jdbc.connection.url = jdbc:mysql://localhost:3306/sakila?useUnicode=true&characterEncoding=utf8 jdbc.connection.username = root jdbc.connection.password = 3792354
2. ConfigManager.class
package src.util; import java.io.IOException; import java.io.InputStream; import java.util.Properties; /*this class is used to read the database.properties file, * and get the relevant database information*/ public class ConfigManager { private Properties properties = new Properties(); public ConfigManager(){ String configFile = "database.properties"; InputStream in = ConfigManager.class.getClassLoader().getResourceAsStream(configFile); try { properties.load(in); in.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } public String getString(String key) { return properties.getProperty(key); } }
3. Main.class
//Step 1, import the needed packages import java.io.IOException; import java.io.InputStream; import java.sql.*; import java.util.Properties; import src.util.ConfigManager; /*USE mysql; ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY '831015'; FLUSH PRIVILEGES; */ public class Mysql { // JDBC driver name and database URL static String JDBC_Driver = null; static String url = null; static String UserName = null; static String Password = null; static final String SQL = "select * from actor"; public static void main(String[] args) throws IOException { ConfigManager m = new ConfigManager(); JDBC_Driver = m.getString("jdbc.driver"); url = m.getString("jdbc.connection.url"); UserName = m.getString("jdbc.connection.username"); Password = m.getString("jdbc.connection.password"); Connection conn = null; Statement stmt = null; try { System.out.println("Connecting to Database..."); Class.forName(JDBC_Driver); conn = DriverManager.getConnection(url, UserName, Password); System.out.println("Connected to Databse..."); System.out.println("Creating Statement..."); stmt = conn.createStatement(); System.out.println("Executing the Query..."); ResultSet rs = stmt.executeQuery(SQL); System.out.println("fetching the result..."); while(rs.next()) { int id = rs.getInt("actor_id"); String name = rs.getString("first_name")+ " "+ rs.getString("last_name"); System.out.println(id + " "+ name); } } catch(SQLException se){ //Handle errors for JDBC se.printStackTrace(); }catch(Exception e){ //Handle errors for Class.forName e.printStackTrace(); }finally{ //finally block used to close resources try{ if(stmt!=null) stmt.close(); }catch(SQLException se2){ }// nothing we can do try{ if(conn!=null) conn.close(); }catch(SQLException se){ se.printStackTrace(); }//end finally try }//end try } }
