码迷,mamicode.com
首页 > 数据库 > 详细

Java 用jdbc连接数据库

时间:2017-02-09 22:05:01      阅读:247      评论:0      收藏:0      [点我收藏+]

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

Java 用jdbc连接数据库

准备

安装mysql

下载 mysql-connector-java-5.1.40-bin.jar驱动包

步骤

  1. 创建jdbc项目

  2. 导入mysql-connector-java-5.1.40-bin.ja包

  3. 创建配置文件

jdbc.properties

user=root

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   

原文地址:http://www.cnblogs.com/qq441540598/p/6383961.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!