Statement sm = con.createStatement();
sm.executeUpdate("INSERT INTO data "+"VALUES('person', 70)");
🔒 Locked
[java] Writing to a Database
Advertisement
Original Post
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 but instead of having 'person' i want it to get text from a text area and send that.
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.
How are you connecting now?
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
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
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.
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.
Topic Locked
This topic has been locked by a moderator. New replies are not allowed.
Sign in to reply to this topic.
Advertisement