Java连接Mysql:通过配置文件

1、导入数据库驱动jar包,与及编写数据库配置文件(mysql.properties),mysql.properties放在项目的src目录下[本例使用的数据库为Mysql5.5,数据库驱动jar包为mysql-connector-java-5.0.8-bin.jar]

mysql.properties:

name=com.mysql.jdbc.Driver                    //数据库驱动名称
url=jdbc:mysql://127.0.0.1:3306/dormitory //数据库连接url
user=root                                               //数据库连接用户名
password=123456                                 //数据库连接密码

2、编写mysql.properties读取属性类Mysqlread.java,负责读取mysql.properties中的属性名称

import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.util.Iterator;
import java.util.Properties;
public class Mysqlread {
    public static final String [] message=readurl();
    private static String[] readurl() {
        Properties prop=new Properties();
        String [] message=new String[4];
        int i=0;
        try {
            InputStream in=new BufferedInputStream(new FileInputStream("src/mysql.properties"));
            prop.load(in);
            message[0]=prop.getProperty("name");
            message[1]=prop.getProperty("url");
            message[2]=prop.getProperty("user");
            message[3]=prop.getProperty("password");
            in.close();
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
        return message;
    }
}

3、编写数据库连接类MysqlOperation.java

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class MysqlOperation {
    private static final String[] mysqlmessage=Mysqlread.message;
    public static Connection getConnection()
    {
        Connection conn=null;
        try {
            Class.forName(mysqlmessage[0]);
            conn=DriverManager.getConnection(mysqlmessage[1],mysqlmessage[2],mysqlmessage[3]);
        } catch (ClassNotFoundException e) {
            System.out.println("驱动类库不能发现");
        } catch (SQLException e) {
            System.out.println("SQL异常");
        }
        return conn;
    }
    public static void close(Connection conn){
        if(conn!=null)
        {
            try {
                conn.close();
            } catch (SQLException e) {
                System.out.println("SQL异常");
            }
        }
    }
    public static void close(Statement statement){
        if(statement!=null){
            try {
                statement.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
            statement=null;
        }
    }
    public static void close(ResultSet rs)
    {
        if(rs!=null){
            try {
                rs.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
            rs=null;
        }
    }
}

4、编写一个测试类测试上述代码是否成功

import static org.junit.Assert.*;
import java.sql.Connection;
import org.junit.Test;
import com.mrw.util.MysqlOperation;
public class TestPropertisread {
    @Test
    public void test() {
        Connection conn=MysqlOperation.getConnection();
        System.out.println(conn);
        MysqlOperation.close(conn);
    }
}

运行结果为:


  • 1
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值