时间:2021-07-01 10:21:17 帮助过:28人阅读
在公司里,开发系统往往使用开发环境,真实环境只有在系统开发好了之后,才可以上线使用,那么数据库连接如何配置呢?
答案是,使用.properties文件配置。直接上代码:
DBConfig.properties:
#development environment server_oa = *** port_oa = *** user_oa = *** pass_oa = *** dbname_oa = <span style="font-family: Arial, Helvetica, sans-serif;">oadb</span> #real environment #server_oa = <span style="font-family: Arial, Helvetica, sans-serif;">***</span> #port_oa = <span style="font-family: Arial, Helvetica, sans-serif;">***</span> #user_oa = <span style="font-family: Arial, Helvetica, sans-serif;">***</span> #pass_oa = <span style="font-family: Arial, Helvetica, sans-serif;">***</span> #dbname_oa = oadb
在DB类中:
public class DBConnection { public static String server_oa; public static String port_oa; public static String user_oa; public static String pass_oa; public static String dbname_oa; public static String url_oa; static{ try { Properties properties = new Properties(); InputStream inStream = Thread.currentThread().getContextClassLoader().getResourceAsStream("DBConfig.properties"); properties.load(inStream); server_oa = properties.getProperty("server_oa"); port_oa = properties.getProperty("port_oa"); user_oa = properties.getProperty("user_oa"); pass_oa = properties.getProperty("pass_oa"); dbname_oa = properties.getProperty("dbname_oa"); url_oa = "jdbc:mysql://" + server_oa + ":" + port_oa + "/" + dbname_oa + "?user=" + user_oa + "&password=" + pass_oa; } catch (IOException e) { e.printStackTrace(); } } }
这样,你只要正常发布自己的系统,环境变了,配置一下DBConfig.properties文件即可。。。。
版权声明:本文为博主原创文章,未经博主允许不得转载。
去哪网实习总结:怎样配置数据库连接(JavaWeb)
标签:java web 数据库 配置数据库连接