Steps of the JDBC Connection database query information (extracted as configuration file mode)

Source: Internet
Author: User

The drawbacks of hard-coded formats: When a database changes, you re-modify the code, recompile, and deploy

Workaround: Write the database information in the configuration file and let the program read the configuration file to obtain this information

jdbc.driver.class=com.mysql.jdbc.driverjdbc.connection.url=jdbc:mysql://127.0.0.1:3306/ treejdbc.connection.username=rootjdbc.connection.password=123321

Writing a configuration file database.properties
Create Package Com.pb.tree.util, write class ConfigManager read properties file

Package cn.tree.util;

Import java.io.IOException;
Import Java.io.InputStream;
Import java.util.Properties;

Tool classes for reading configuration files-Singleton mode
public class ConfigManager {
Read configuration file Properties.load (InputStream)

private static ConfigManager ConfigManager;
private static properties properties;

Private ConfigManager () {
String configfile = "Database.properties";
Properties = new properties ();
InputStream is = ConfigManager.class.getClassLoader ()
. getResourceAsStream (ConfigFile);

try {
Properties.load (IS);
Is.close ();
} catch (IOException e) {
E.printstacktrace ();
}
}

public static ConfigManager getinstance () {
if (ConfigManager = = null) {
ConfigManager = new ConfigManager ();
}
return ConfigManager;
}

public string getString (string key) {
return Properties.getproperty (key);
}

}



Ways to modify query information for the Menudao class

Package Cn.tree.util;import Java.io.ioexception;import Java.io.inputstream;import java.util.properties;// Tool class for reading configuration files-Singleton mode public class ConfigManager {//Read config file properties.load (inputstream) private static ConfigManager Configmanager;private static Properties properties;private ConfigManager () {String configfile = "Database.properties"; Properties = new properties (); InputStream is = ConfigManager.class.getClassLoader (). getResourceAsStream (ConfigFile); try {properties.load (IS); Is.close ();} catch (IOException e) {e.printstacktrace ()}} public static ConfigManager getinstance () {if (ConfigManager = = null) {ConfigManager = new ConfigManager ();} return ConfigManager;} public string getString (string key) {return Properties.getproperty (key);}}

  

Package Cn.tree.dao;import Java.sql.connection;import Java.sql.drivermanager;import java.sql.resultset;import Java.sql.sqlexception;import Java.sql.statement;import Cn.tree.util.configmanager;public class MenuDao {// Query home directory public void Getmainlist () {Connection Connection = null; Statement st = null; ResultSet rs = null; String Driver = Configmanager.getinstance (). getString ("Jdbc.driver.class"); String URL = configmanager.getinstance (). getString ("Jdbc.connection.url"); String username = configmanager.getinstance (). getString ("Jdbc.connection.username"); String password = configmanager.getinstance (). getString ("Jdbc.connection.password"); try {//1, Load drive class.forname ( driver);//2, get database connection connection = drivermanager.getconnection (URL, username, password);//3, Get Statement object, Execute SQL statement String sql = "SELECT * from main"; st = Connection.createstatement (); rs = st.executequery (sql);//4, processing SQL execution result while ( Rs.next ()) {int id = rs.getint ("id"); String name = rs.getstring ("name"); SYSTEM.OUT.PRINTLN ("ID:" + id + "\ T")+ "Name:" + name);}} catch (ClassNotFoundException e) {e.printstacktrace ()} catch (SQLException e) {e.printstacktrace ();} finally {try {//5, Release resources Rs.close (); St.close (); Connection.close ();} catch (SQLException e) {e.printstacktrace ();}}} public static void Main (string[] args) {Menudao Menudao = new Menudao (); Menudao.getmainlist ();}}

  

Steps of the JDBC Connection database query information (extracted as configuration file mode)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.