techPowerUp! Forums

Go Back   techPowerUp! Forums > Software > Programming & Webmastering

Reply
 
Thread Tools
Old Dec 20, 2012, 11:56 AM   #1
Wozzer
500 Posts
 
Join Date: Jun 2008
Location: England
Posts: 990 (0.55/day)
Thanks: 166
Thanked 76 Times in 72 Posts
Send a message via MSN to Wozzer

System Specs

SQL Operations in Java

Hi all,

I'm trying to create an if statement that:
"When a user input equals ANY of the the data in column "userID", Then execute"

I'm aware you can use the .equals operation but not sure how you would link it to the column within the database.

Wozzer
Wozzer is offline  
Reply With Quote
Old Dec 20, 2012, 12:28 PM   #2
Mindweaver
Moderato®™
 
Mindweaver's Avatar
 
Join Date: Apr 2009
Location: Statesville, NC
Posts: 3,622 (2.42/day)
Thanks: 4,313
Thanked 2,315 Times in 1,148 Posts

System Specs

Have you loaded the JDBC driver, and successfully connected to your database?
__________________
“As long as I feel the warmth from the sun and breathe precious air…. I must ask questions to feed the Mind!”

Battletag: Mindweaver#1523
Mindweaver is offline  
Crunching for Team TPU
Reply With Quote
Old Dec 20, 2012, 12:47 PM   #3
3870x2
3500 Posts
 
3870x2's Avatar
 
Join Date: Feb 2008
Location: Joplin, Mo
Posts: 4,540 (2.38/day)
Thanks: 175
Thanked 691 Times in 557 Posts
Send a message via Skype™ to 3870x2

System Specs

Quote:
Originally Posted by Mindweaver View Post
Have you loaded the JDBC driver, and successfully connected to your database?
It seems like something he would have already done if he is at this point.

If you haven't, this would be the place to start.
__________________
A+, N+, S+, MCSE.
Heatware
STEAM ID Name: furi0nst0rmrage (0s are zeros)
M O D E R N||W A R F A R E || 2 || CLUBHOUSE // TEAM
The amount exaltation of the processor cores can brings amazing floating” -sparkle
3870x2 is offline  
Reply With Quote
Old Dec 20, 2012, 01:01 PM   #4
Mindweaver
Moderato®™
 
Mindweaver's Avatar
 
Join Date: Apr 2009
Location: Statesville, NC
Posts: 3,622 (2.42/day)
Thanks: 4,313
Thanked 2,315 Times in 1,148 Posts

System Specs

Quote:
Originally Posted by 3870x2 View Post
It seems like something he would have already done if he is at this point.

If you haven't, this would be the place to start.
It would seem, but I'd like to know for sure.

If it's just the Select statement then something like this:

Code:
"SELECT * FROM UserTable WHERE userID = '" + UserInput + "';"
__________________
“As long as I feel the warmth from the sun and breathe precious air…. I must ask questions to feed the Mind!”

Battletag: Mindweaver#1523

Last edited by Mindweaver; Dec 20, 2012 at 01:18 PM.
Mindweaver is offline  
Crunching for Team TPU
Reply With Quote
The Following User Says Thank You to Mindweaver For This Useful Post:
Old Dec 20, 2012, 01:20 PM   #5
Aquinus
3500 Posts
 
Aquinus's Avatar
 
Join Date: Jan 2012
Location: Dover, New Hampshire, USA
Posts: 4,233 (8.87/day)
Thanks: 1,259
Thanked 1,313 Times in 975 Posts

System Specs

Instead of telling how how you're trying to solve the problem, maybe you should describe what you're doing (for the project,) and where you're at so we can have an idea about what you're trying to do rather than how you're trying to approach it.
__________________
MyHeat
Aquinus is offline  
Crunching for Team TPU
Reply With Quote
Old Dec 20, 2012, 01:33 PM   #6
okidna
200 Posts
 
okidna's Avatar
 
Join Date: Jan 2012
Location: Indonesia
Posts: 361 (0.72/day)
Thanks: 496
Thanked 271 Times in 151 Posts

System Specs

Quote:
Originally Posted by Wozzer View Post
Hi all,

I'm trying to create an if statement that:
"When a user input equals ANY of the the data in column "userID", Then execute"

I'm aware you can use the .equals operation but not sure how you would link it to the column within the database.

