![]() |
|
|
#1 |
![]() Join Date: Jun 2008
Location: England
Posts: 990 (0.55/day)
Thanks: 166
Thanked 76 Times in 72 Posts
|
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 |
|
|
|
|
|
#2 |
|
Moderato®™
Join Date: Apr 2009
Location: Statesville, NC
Posts: 3,666 (2.45/day)
Thanks: 4,326
Thanked 2,322 Times in 1,153 Posts
|
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 |
|
|
|
|
|
#3 | |
![]() Join Date: Feb 2008
Location: Joplin, Mo
Posts: 4,543 (2.37/day)
Thanks: 175
Thanked 691 Times in 557 Posts
|
Quote:
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 |
|
|
|
|
|
|
#4 | |
|
Moderato®™
Join Date: Apr 2009
Location: Statesville, NC
Posts: 3,666 (2.45/day)
Thanks: 4,326
Thanked 2,322 Times in 1,153 Posts
|
Quote:
![]() 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. |
|
|
|
|
| The Following User Says Thank You to Mindweaver For This Useful Post: |
|
|
#5 |
![]() Join Date: Jan 2012
Location: Dover, New Hampshire, USA
Posts: 4,263 (8.86/day)
Thanks: 1,276
Thanked 1,329 Times in 986 Posts
|
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 |
|
|
|
|
|
#6 | |
![]() Join Date: Jan 2012
Location: Indonesia
Posts: 362 (0.71/day)
Thanks: 496
Thanked 271 Times in 151 Posts
|
Quote:
PHP Code:
And +1. |
|
|
|
|
|
|
#7 |
![]() Join Date: Jun 2008
Location: England
Posts: 990 (0.55/day)
Thanks: 166
Thanked 76 Times in 72 Posts
|
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");
}
Code:
java.sql.SQLException: Can not issue SELECT via executeUpdate() Last edited by Wozzer; Dec 20, 2012 at 02:21 PM. |
|
|
|
|
|
#8 |
![]() Join Date: Aug 2007
Location: Geneva, FL, USA
Posts: 3,010 (1.42/day)
Thanks: 567
Thanked 606 Times in 487 Posts
|
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
}
![]() (hope I got it right, only write JAVA when I have to). |
|
|
|
| The Following User Says Thank You to Jizzler For This Useful Post: |
|
|
#9 | |
![]() Join Date: Jan 2012
Location: Indonesia
Posts: 362 (0.71/day)
Thanks: 496
Thanked 271 Times in 151 Posts
|
Quote:
Use .executeQuery() or .execute() if you want to use SELECT. Remember the output is in the form of ResultSet. |
|
|
|
|
| The Following User Says Thank You to okidna For This Useful Post: |
|
|
#10 | |
![]() Join Date: Jun 2008
Location: England
Posts: 990 (0.55/day)
Thanks: 166
Thanked 76 Times in 72 Posts
|
Quote:
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. |
|
|
|
|
|
|
#11 |
![]() Join Date: Aug 2007
Location: Geneva, FL, USA
Posts: 3,010 (1.42/day)
Thanks: 567
Thanked 606 Times in 487 Posts
|
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. |
|
|
|
|
|
#12 | |
![]() Join Date: Jun 2008
Location: England
Posts: 990 (0.55/day)
Thanks: 166
Thanked 76 Times in 72 Posts
|
Quote:
lol @ the cartoon strip.
|
|
|
|
|
![]() |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
|
|
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 |