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

jdbc 连接数据库

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

static String str="com.mysql.jdbc.Driver"; public static String db_URL="jdbc:mysql://localhost:3306/test"; private static String db_Name="root"; private static String db_Password="123456";

以前的代码就是这样写的, 后来在园子里面找到了一个配置文件写的数据库连接感觉棒棒哒.~~

 // 加载dbinfo.properties配置文件
            InputStream in = JdbcUtils.class.getResourceAsStream("dbinfo.properties");// /jdbc/src/dbutils/dbinfo.properties
            Properties properties = new Properties();
            properties.load(in);

            // 获取驱动名称、url、用户名以及密码
            driverName = properties.getProperty("driverName");
            url = properties.getProperty("url");
            user = properties.getProperty("user");
            password = properties.getProperty("password");

            // 加载驱动
            Class.forName(driverName);

文章的主体是看到园子里一个朋友的. 不过改了   InputStream in = JdbcUtils.class.getResourceAsStream("dbinfo.properties");// /jdbc/src/dbutils/dbinfo.properties

还有就是总结  properties  java类的使用.代码几句话就搞定了.

类.getResourceASStream("/路径名");//    以项目为根路径;

load方法是总要的方法;

getProperty();//看下面插入的properties文件就知道了.   文件的后缀名是.properties

driverName=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/jdbc
user=root
password=123456

 

jdbc 连接数据库

标签:

人气教程排行