0

Consider am assigning the URL in the code below to a string, say

String link = "http://www.topix.com/rss/city/ellensburg-wa";

How should I use the string in the below code instead of the URL itself.

Note: am a beginner in java

 stmt.executeQuery("select url from urls where url='http://www.topix.com/rss/city/ellensburg-wa'");

 stmtR.executeUpdate("insert into urls values(21211,'http://www.topix.com/rss/city/ellensburg-wa','source',1,0)"
0

4 Answers 4

7

If you want to create a nice query use a prepared statement

PreparedStatement insertUrlStatement = con.prepareStatement("INSERT INTO urls VALUES(?, ?, ?, ?)");
//Replace the **first** "?" by an "id" variable content (containing an **int**)
insertUrlStatement.setInt(1, id);
//Replace the **second** "?" by the "url" variable content (containing a **String**)
insertUrlStatement.setString(2, url);
//Two other setXxx();
insertUrlStatement.executeUpdate()
Sign up to request clarification or add additional context in comments.

6 Comments

I agree; while some may say this is too hard for a beginner, it's the right way to do it. String concatenation is "never" the right way to build SQL queries, and this is just something you should learn from the start.
(1, id) (2,id) for what?? please explain
the first argument is the position of your replaced "?" the second is the value. (2, id) was meant to be (2, url) as the url variable you gave in your question, my mistake.
setInt, setString. sorry i could not understand its working here. could you be brief?
and so I should use (2, link) for inserting the string into DB rite? since I have assigned the URL to the string named link
|
2
stmt.executeQuery("select url from urls where url='" + link + "'");

stmtR.executeUpdate("insert into urls values(21211,'" + link + "','source',1,0)"

+ is Java's string concatenation operator.
See: http://download.oracle.com/javase/1.4.2/docs/api/java/lang/String.html


ATTENTION!!

You should really consider using prepared statements (see other answers) if you are going to use this for SQL queries.

3 Comments

You should also consider using prepared statements to avoid sql injections and improve performance.
It's a really bad Idea and dangerous.
@Colin I didn't realize this was being used to prepare SQL statements
1

I've got to give my 2p on this one.

NEVER EVER Use string concatenation and SQL.

(ok that should perhaps read as never use sting concatenation and user input)

Follow the advice given above about using prepared statements.

Think about what would happen if you used string concatenation and SQL, when some nasty user enters the link

x'; DROP TABLE urls; --

Your code would look like

stmt.executeQuery("select url from urls where url='x'; DROP TABLE urls; --'");

Seriously don't even write a prototype that does this, bad code is always bad code and will end up being used. You don't want to be fired for writing one of the top ten vulnerabilities do you? www.drdobbs.com/web-development/224400744

Goto this site for a lot more examples and reasons why SQL string concatenation is BAD http://unixwiz.net/techtips/sql-injection.html

1 Comment

is it really possible to send two (or more) SQL statements in one executeQuery? I think that ; is not accepted in a SQL statement. I could not do it using mysql-connector 5.1.5 ... and I remember it not working with older versions of Oracle.
0

You can do that like this:

stmt.executeQuery("select url from urls where url='"+link+"'");

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.