Click to See Complete Forum and Search --> : Java and MySQL


RAGEAngel9
10-13-2003, 04:59 PM
Hey,
I'm working on a project where I'm updating a MySQL DB through a Java applet. I'm wondering if there will be any problem if I opened a connection at the beginning of the applet and left it open til the applet was finished running?
Also, would there a problem if the connection was open and the user closed the applet early?
I'm new to sql and java too, so I'm not familiar with things like this.

Stuka
10-13-2003, 05:15 PM
Leaving the connection open is a Good Idea (TM). DB connections are relatively expensive to set up, so it's better to keep a single one alive. Killing the applet SHOULD cause the connection object to go out of scope (if it doesn't completely kill the VM, which would have the same basic effect), so the only danger is if your update was in progress and got killed - if this is critical info, use transactions :)

RAGEAngel9
10-13-2003, 05:17 PM
Great thanks!
Hmm transaction, sounds like I better go look that up.

Stuka
10-13-2003, 05:53 PM
Transactions make a group of DB activities atomic - basically, from the time you start it, until the time you end it, if any part fails, you can roll back all of the changes. Also, if your program dies, the transaction will die also, rolling back any partial changes.

RAGEAngel9
10-13-2003, 06:06 PM
hmmm not sure if that's exactly what I'd want.
Because I make anywher from 1 to well technically infinite(but unlikely) insertions each time the applet is run. If it only rolled back the last insertion, I could except that.

Also 2 other questions.
When using the following lines:

Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");

String url = "jdbc:odbc:Fred";
Connection con = DriverManager.getConnection(url, "Fernanda", "J8");


Where would I get the string for the forName if I'm using MySQL?
If I'm using a um non-local database, how do I edit the url to add in the address of the db?

In the examples I've looked at they mention adding the url but only show how to use local DBs.

Above examples taked from java.sun.com.

RAGEAngel9
10-13-2003, 06:17 PM
Oh oh I found my answer.
Should have just started with mysql's site instead of java's or my friggin "Deep Insight"ful text book.
In case, I confused anyone or anyone else is as dumb as me.
This is what I was looking for:
Class.forName("com.mysql.jdbc.Driver").newInstance();

Connection conn = DriverManager.getConnection("jdbc:mysql://localhost/test?user=monty&password=greatsqldb");

Stuka
10-14-2003, 10:03 AM
You can make each individual insert a transaction if that's appropriate - it really depends on the nature of your application. If each insert is done independently, then if the user kills the app early, you'll either get the last insert or not, and there's no way to deterministically decide which will happen. If that's not an issue for you, then there's nothing to worry about. If a given 'set' of inserts needs to either all make it or not, then transactions will help with that. Otherwise, I think you're ok.