Skip to main content
gamedev.net
🔒 Locked

[java] Writing to a Database

Started by Swattkidd Jun 16, 2007 at 7:22 PM 8 replies 1.2k views
Original Post
Swattkidd
Swattkidd
Ok well i have a table that i want to send some information. I figured out how to read information from the table and i do know how to send information to the table, but the problem comes in when i want to getText from a textfield and then send that text to the table. to just send the text to the table i do this
    Statement sm = con.createStatement();
       sm.executeUpdate("INSERT INTO data "+"VALUES('person', 70)");
but instead of having 'person' i want it to get text from a text area and send that.
CaptainJester
CaptainJester
Statement sm = con.createStatement();       sm.executeUpdate("INSERT INTO data "+"VALUES('" + myTextField.getText() + "', 70)");


Although this isn't secure. Once you learn some more, learn how to use prepared statements. That will increase your security.
"None of us learn in a vacuum; we all stand on the shoulders of giants such as Wirth and Knuth and thousands of others. Lend your shoulders to building the future!" - Michael Abrash[JavaGaming.org][The Java Tutorial][Slick][LWJGL][
Swattkidd
Swattkidd
ahh yes, i figured it out after a couple of hours :D

but yea i ran into another problem, how can i deploy my application, using a jar file, and still have it connect to a database?

because right now when i run my jar file it doesnt connect.:O
CaptainJester
CaptainJester
How are you connecting now?
"None of us learn in a vacuum; we all stand on the shoulders of giants such as Wirth and Knuth and thousands of others. Lend your shoulders to building the future!" - Michael Abrash[JavaGaming.org][The Java Tutorial][Slick][LWJGL][
ROBERTREAD1
ROBERTREAD1
This function is in VBScript but I'd recommend something similar in Java.

Function SQLString(sText, iLeft)
If iLeft=0 Then
SQLString = Replace(sText, "'" , "''")
Else
SQLString = Replace(Left(Trim(sText), iLeft), "'" , "''")
End If
End Function


It escapes ' chars to '' so SQL injection is harder and also trims text to fit into the width you set for the field
Swattkidd
Swattkidd
Quote:
Original post by CaptainJester
How are you connecting now?


Well see at first i realized i was connecting to localhost so i changed it to this

con = DriverManager.getConnection(jdbc:mysql://localhost:3306/java,
"meow", "meow

then i tried that same thing with my static ip, not localhost. Still no connection, i either get can not connect im guessing that is because of the port number or i get a NESTED exception which i have no idea why.
ddyer
ddyer
Quote:
Original post by Swattkidd
ahh yes, i figured it out after a couple of hours :D

but yea i ran into another problem, how can i deploy my application, using a jar file, and still have it connect to a database?

because right now when i run my jar file it doesnt connect.:O


Even if it's possible, this isn't a good idea. A database which
can be manipulated directly by a remote process (especially a java
process) can easily be hijacked and hacked.

Instead, implement the database manipulation by a trusted agent,
and communicate with the agent from your java process. Note;
your efforts will be wasted if the communication protocols
are tantamount to the java process writing the queries and passing
them to the trusted agent.
---visit my game site http://www.boardspace.net - free online strategy games
Swattkidd
Swattkidd
Well i will never really hand this application out, its just for me and my friend thats it. I am still having the problem of connecting to database from a JAR file. See above ^.
ddyer
ddyer
Unsigned applets can only open sockets back to the host from which
the applet was loaded, so the URL you use to open the
database needs to be similar to the code= url that
loaded the applet.
---visit my game site http://www.boardspace.net - free online strategy games
Swattkidd
Swattkidd
It is not an applet, its a normal application...

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.