Wozzer
I only understand a little bit Java, so pardon me If I'm wrong, userInput is the user input (string variable), users is your table name, and I assume you already have your connection to your database :

PHP Code:
SQLConnection.setURL("insert_connection_string_here");
con=SQLConnection.getNewConnection();
Statement stmt con.createStatement();

// execute your SQL statement here, for example :
String sqlCommand "SELECT userID FROM users WHERE userID='"+userInput+"'";  
ResultSet rs stmt.executeQuery(sqlCommand);

bool hasRows false;
while(
rs.next()){
  
hasRows true;
  
// put the codes that you want to execute here
}

if(!
hasRows)
{
  
// do stuff when no rows/match present.

Please CMIIW.

And

Quote:
Originally Posted by Aquinus View Post
Instead of telling how how you're trying to solve the problem, maybe you should describe what you're doing (for the project,) and where you're at so we can have an idea about what you're trying to do rather than how you're trying to approach it.
+1.
okidna is offline  
Crunching for Team TPU
Reply With Quote
Old Dec 20, 2012, 02:02 PM   #7
Wozzer
500 Posts
 
Join Date: Jun 2008
Location: England
Posts: 990 (0.55/day)
Thanks: 166
Thanked 76 Times in 72 Posts
Send a message via MSN to Wozzer

System Specs

Thanks for the responses. Wasn't expecting so many to reply

To clarify - I have a java program. I only want users to access the software if they are registered.

I've set up a database (and connected to it!). I want to run a query which checks if the users ID and passwords ID match. If matched, permissions will be granted and the main menu will open.

Code:
 System.out.println("MySQL Connect Example.");
        Connection conn = null;
        String url = "jdbc:mysql://localhost:3306/";
        String dbName = "watson_users";
        String driver = "com.mysql.jdbc.Driver";
        String userName = "root";
        String password = "";
        try {
            Class.forName(driver).newInstance();
            conn = DriverManager.getConnection(url + dbName, userName, password);
            System.out.println("Connected to the database");

            Statement st = (Statement) conn.createStatement();

            String query_to_update = "SELECT * FROM `user_database` WHERE CollarID='" + collarIDField.getText() + "' AND Password='" + passwordField.getText() + "'";

            System.out.println("Query: " + query_to_update);

            int val = st.executeUpdate(query_to_update);
            System.out.println("We've got to this point");

            conn.close();
        } catch (InstantiationException | IllegalAccessException | SQLException ex) {
            Logger.getLogger(MetaDataExtractor.class.getName()).log(Level.SEVERE, null, ex);
            JOptionPane.showMessageDialog(this, "Can't connect to Database");
        } catch (ClassNotFoundException ex) {
            JOptionPane.showMessageDialog(this, "Can't connect to Database");
        }
Error:
Code:
java.sql.SQLException: Can not issue SELECT via executeUpdate()
Edit: Just noticed .executeUpdate only allows for INSERT, DELETE, and UPDATE.

Last edited by Wozzer; Dec 20, 2012 at 02:21 PM.
Wozzer is offline  
Reply With Quote
Old Dec 20, 2012, 02:39 PM   #8
Jizzler
2000 Posts
 
Jizzler's Avatar
 
Join Date: Aug 2007
Location: Geneva, FL, USA
Posts: 3,010 (1.43/day)
Thanks: 567
Thanked 606 Times in 487 Posts

System Specs

Important is that you clean those input strings.

Code:
PreparedStatement selectUser = conn.prepareStatement("SELECT * FROM user_database WHERE CollarID= ? AND Password = ?");

selectUser.setString(1, collarIDField.getText());
selectUser.setString(2, passwordField.getText());

if ( selectUser.execute() ) {
	//get the data, welcome the user
} else {
	//tell them to get it right or bugger off
}
Now when I claim that my ID is "1; DROP TABLE user_database;" I'm not going to ruin your day.


(hope I got it right, only write JAVA when I have to).
Jizzler is offline  
Reply With Quote
The Following User Says Thank You to Jizzler For This Useful Post:
Old Dec 20, 2012, 02:40 PM   #9
okidna
200 Posts
 
okidna's Avatar
 
Join Date: Jan 2012
Location: Indonesia
Posts: 361 (0.72/day)
Thanks: 496
Thanked 271 Times in 151 Posts

System Specs

Quote:
Originally Posted by Wozzer View Post
Edit: Just noticed .executeUpdate only allows for INSERT, DELETE, and UPDATE.
.executeUpdate() generally used for altering the databases. DROP TABLE or DATABASE, INSERT into TABLE, UPDATE TABLE, DELETE from TABLE statements will be used in this.

Use .executeQuery() or .execute() if you want to use SELECT. Remember the output is in the form of ResultSet.
okidna is offline  
Crunching for Team TPU
Reply With Quote
The Following User Says Thank You to okidna For This Useful Post:
Old Dec 20, 2012, 05:25 PM   #10
Wozzer
500 Posts
 
Join Date: Jun 2008
Location: England
Posts: 990 (0.55/day)
Thanks: 166
Thanked 76 Times in 72 Posts
Send a message via MSN to Wozzer

System Specs

Quote:
Originally Posted by Jizzler View Post
Important is that you clean those input strings.

Code:
PreparedStatement selectUser = conn.prepareStatement("SELECT * FROM user_database WHERE CollarID= ? AND Password = ?");

selectUser.setString(1, collarIDField.getText());
selectUser.setString(2, passwordField.getText());

if ( selectUser.execute() ) {
	//get the data, welcome the user
} else {
	//tell them to get it right or bugger off
}
Now when I claim that my ID is "1; DROP TABLE user_database;" I'm not going to ruin your day.


(hope I got it right, only write JAVA when I have to).
Just to clarify - Is that to prevent SQL injection attacks, etc?

Thanks for all the replies. Working on it now.

Edit - Login seems to be working although I don't think it's checking the actual results as it's opening my program based on .execute
(IE - If anything is executed, do this).

How can I check the returned results from my database?
Thanks all!

Last edited by Wozzer; Dec 20, 2012 at 06:40 PM.
Wozzer is offline  
Reply With Quote
Old Dec 20, 2012, 06:46 PM   #11
Jizzler
2000 Posts
 
Jizzler's Avatar
 
Join Date: Aug 2007
Location: Geneva, FL, USA
Posts: 3,010 (1.43/day)
Thanks: 567
Thanked 606 Times in 487 Posts

System Specs

Yup. http://xkcd.com/327/

Even if the likelihood of revealing information is near impossible, it's still a best practice. No matter what they put in it's either going to work else not - with the "not" being handled the same way every time. Otherwise, one may be able to cause an error that reveals something of interest.


-Edit-

excute(), getResultSet():

http://docs.oracle.com/javase/1.4.2/....html#execute()

Last edited by Jizzler; Dec 20, 2012 at 06:51 PM.
Jizzler is offline  
Reply With Quote
The Following 2 Users Say Thank You to Jizzler For This Useful Post:
Old Dec 20, 2012, 07:01 PM   #12
Wozzer
500 Posts
 
Join Date: Jun 2008
Location: England
Posts: 990 (0.55/day)
Thanks: 166
Thanked 76 Times in 72 Posts
Send a message via MSN to Wozzer

System Specs

Quote:
Originally Posted by Jizzler View Post
Yup. http://xkcd.com/327/

Even if the likelihood of revealing information is near impossible, it's still a best practice. No matter what they put in it's either going to work else not - with the "not" being handled the same way every time. Otherwise, one may be able to cause an error that reveals something of interest.


-Edit-

excute(), getResultSet():

http://docs.oracle.com/javase/1.4.2/....html#execute()
Will read up on the documentation later tonight. Many thanks Jizzler.

lol @ the cartoon strip.
Wozzer is offline  
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Audio dropouts during file operations Hood Audio, Video & Home Theater 0 Dec 10, 2012 05:14 PM
Western Digital Announces Q2 Results and Operations Update Cristian_25H News 2 Jan 24, 2012 01:20 AM
EVGA is not closing its European operations r9 Motherboards & Memory 6 Jun 1, 2010 10:53 PM
NEC Electronics and Renesas to Integrate Business Operations malware News 2 Apr 29, 2009 10:05 PM
NVIDIA Names Debora Shoquist Senior VP of Operations malware News 3 Oct 1, 2007 06:19 AM


All times are GMT. The time now is 05:30 PM.


Powered by vBulletin® Version 3.8.6
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
no new posts