java datasource 配置_java datasource.properties 数据库相关信息的配置

在数据库配置中,经常使用配置文件来配置数据库

1:数据配置文件

# databasetype : 数据库的类别

# driverclass     : 数据库驱动类

# databaseurl   : 数据库连接URL

# usejndi         : 是否使用JNDI调用数据库,true 开启,其余为默认值关闭

# databasejndi : 如果使用JNDI调用,则配置调用名称

# debug          : 日志工具,log4j,其余使用默认的Console

# ================ SQLite ================ #

databasetype=SQLite

driverclass=org.sqlite.JDBC

databaseurl=jdbc\:sqlite\:SQLiteDB\\test.db

usejndi=false

databasejndi=test

debug=log4j

# ================ MySQL ================ #

#databasetype=MySQL

#driverclass=com.mysql.jdbc.Driver

#databaseurl=jdbc:mysql://127.0.0.1:3306/test?user=root&password=123456&useUnicode=true&characterEncoding=GB2312

#usejndi=false

#databasejndi=test

#debug=console

2:读取数据库配置文件

import java.io.IOException;

import java.io.InputStream;

import java.util.Properties;

/**

* 读取properties配置文件的toolKit

*/

public class PropertiesHelper {

public static String getProperty(String fileName, String key) {

String value = "";

InputStream in = null;

try {

in = PropertiesHelper.class.getResourceAsStream("/" + fileName);

Properties properties = new Properties();

properties.load(in);

value = properties.getProperty(key);

} catch (Exception e) {

e.printStackTrace();

} finally {

try {

in.close();

} catch (IOException e) {

e.printStackTrace();

}

}

return value;

}

}

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: application.properties是Spring Boot项目中的配置文件,用于配置应用程序的各种属性。其中,配置数据库的属性如下: # 数据库配置 spring.datasource.url=jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8&useSSL=false spring.datasource.username=root spring.datasource.password=123456 spring.datasource.driver-class-name=com.mysql.jdbc.Driver 其中,spring.datasource.url是数据库连接地址,spring.datasource.username和spring.datasource.password是数据库的用户名和密码,spring.datasource.driver-class-name是数据库驱动程序的类名。这些属性可以根据实际情况进行修改。 ### 回答2: 在使用Spring Boot框架开发应用时,常常需要连接数据库来存取数据。而连接数据库配置通常都是在application.properties文件中进行。本文将介绍如何在application.properties文件中配置数据库。 1. 首先需要选择一个合适的数据库,通常会选择MySQL、Oracle、PostgreSQL、SQL Server等常用的关系型数据库。在选择数据库时,需要考虑实际业务需求、性能要求和数据安全等因素。 2. 在选择好数据库后,在应用的pom.xml文件中引入相关的依赖,例如: ``` <!--MySQL数据库--> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency> <!--Oracle数据库--> <dependency> <groupId>com.oracle.ojdbc</groupId> <artifactId>ojdbc8</artifactId> </dependency> <!--PostgreSQL数据库--> <dependency> <groupId>org.postgresql</groupId> <artifactId>postgresql</artifactId> </dependency> <!--SQL Server数据库--> <dependency> <groupId>com.microsoft.sqlserver</groupId> <artifactId>mssql-jdbc</artifactId> </dependency> ``` 3. 然后在application.properties文件中配置数据库连接相关信息,例如: ``` #MySQL数据库 spring.datasource.url=jdbc:mysql://localhost:3306/test_db spring.datasource.username=root spring.datasource.password=123456 #Oracle数据库 spring.datasource.url=jdbc:oracle:thin:@localhost:1521:orcl spring.datasource.username=system spring.datasource.password=123456 #PostgreSQL数据库 spring.datasource.url=jdbc:postgresql://localhost:5432/test_db spring.datasource.username=postgres spring.datasource.password=123456 #SQL Server数据库 spring.datasource.url=jdbc:sqlserver://localhost:1433;databaseName=test_db spring.datasource.username=sa spring.datasource.password=123456 ``` 4. 其中,spring.datasource.url表示数据库的连接地址,可以根据实际情况修改;spring.datasource.username和spring.datasource.password表示数据库的用户名和密码,同样也需要根据实际情况修改。 5. 如果需要指定其他配置,可以增加如下配置: ``` #指定JDBC驱动类名 spring.datasource.driver-class-name=com.mysql.jdbc.Driver spring.datasource.driver-class-name=oracle.jdbc.driver.OracleDriver spring.datasource.driver-class-name=org.postgresql.Driver spring.datasource.driver-class-name=com.microsoft.sqlserver.jdbc.SQLServerDriver #指定初始化连接池大小 spring.datasource.initialSize=5 #指定连接池最大值 spring.datasource.maxActive=50 #指定获取连接的最大等待时间,毫秒为单位 spring.datasource.maxWait=60000 #指定测试连接是否可用的SQL语句 spring.datasource.validationQuery=SELECT 1 #指定空闲连接的最大存活时间,毫秒为单位 spring.datasource.maxIdle=30 #指定连接池中最小的连接数 spring.datasource.minIdle=5 #指定数据库字符集 spring.datasource.connectionProperties=useUnicode\=true&characterEncoding\=UTF-8 ``` 以上就是如何在application.properties文件中配置数据库连接的方法,通过以上配置,应用就可以使用数据库来存取数据了。 ### 回答3: application.properties是Spring Boot中常用的配置文件之一,它主要用于配置应用程序中的不同组件,包括但不限于数据库、邮件、缓存等。在配置数据库时,我们可以使用如下方式: 1. 配置数据库连接信息: spring.datasource.url=jdbc:mysql://localhost:3306/mydb spring.datasource.username=root spring.datasource.password=123456 其中,spring.datasource.url用于指定数据库的连接地址和端口号,mydb为要连接的数据库名称;spring.datasource.username和spring.datasource.password分别用于指定数据库的用户名和密码,这些信息需要根据实际情况来进行配置。 2. 配置数据库连接池信息: spring.datasource.driver-class-name=com.mysql.jdbc.Driver spring.datasource.initialSize=10 spring.datasource.maxActive=50 spring.datasource.maxWait=60000 spring.datasource.minIdle=5 spring.datasource.testWhileIdle=true spring.datasource.validationQuery=SELECT 1 其中,spring.datasource.driver-class-name用于指定数据库的驱动类;spring.datasource.initialSize和spring.datasource.maxActive用于配置数据库连接池的初始大小和最大连接数;spring.datasource.maxWait用于指定从连接池中获取连接的最大等待时间;spring.datasource.minIdle用于指定连接池中保持的最小空闲连接数;spring.datasource.testWhileIdle和spring.datasource.validationQuery分别用于测试连接是否有效。 3. 配置数据库方言和生成策略: spring.jpa.database=mysql spring.jpa.hibernate.ddl-auto=update spring.jpa.show-sql=true spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL5Dialect 其中,spring.jpa.database用于指定使用的数据库类型;spring.jpa.hibernate.ddl-auto用于指定生成表的策略,如update表示根据实体类自动更新表结构;spring.jpa.show-sql用于指定是否显示SQL语句;spring.jpa.properties.hibernate.dialect用于设置使用的方言,可以根据不同的数据库类型进行配置。 以上就是常用的application.properties配置数据库的方法,需要根据实际情况进行配置,以确保应用程序能够正常连接数据库并运行。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值