>  기사  >  Java  >  구성 파일(코드)을 통해 jdbc가 데이터베이스에 연결하고 데이터를 호출하는 방법을 구현하는 방법

구성 파일(코드)을 통해 jdbc가 데이터베이스에 연결하고 데이터를 호출하는 방법을 구현하는 방법

不言
不言원래의
2018-09-14 16:16:552051검색

이 기사의 내용은 구성 파일(코드)을 통해 데이터베이스에 연결하고 데이터를 호출하는 방법에 대한 것입니다. 필요한 친구가 참고할 수 있기를 바랍니다. 너.

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;
    }
}

구성 파일 db.properties

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

관련 권장 사항:

Java JDBC는 구성 파일을 사용하여 데이터베이스

mysql-Java 코드를 전달하는 방법 데이터베이스 연결을 구현합니다

위 내용은 구성 파일(코드)을 통해 jdbc가 데이터베이스에 연결하고 데이터를 호출하는 방법을 구현하는 방법의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!

성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.