How is Java related to Mysql?

Asked

Viewed 233 times

-1

I want to develop an expense control software and I want to use Javaswing with Mysql. So I have a question about the database. I know it stays on a server, but when I compile the program and want to distribute the software I have to install a server on other machines? Does the database go together with the program? I don’t understand how this relationship between Java and Mysql is done.

  • 2

    This has nothing to do with JAVA. Mysql is a separate product. If you want to use Mysql, you need to install Mysql on at least ONE of the client’s network machines. If you don’t want this feature, you can choose another of endless ways to store data with java. It would be nice to [Dit] your question and be clearer on what you really need. If it’s just 'talk' about it to get situated, it would be better to make a few dots with more specific questions to be able to participate in the network chat.

  • 1

    Not that I consider this to be one of the best questions on the site, but in my opinion it is perfectly valid. Unless someone tells me otherwise, I disagree with who votes to shut it down.

  • 1

    I don’t agree with the downvotes either. As absurd as it may seem some questioning (mainly for the great sages of the site), who asks and needs a clarification.

1 answer

1

When you go to connect Java with the database (in the case of Mysql), you typically do something like this:

Connection con = DriverManager.getConnection(
        "jdbc:mysql://localhost:3306/basededados", "usuario", "senha");

Note that in the first parameter you have a URL. If in it, instead of you use localhost, use an IP or host name, that’s where it will connect. Thus, you give the network location where the database is, and therefore the database does not need to be on the same machine where your Swing program runs. This command will only be executed when the program is running, never during the build. Because it may be that the user or seha, or even the URL itself, is read from the user or some configuration file.

You compile the program without the database, just put in the classpath or modulepath the JAR needed to connect to it at runtime. You do not need the database to compile the program.

It might even make sense for you to install Mysql on the same machine where the Swing program would run. But if you need two or more machines accessing the same database instead of separate database, it would only be installed on a single machine and accessed via IP or hostname.

To class DriverManager and the interface Connection are in the bundle java.sql. This is the package that contains the classes and interfaces needed for your program to use any database. However, the connection to the same database is managed by the connector, which in the case of Mysql, can be downloaded here. In general, for each different database type, you will need a different specific connector.

  • 2

    I found valid the answer. I did not understand the downvotes

  • 1

    I will be honest, at first I did not understand the answer, Aliais gave to understand, I will not deny that I still think that to the knowledge of AP I think it will sound confusing, I believe it is possibly necessary to explain to him that depending on the mysql case nor would be the focus of it, depends on the type of app, and if it is an app for multiple users or for a store for example, then it would be necessary to explain a little more the communication of the "network". And to return to the case otherwise would explain how to work with a "local bank" (something that is aimed at working with local same), I do not disagree with the answer...

  • 2

    ...only that at first it did not seem to me the sufficient explanation for what the ap needs to understand, I find the promising answer and that can evolve and will have my support in this case, only that I do not think that the downvotes were wrong, my downvote I removed already, I was going to comment at the time but I was a little busy, I removed it now because after editing the answer already has a "north", but I think that honestly the AP will have difficulty and you could consider improving the answer in the points I mentioned. I hope you will take everything into account as a suggestion to improve the posting.

  • 1

    I already think that when you answer only what you’ve been asking is enough. A plus would be at the discretion of who answers, alerting the user to other possibilities for databases, criteria for choice etc. Enriches the answer but it is not always what those who ask.

  • 1

    @Guilhermenascimento Well, I think the answer already covers all the points of the question. If he has a question like "how or where to install Mysql?", then I would say that it is best to open another question. If he wants clarification on what to do with the Connection, the same thing and could point up a lot of other questions on this site. However, I added a few more things that I think would be enough. If he (or anyone else) has more questions about my response, just post a comment.

  • @Joaopaulo if you look at the posting history will see that at first she did not answer some things of what was asked. Important this to contextualize the previous comments.

  • 2

    So in my case it wouldn’t necessarily be Mysql, I could use a local database. What would be the alternatives?

  • @Daniloferrari There are several databases you could use: Mysql, Mariadb, Postgresql, Oracle, SQL Server, Firebird, H2, Sqlite, Derby and a lot of others, the list is great. It is true that some of them like SQL Server and Oracle are fairly heavy and it would make little sense to use them locally, but still this is perfectly possible. In the case of Mysql, it is perfectly possible in any of the cases.

  • @Daniloferrari But if you ever think about changing the database, I would recommend Mariadb. However, this is my personal recommendation, there will be people who will have some different opinion.

  • Dear @Daniloferrari I believe that yes, it would be interesting you detail in which types of environment you believe your application will run, I speculated a little and I think that because of this you can already understand better and until you can improve your question, I think Victor already got a "north" for you, I’ll wait, I think it will be possible to keep the question open if you make such an improvement.

Show 5 more comments

Browser other questions tagged

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