1

I have created a MySQL database. I am building a GUI in java. How can I connect this Java software to the database?

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
SLA
  • 363
  • 2
  • 4
  • 11
  • Duplicate of [Java connectivity with MySQL](http://stackoverflow.com/questions/2839321/java-connectivity-with-mysql) – BalusC Nov 20 '10 at 17:39

5 Answers5

2

First, you need to add the mysql jdbc driver jar to your project, then you would create a connection as follows:

String url = "jdbc:mysql://yourhost:port/dbname";
Class.forName ("com.mysql.jdbc.Driver"); // to load the driver
Connection conn = DriverManager.getConnection (url, userName, password);

Here yourhost is the name or ip address of the server, port is the port number to which the mysql server is bound, dbname is the name of your database.

Maurice Perry
  • 32,610
  • 9
  • 70
  • 97
0

using any of the miriad of Java/SQL API, so you can perform normal SQL queries. JDBC is a start

hhafez
  • 38,949
  • 39
  • 113
  • 143
0

Start using JDBC, it's best for start. Take a look on some tutorials

amra
  • 16,125
  • 7
  • 50
  • 47
0

this post has helped me quite alot! I'm quite 'disapointed' and 'suprised' I suppose that Java doesn't have the fuctionatly of connected to online databases built in. One thing that would make it better, and please tell me the followings true: the fact that users of my software don't have to do anything special like install any drivers?

Andy
  • 3,600
  • 12
  • 53
  • 84
  • So, you expect that Sun/Oracle needs to create and maintain the JDBC connectivity with over a hundred of different databases internally, of which each communicates in a different way with the outside world? Forget about it, it's not their responsibility :) In fact, I am very surprised about your surprisement. The JDBC API is actually smart. It enables you to write the same Java code regardless of the specific DB server used. Whenever you want to switch the DB, all you need to change are the JDBC driver and the URL and if necessary some DB-specific SQL statements. – BalusC Dec 01 '10 at 19:08
  • Why is that surprising to you? People are either ignorant of all the licensing terms with redistributing the other jdbc drivers or they don't give a shit and just want things to work out of the box. – Hiro2k Dec 01 '10 at 19:18
  • Hi, again, I'm stunned by the speady response and very appriciative! It's not that I don't expect things like this to work out of the box, and I'm not ignorant, it's just that I thought Java having the nature that it has it would be extremely compatible 'out of the box/built' with things online like this! But anyway, once my software is all packaged up, will the user need any extra files or drivers, etc – Andy Dec 01 '10 at 19:40
  • You should know that you can't redistribute the MySQL driver unless your project is also licensed by some OSI approved open source license. Well you could also pay MySQL for a license, but that's a big hassle. We had to switch to Postgres because of Oracles crap. – Hiro2k Dec 02 '10 at 17:02
  • @Hiro2k Thank you for that bit of information, I'm now even more intreged! By redistribution what exactly do you mean. I'd be using it in commercal software to connect to a MySQL database online, is that OK or is against Oracles rules? Anyway, two qestions. 1) How does anyone know your using JDBC to tell you off & 2) Don't you still need JDBC for postgres? – Andy Dec 14 '10 at 12:01
  • The JDBC driver for MySQL is distributed under a GPL license which means you can't link to it directly without making your own software GPL. There is no way for them to tell, but if you get audited then it would become a huge problem and a liability for your customers. 2 The Postgres JDBC driver uses a BSD license which allows you to do whatever you want basically. Redistribution can mean a lot of things, you need to talk to your lawyer to find out. MySQL told me that even if we don't bundle it with our software, telling our customer to download and install mysql and the driver counts. – Hiro2k Dec 14 '10 at 15:46
  • Thank you for the information about licensing! It seems a bit of a pain in the bum to me but that's business for ya! I'll do some more research into all this but it looks like i'll be switching to Postgres unfortunatly! Thanks anyway – Andy Dec 14 '10 at 18:52
0

I've already added one answer - asking a question but the comments/reply's I got seem to be a bit off topic and now I've commented back asking for the answer to my original question no one seems to be answering (Have a look for your self - see what I mean)

So, the orignal question :: The only thing I want to know;

Do the users of my software I made using JDBC have to do anything special regarding JDBC, or is just me who needs to have JDBC while writting the program?

PS Sorry if I'm a bit pushy or doing things that your not supposed to do - I'm new to this community!

Andy
  • 3,600
  • 12
  • 53
  • 84