I am trying to connect to my DB
by MySQL
pe I get that error of Constructor threw exception; nested exception is java.lang.NullPointerException
and do not access any parameter:
jdbc.driverClassName = com.mysql.jdbc.Driver
jdbc.url = jdbc:mysql://localhost:3306/facturacion?zeroDateTimeBehavior=convertToNull
jdbc.username = myuser
jdbc.password = mypassword
Caused by: java.lang.NullPointerException
at ifac.Config.MySQL.<init>(MySQL.java:43)
at ifac.Config.MySQL$$EnhancerBySpringCGLIB$$63df6b53.<init>(<generated>)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:147)
... 63 more
@Configuration
@EnableTransactionManagement
@Component
@PropertySource(value = {"classpath:application.properties"})
public class MySQL {
@Autowired
private Environment environment;
private Connection conexion = null;
public MySQL() {
System.out.println("...........................................");
System.out.println("...........................................");
// System.out.println(environment.getProperty("jdbc.username"));
// System.out.println(environment.getProperty("jdbc.password"));
// System.out.println(environment.getProperty("jdbc.driverClassName"));
// System.out.println(environment.getProperty("jdbc.url"));
System.out.println("..........................................");
System.out.println("..........................................");
try {
String usuario = environment.getRequiredProperty("jdbc.username");
String password = environment.getRequiredProperty("jdbc.password");
Class.forName(environment.getRequiredProperty("jdbc.driverClassName"));
String url = environment.getRequiredProperty("jdbc.url");
conexion = (Connection) DriverManager.getConnection(url, usuario, password);
} catch (SQLException | ClassNotFoundException ex) {
}
}
public Connection getConexion() {
return conexion;
}
public Connection cerrarConexion() {
try {
conexion.close();
} catch (SQLException ex) {
System.out.println(ex);
}
conexion = null;
return conexion;
}
}