jdbc读取properties文件连接数据库

jdbc驱动通过properties文件存储数据库相关信息

连接数据库模块

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ResourceBundle;

public class jdbcUtil {
    //获取connection对象
    //关闭connection
    //关闭statement对象
    private static String driver;
    private static String connectionUrl;
    private static String username;
    private  static String psw;

    static {
        //读取propertile
       ResourceBundle  bundle=ResourceBundle.getBundle("jdbc");
        driver=bundle.getString("driver");
        connectionUrl=bundle.getString("connectionUrl");
        username=bundle.getString("username");
        psw=bundle.getString("psw");

        try {
            Class.forName(driver);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }
    public static Connection getConnection(){
        Connection conn=null;
        try {
            conn= DriverManager.getConnection(connectionUrl,username,psw);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return conn;
    }
    public static void closeStatement(Statement state){
        if(state!=null){
            try {
                state.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
    public static void closeConnection(Connection conn){
        if(conn!=null){
            try {
                conn.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
}

properties文件内容,注意bundle.getString(“XXX”);XXX要和该文件中的变量名一样,比如bundle.getString(“driver”);

driver =com.mysql.jdbc.Driver
connectionUrl=jdbc:mysql://localhost:端口号/数据库名?useUnicode=true&characterEncoding=utf8
username =root//(默认是root)
psw=******//(连接数据库的密码)

测试类

package jdbc;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class testJdbc {
    public void insertTable(){
        //驱动注册
        Connection conn=null;
        Statement state=null;
        try {
            conn=jdbcUtil.getConnection();
            //String str="insert into usertable values(5,'zhao','123','123@1qq')";
            String str1="select *from usertable";
            state=conn.createStatement();
            ResultSet rs=state.executeQuery(str1);
            //int flag=state.executeUpdate(str);
            //System.out.print(flag);
            while(rs.next()){
                System.out.println(rs.getString("id")+" "+rs.getString("username")+" "
                        +rs.getString("password")+" "+rs.getString("email"));
            }
            if(rs!=null){
                rs.close();
            }

        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            jdbcUtil.closeConnection(conn);
            jdbcUtil.closeStatement(state);

        }

    }
    public static void main(String[] args) {
        testJdbc test=new testJdbc();
        test.insertTable();

    }
}

运行如下
在这里插入图片描述
修改数据库信息时只需要在properties中修改即可

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值