300字范文,内容丰富有趣,生活中的好帮手!
300字范文 > 如何实现jdbc连接数据库并通过配置文件调用数据办法(code)

如何实现jdbc连接数据库并通过配置文件调用数据办法(code)

时间:2023-05-02 11:44:49

相关推荐

如何实现jdbc连接数据库并通过配置文件调用数据办法(code)

Java|java教程

jdbc

Java-java教程

本篇文章给大家带来的内容是关于如何实现jdbc连接数据库并通过配置文件调用数据的方法(代码) ,有一定的参考价值,有需要的朋友可以参考一下,希望对你有所帮助。

select() 源码,vscode连接模拟器输出,ubuntu 图标异常,启动了多个tomcat,爬虫三兄弟,php语言 知乎,海珠区网络seo优化资费lzw

Dbutil类

企业网站源码带个人用户,ubuntu设备管理,爬虫箱线路坏了,php视频文件怎么转mp4,博客和seolzw

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

温州二人麻将 6603源码,vscode c 知乎,ubuntu安装lib,tomcat中文乱码设置,注入爬虫,mvc php 案例,长沙seo外包优化报价,个人网站源码带手机端,微信论坛模板免费下载lzw

driver=com.mysql.cj.jdbc.Driverhost=localhostpost=3306db=studentuser=rootpsd=123456

相关推荐:

Java JDBC使用配置文件连接数据库

mysql-怎么通过java代码实现对数据库的连接

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。