0
I created a propertie file for the database to read this file from there but the console presents me the following error:
Erronull\properties\conexao.propertie (O sistema não pode encontrar o caminho especificado)
Erronull\properties\conexao.propertie (O sistema não pode encontrar o caminho especificado)
Erronull\properties\conexao.propertie (O sistema não pode encontrar o caminho especificado)
ConexaoMySQL: The url cannot be null
java.sql.SQLException: The url cannot be null
at java.sql.DriverManager.getConnection(Unknown Source)
at java.sql.DriverManager.getConnection(Unknown Source)
at modelo.ConexaoMySQL.conectar(ConexaoMySQL.java:18)
at helper.BancoDadosHelper.getNomeColunas(BancoDadosHelper.java:18)
at principal.Main.testaConexDB(Main.java:38)
at principal.Main.menu(Main.java:30)
at principal.Main.main(Main.java:17)
Exception in thread "main" java.lang.NullPointerException
at helper.BancoDadosHelper.getNomeColunas(BancoDadosHelper.java:21)
at principal.Main.testaConexDB(Main.java:38)
at principal.Main.menu(Main.java:30)
at principal.Main.main(Main.java:17)
Related Classmysql:
package modelo;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class ConexaoMySQL {
private static Connection conexao;
public static boolean conectar() {
Propriedade.setPath(System.getProperty("jdbc.url")+"\\properties\\conexao.propertie");
String url = Propriedade.getValor("url");
String usr = Propriedade.getValor("user");
String pwd = Propriedade.getValor("password");
try {
Class.forName("com.mysql.jdbc.Driver");
conexao = DriverManager.getConnection(url,usr,pwd);
System.out.println("ConexaoMySQL.conectar");
return true;
} catch (Exception e) {
System.out.println("ConexaoMySQL: " + e.getMessage());
e.printStackTrace();
return false;
}
}
public static void fecharConexao() {
if (conexao != null) {
try {
conexao.close();
conexao = null;
System.out.println("ConexaoMySQL.fecharConexao");
} catch (SQLException e) {
System.out.println("ConexaoMySQL: " + e.getMessage());
e.printStackTrace();
}
}
}
public static Connection getConexao(){
return conexao;
}
}
Property class :
package modelo;
import java.io.FileInputStream;
import java.util.Properties;
public class Propriedade {
private static String path;
public static void setPath(String caminhoDoArquivo) {
path = caminhoDoArquivo;
}
public static String getValor(String key) {
String returning = null;
FileInputStream fis = null;
Properties prop = new Properties();
try {
fis = new FileInputStream(path);
prop.load(fis);
returning = prop.getProperty(key);
if (fis != null) {
fis.close();
}
} catch (Exception e) {
System.out.println("Erro" + e.getMessage());
}
return returning;
}
}
File connected.propertie.:
url = jdbc:mysql://localhost:3306/sistema;
user = root;
password = ;
In the method connect I remove this stretch then ? (Property.setPath(System.getProperty("user.dir")+" related properties.propertie"); ), but still giving error.
– Marcelo T. Cortes
@Jarwin this, removes it and uses the class this way. The file is in the classpath of your application, isn’t it? Another thing: removed the
;
of property?– Bruno César
Face is kind of hard to explain the error that is happening, here is my complete code for you to better understand my situation: https://www.dropbox.com/s/0vo699f7b4nz1v9/AulaBanco.zip?dl=0 and my sql file: http://pastebin.com/LxfWA3GG
– Marcelo T. Cortes
@Jarwin guy, put the error, unfortunately I can’t take your project and test here, I just won’t install Mysql for this or convert SQL to run in another database. This answer solves your initial error, if it is another completely different error, consider creating another answer.
– Bruno César
'Exception in thread "main" java.lang.Exceptionininitializererror at model.ConexaoMySQL.connect(Conexaomysql.java:13) at helper.BancoDadosHelper.getNomeColunas(Bancodadoshelper.java:18) at principal.Main.testaConexDB(Main.java:38) at principal.Main.menu(Main.java:30) at principal.Main.main(Main.java:17) Caused by: java.lang.Nullpointerexception at java.util.Properties$Linereader.readline(Unknown Source) at java.util.Properties.load0(Unknown Source) at java.util.Properties.load(Unknown Source) at modelo.Propriedade. <clinit>(property.java:15) ... 5 more'
– Marcelo T. Cortes
@Jarwin your property file is not on classpath application, just as it is not in a folder called properties who is in the classpath. Put up the file
conexao.propertie
in the briefcasesrc
and alters of/properties/conexao.propertie
for/conexao.propertie
– Bruno César
You have now given this error: Conexaomysql: Access denied for user 'root'@'localhost' (using password: YES) java.sql.Sqlexception: Access denied for user 'root'@'localhost' (using password: YES) at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:998)
 at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3835)
 at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3771)
 at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:870)
 at com.mysql.jdbc.MysqlIO.proceedHandshakeWithPluggableAuthentication(Mysqlio.java:1659) .......... And I don’t have a password in my bank...
– Marcelo T. Cortes
@Jarwin man, are you reading the error messages? Did you see that the problem is authentication? Take a look, make the necessary changes, the best way to learn is to explore the problems and not just expect someone to do their expensive homework if you look at the
getValor
ofPropriedade
will see why the error, is simple, anyway the answer is updated...– Bruno César
It worked out dude, sorry to keep insisting on mistakes all the time is that I until I can’t figure out what’s going on I go crazy !!!! I just wanted to understand why returning.length() > 0 ?
– Marcelo T. Cortes
@Jarwin this why I’ll leave it up to you to find out, run tests and everything, you’ll see why he made a difference in your case :)
– Bruno César
Ok , You recommend me some good book to learn java web and JDBC in view of your experience ?
– Marcelo T. Cortes
Let’s go continue this discussion in chat.
– Marcelo T. Cortes