Java database connection codes, do you really understand what is happening or write the code? I write

Asked

Viewed 32 times

-2

People speak the truth, when it comes to connecting to the database, you understand right what’s going on? Or just save the codes to type the sql queries as I do? This is very difficult to understand, do you think most people know this? or are there few? I have a question regarding the java, referring to the codes of manipulation of database,already used them a long time, but, I only recorded the sequence of them.

On this line of code below:

String parte1 = "jdbc:mysql://"+this.ip+":"+this.porta+"/"+this.nomeBanco;
Connection conexao = DriverManager.getConnection(parte1,this.usuario,this.senha);
PreparedStatement pesquisa = conexao.prepareStatement("select *from filmes");

When we do Connection connected; this means that I am creating a Connection type variable that will store values. But what kind of value can I keep? An object? primitive types?

By the above code, tell me if I am correct of what will happen, the connection variable type Connection will receive the return of the Drivermanager class method DriverManager.getConnection(parte1,this.usuario,this.senha);

I gave a data output on this variable conexao and returned that:

com.mysql.cj.jdbc.ConnectionImpl@37d31475

After that, the code does this:

PreparedStatement pesquisa = conexao.prepareStatement("select *from filmes");

In that part: conexao.prepareStatement("select *from filmes"); then I don’t understand anything else the variable conexao in an era a variable was holding the value "com.mysql.Cj.jdbc.Connectionimpl@37d31475" and now it has become an object calling a method?

And people I’ve studied By, mvc, html,css,javascript,php,java,jquery,Aravel I’ve done web sites systems, I’m already 3 years with programming, but I didn’t understand it there not.

If someone can send an explanation of how it works well this connection code to bank thank, because, and people speak the truth everyone knows how it works well or just uses more select,update and etc as I do?

  • Remembering that I record the code because I couldn’t understand.

  • https://docs.oracle.com/javase/8/docs/api/java/lang/Object.html#toString--

  • When you print out some object (System.out.println(objeto)) internally is called the method toString of this object. In many cases (as seems to be the Connection) this method is not implemented and the class ends up calling the mother class method (which in case should be Object). And Object.toString returns the class name along with the hashcode (as explained by the link above), which is this com.mysql.cj.jdbc.ConnectionImpl@37d31475 that you saw. But that doesn’t mean that this is the value/content of the class.

  • The variable conexao is therefore an instance of com.mysql.cj.jdbc.ConnectionImpl (that is, it is an object, and therefore has methods, etc.). What is inside it does not matter from the point of view of who will use, what matters is that it ensures that it is a valid connection to the database. But if you want to see what it has, just check the source code: https://github.com/mysql/mysql-connector-j/blob/release/8.0/src/main/user-impl/java/com/mysql/cj/jdbc/ConnectionImpl.java

  • By the way, printing an object does not guarantee that it will show everything it has. Each class can overwrite toString to return what you want (or use the implementation of Object, already explained above). Even because not everything needs/should be shown, and anyway the toString that’s not the point.

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.