jdbc连接数据库,通过配置文件调用
2018-09-18 06:36:31来源:博客园 阅读 ()
Dbutil类
package com.db; import java.io.IOException; import java.io.InputStream; import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException; import java.util.Properties; public class Dbutil { private String driver = "com.mysql.cj.jdbc.Driver"; private String host = "localhost"; private String post = "3306"; private String db = "student"; private String user = "root"; private String psd = "123456"; private String url = ""; private Connection con = null; public Dbutil() { Properties pro = new Properties(); InputStream in = Dbutil.class.getResourceAsStream("db.properties"); try { pro.load(in); this.driver =pro.getProperty("driver"); this.host= pro.getProperty("host"); this.post=pro.getProperty("post"); this.db=pro.getProperty("db"); this.user=pro.getProperty("user"); this.psd=pro.getProperty("psd"); this.url = "jdbc:mysql://" + host + ":" + post + "/" + db + "?serverTimezone=PRC&useSSL=false"; Class.forName(driver); this.con=DriverManager.getConnection(url,user,psd); } catch (IOException e) { e.printStackTrace(); } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (SQLException e) { e.printStackTrace(); } } public Dbutil(String host, String post, String db, String user, String psd){ this.host = host; this.post = post; this.db = db; this.user = user; this.psd = psd; this.url = "jdbc:mysql://" + host + ":" + post + "/" + db + "?serverTimezone=PRC&useSSL=false"; try { Class.forName(driver); this.con=DriverManager.getConnection(url,user,psd); } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (SQLException e) { e.printStackTrace(); } } public String getDriver() { return driver; } public void setDriver(String driver) { this.driver = driver; } public String getHost() { return host; } public void setHost(String host) { this.host = host; } public String getPost() { return post; } public void setPost(String post) { this.post = post; } public String getDb() { return db; } public void setDb(String db) { this.db = db; } public String getUser() { return user; } public void setUser(String user) { this.user = user; } public String getPsd() { return psd; } public void setPsd(String psd) { this.psd = psd; } public String getUrl() { return url; } public void setUrl(String url) { this.url = url; } public Connection getCon() { return con; } public void setCon(Connection con) { this.con = con; } }
配置文件 db.properties
driver=com.mysql.cj.jdbc.Driver host=localhost post=3306 db=student user=root psd=123456
标签:
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有
上一篇:视频截图Util
- SpringBoot 2.3 整合最新版 ShardingJdbc + Druid + MyBatis 2020-06-11
- JSP+Structs+JDBC+mysql实现的诚欣电子商城 2020-06-08
- Spring11_JdbcTemplate 2020-06-07
- 拼多多三面惨败,问题在于java中间件、数据库与spring框架. 2020-06-04
- Mybatis 的连接池技术 2020-06-01
IDC资讯: 主机资讯 注册资讯 托管资讯 vps资讯 网站建设
网站运营: 建站经验 策划盈利 搜索优化 网站推广 免费资源
网络编程: Asp.Net编程 Asp编程 Php编程 Xml编程 Access Mssql Mysql 其它
服务器技术: Web服务器 Ftp服务器 Mail服务器 Dns服务器 安全防护
软件技巧: 其它软件 Word Excel Powerpoint Ghost Vista QQ空间 QQ FlashGet 迅雷
网页制作: FrontPages Dreamweaver Javascript css photoshop fireworks Flash