springboot设置只让连接三次,如果还是失败就不再连接没报错
java.lang.ExceptionInInitializerError
Caused by: java.lang.NullPointerException
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Unknown Source)
at com.test.TestApplication.<clinit>(TestApplication.java:27)
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.scheduling.annotation.EnableScheduling;
import java.sql.*;
@SpringBootApplication
@EnableCaching
@EnableScheduling
public class TestApplication {
private static int MAX_CONNECTION_ATTEMPTS = 3;
@Value("${spring.datasource.url}")
private static String url ;
@Value("${spring.datasource.username}")
private static String userName ;
@Value("${spring.datasource.password}")
private static String pwd ;
@Value("${spring.datasource.driver-class-name}")
private static String driverName ;
static {
try {
Class.forName(driverName);
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
public static void main(String[] args) throws SQLException {
int connectionAttempts = 0;
while (connectionAttempts < MAX_CONNECTION_ATTEMPTS) {
Connection connection = null;
PreparedStatement ps = null;
ResultSet rs = null;
String sql=" select 1 from dual ";
try {
connection = DriverManager.getConnection(url,
userName, pwd);
ps = connection.prepareStatement(sql);
rs = ps.executeQuery();
while (rs.next()) {
int id =rs.getInt(0);
String typename =rs.getString(1);
System.out.println(id+":"+typename);
}
SpringApplication.run(TestApplication.class, args);
} catch (SQLException e) {
connectionAttempts++;
e.printStackTrace();
}finally{
if(rs != null) {
rs.close();
}
if(ps !=null){
ps.close();
}
if(connection != null) {
connection.close();
}
}
}
}
}
如下图:我后台都是使用了大写,也查看了返回值,大写
检查前端获取数据时发现,我前端获取到的数据参数名为小写了。
下面就是我前端获取到的数据展示。
经过繁琐的测试和阅读,终于有成就了。。666
只需要在我的Domain文件里把未对象添加@JsonProperty(“需要显示的字段”),这样,框架会自动帮我们在转json数据时,自动转为我们设置的key
最后:,在DAO的所有对象上都加上这个@JsonProperty参数
哈哈完美。
最后附上 @JsonProperty 的使用详解:
源于chatGPT仅供参考
要在Spring Boot中设置只允许连接三次,如果连续三次失败则不再尝试连接,并且不显示错误信息,您可以使用以下代码进行修改:
```java
@SpringBootApplication
@EnableCaching
@EnableScheduling
public class TestApplication {
private static final int MAX_CONNECTION_ATTEMPTS = 3;
@Value("${spring.datasource.url}")
private static String url;
@Value("${spring.datasource.username}")
private static String userName;
@Value("${spring.datasource.password}")
private static String pwd;
@Value("${spring.datasource.driver-class-name}")
private static String driverName;
static {
try {
Class.forName(driverName);
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
public static void main(String[] args) throws SQLException {
int connectionAttempts = 0;
while (connectionAttempts < MAX_CONNECTION_ATTEMPTS) {
Connection connection = null;
PreparedStatement ps = null;
ResultSet rs = null;
String sql = "select 1 from dual";
try {
connection = DriverManager.getConnection(url, userName, pwd);
ps = connection.prepareStatement(sql);
rs = ps.executeQuery();
while (rs.next()) {
int id = rs.getInt(1);
String typename = rs.getString(2);
System.out.println(id + ":" + typename);
}
SpringApplication.run(TestApplication.class, args);
break; // 成功连接时跳出循环
} catch (SQLException e) {
connectionAttempts++;
if (connectionAttempts >= MAX_CONNECTION_ATTEMPTS) {
break; // 达到最大连接尝试次数时跳出循环
}
} finally {
if (rs != null) {
rs.close();
}
if (ps != null) {
ps.close();
}
if (connection != null) {
connection.close();
}
}
}
}
}
上述代码中,我们通过添加 break
语句在成功连接时和达到最大连接尝试次数时跳出循环。这样可以确保只进行三次连接尝试,如果连续三次都失败会退出循环,并且不显示错误信息。
请注意,我们还将 MAX_CONNECTION_ATTEMPTS
常量声明为 final
,以防止其它地方修改该值。此外,根据您的数据库查询语句需要调整 ResultSet 获取字段的索引。
希望这可以解决您的问题。如果您有任何进一步的疑问,请随时提问。
```c#
根据您提供的代码和问题描述,存在以下几个问题:
1. 使用`@Value`注解在静态字段上:`@Value`注解通常用于注入配置值到实例变量中,而不适用于静态字段。因此,将`@Value`注解从静态字段移除,并将相关的配置值作为参数传递给静态方法。
2. NPE(NullPointerException)异常:在您的代码中,可能引发NPE异常的位置是`Class.forName(driverName)`。这可能是由于`driverName`的值为空引起的。请确保您在配置文件中正确配置了`spring.datasource.driver-class-name`属性,并且该属性的值是有效的数据库驱动程序类名。
3. 异常处理:在异常处理中,您只是打印了异常信息,但没有进一步的处理或跳出循环。建议在达到最大连接尝试次数后,使用`break`语句跳出连接循环。
下面是修正后的代码示例:
```java
@SpringBootApplication
@EnableCaching
@EnableScheduling
public class TestApplication {
private static final int MAX_CONNECTION_ATTEMPTS = 3;
@Value("${spring.datasource.url}")
private String url;
@Value("${spring.datasource.username}")
private String userName;
@Value("${spring.datasource.password}")
private String pwd;
@Value("${spring.datasource.driver-class-name}")
private String driverName;
public static void main(String[] args) throws SQLException {
SpringApplication.run(TestApplication.class, args);
int connectionAttempts = 0;
while (connectionAttempts < MAX_CONNECTION_ATTEMPTS) {
Connection connection = null;
PreparedStatement ps = null;
ResultSet rs = null;
String sql = "SELECT 1 FROM dual";
try {
Class.forName(driverName);
connection = DriverManager.getConnection(url, userName, pwd);
ps = connection.prepareStatement(sql);
rs = ps.executeQuery();
while (rs.next()) {
int id = rs.getInt(1);
String typename = rs.getString(2);
System.out.println(id + ":" + typename);
}
break; // 连接成功,跳出循环
} catch (ClassNotFoundException | SQLException e) {
connectionAttempts++;
e.printStackTrace();
} finally {
if (rs != null) {
rs.close();
}
if (ps != null) {
ps.close();
}
if (connection != null) {
connection.close();
}
}
}
}
}
请确保在配置文件中正确设置了数据库连接相关的参数,并且spring.datasource.driver-class-name
属性的值是有效的数据库驱动程序类名。
如果您仍然遇到问题,请提供完整的错误堆栈跟踪信息,以便我可以更好地帮助您解决问题。
```