当前位置:Gxlcms > 数据库问题 > Java 用jdbc连接数据库

Java 用jdbc连接数据库

时间:2021-07-01 10:21:17 帮助过:23人阅读

password=1230

driverClass=com.mysql.jdbc.Driver

jdbcUrl=jdbc:mysql:///atguigu

 

 

  1. 代码

 

import java.io.IOException;

import java.io.InputStream;

import java.util.Properties;

import java.sql.DriverManager;

import java.sql.SQLException;

 

import com.mysql.jdbc.Connection;

 

public class JDBCTools {

 

    public JDBCTools() {

        

    }

 

    public static Connection getConnection() throws IOException, SQLException {

        Connection conn = null;

        Properties properties = new    Properties();

        InputStream inStream = JDBCTools.class.getClassLoader()

                .getResourceAsStream("jdbc.properties");

        properties.load(inStream);

 

        String user = properties.getProperty("user");

        String password = properties.getProperty("password");

        String driverClass = properties.getProperty("driverClass");

        String jdbcUrl = properties.getProperty("jdbcUrl");

        

        conn = (Connection) DriverManager.getConnection(jdbcUrl,user,password);

        

        return conn;

        

    }

    public static void main(String[] args) throws IOException, SQLException {

        System.out.println(getConnection());

    }

}

Java 用jdbc连接数据库

标签:连接数   imp   java   步骤   system   ssl   连接数据库   配置   string   

人气教程排行