Home > Java > javaTutorial > body text

How to implement the method of jdbc connecting to the database and calling data through the configuration file (code)

不言
Release: 2018-09-14 16:16:55
Original
2099 people have browsed it

The content of this article is about how to implement jdbc to connect to the database and call the data through the configuration file (code). It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you. help.

Dbutil class

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;
    }
}
Copy after login

Configuration file db.properties

driver=com.mysql.cj.jdbc.Driver
host=localhost
post=3306db=student
user=root
psd=123456
Copy after login

Related recommendations :

Java JDBC uses configuration files to connect to the database

mysql-How to connect to the database through java code

The above is the detailed content of How to implement the method of jdbc connecting to the database and calling data through the configuration file (code). For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template