Writing sql query that relate two screens [closed] - java

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
Good morning . I have a question . I have two screens in my program one is check in that will have special field (check no).The other screen is check out. It also have check no . I want to write a query that relate the check in with check out by using the check no and save it in one row in database . i wrote :
public void actionPerformed(ActionEvent arg0) {
while(textField.getText()==Securityoffcheck.getnumber())
{
try{
String timeStamp = new SimpleDateFormat("dd.MM.yyyy HH.mm.ss").format(new Date());
String sql = "INSERT INTO RECORD(CHECK_OUT)VALUES(?)";
PreparedStatement pst=con.prepareStatement(sql);
pst.setString(12, timeStamp);
pst.execute();
}
catch (Exception e) {
JOptionPane.showMessageDialog(null,e);
}
}
}
});
but nothing happen .

Check your parameter index in pst.setString(12, ...). For your SQL statement INSERT INTO RECORD(CHECK_OUT) VALUES(?) you should set the parameter index to 1.

Related

Update MySQL table row [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 12 months ago.
Improve this question
Hi, I wanted to add a Password change function to my programm. For that i need to update the password column of the specific user. I researched about it and fount this and tried it:
UPDATE uprtable SET password = '"+password+"' WHERE username = '"+username+"';
I also found out that i need to select the row after it. So I did this too.
SELECT username, password FROM uprtable WHERE username '"+username+"';
The Problem is it doesnt update it and I dont get any Errors where I could get any Info about what is wrong.
Here is the Code direct from the Class:
public void changepw() throws Exception{
Connection con = getConnection();
String passwordID1 = String.valueOf((passwordfield.getPassword()));
String passwordID2 = String.valueOf((passwordfield2.getPassword()));
String passwordID3 = String.valueOf((passwordfield3.getPassword()));
if (passwordID1.equals(resultSet.getString("password"))) {
if (passwordID2.equals(passwordID3)) {
String userID = resultSet.getString("username");
try {
PreparedStatement statementpwchange = con.prepareStatement("UPDATE uprtable SET password = '"+passwordID2+"' WHERE username = '"+userID+"'");
PreparedStatement statementpwchangeconfirm = con.prepareStatement("SELECT username, password, rank FROM uprtable WHERE username = '"+userID+"'");
System.out.println("Updated");
frame.dispose();
}
catch(Exception e){
System.out.println(e);
}
}
else {
messageLabel.setForeground(Color.RED);
messageLabel.setText("The Passwords do not match");
}
}
}
If you need more Information about the Code, feel free to ask.
Thank you in advance!
You have prepared the statement but have not executed the statement. For reference: INSERT (as well as UPDATE and DELETE) statements should use executeUpdate().
SELECT statements should use executeQuery().

Why is my SQL Query only selecting the last entry in a column [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 1 year ago.
Improve this question
I am trying to connect to SQL Server from an Android app and after days of struggling, I have finally figured it out. Now I have run into a much simpler problem. My select query only selects the last value from the table. The table only contains string values for now as it is a test table. Here is what I wrote in my code.
public void sqlButton(View view) {
if (connection != null) {
Statement statement = null;
try {
statement = connection.createStatement();
ResultSet resultSet = statement.executeQuery("Select * from [dbo].[Table_1];");
while (resultSet.next()) {
textView.setText(resultSet.getString(1));
}
} catch (SQLException e) {
e.printStackTrace();
}
}
else {
textView.setText("Connection is null");
}
}
Your problem is probably that you're overwriting your output text each time.
Write this to a multiline text field, so you see all results:
textView.setText(textView.getText() + "\n" + resultSet.getString(1));
Actually, the select query returns all records in your table. However, the problem is in this part
while (resultSet.next()){ textView.setText(resultSet.getString(1));}
the text in the next loop will replace one in the previous. Therefore, you only see the last one.

Get data from adjoining cell - MS Access - Java [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
I'm writing a Java Swing program for a college library. This program is connected to an MS Access database.
The program has a JTextField where the "Number" has to be entered. Below the TextField, there's a JButton. After the number is entered and the button is clicked, I want the program to print out the respective "Department"
For example :
When the user types the Number "3" in the JTextField and clicks the 'Enter'
button, I want my program to print out "Computers" i.e the data from the adjoining cell.
How do I achieve this? Thanks in advance!
Start by taking a look at JDBC Database Access.
You will need a JDBC driver for MS Access, UCanAccess is reasonable popular.
You will then need to:
Load the JDBC driver
Connect to the database
Execute a SQL query which can select the data you want based on your critera
Retrieve the data from the resulting data set
For example...
try {
Class.forName("net.ucanaccess.jdbc.UcanaccessDriver");
try (Connection conn = DriverManager.getConnection("jdbc:ucanaccess://<mdb or accdb file path>", user, password)) {
try (PreparedStatement stmt = conn.prepareStatement("SELECT department FROM Table1 WHERE ID = ?")) {
stmt.setInt(1, 3); // Use a variable for the ID
try (ResultSet rs = stmt.executeQuery()) {
// We're only expecting a single row...
if (rs.next()) {
String department = rs.getString(1);
}
}
}
} catch (SQLException exp) {
exp.printStackTrace();
}
} catch (ClassNotFoundException exp) {
exp.printStackTrace();
}
You might also want to take a look at a SQL Tutorial

How to display a message in JPanel from DATABASE? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I'm making a java Project that going to display a Request "something :)" from database..
I've done making a database and a request form.
My problem is I don't have an idea how to display the message on my JPANEL.
Here's the code.
try{
Class.forName("com.mysql.jdbc.Driver");
conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/webservice","root","");
String sql="Insert into request(PurposeofTrip,DestinationS,VehicleModel,RequestingDept,DriverName,OvernightUse) values (?,?,?,?,?,?)";
pst=conn.prepareStatement(sql);
pst.setString(1, txt_trip.getText());
pst.setString(2, txt_dest.getText());
String value=combo_model.getSelectedItem().toString();
pst.setString(3, value);
pst.setString(4, txt_requ.getText());
pst.setString(5, txt_dname.getText());
String value2=combo_night.getSelectedItem().toString();
pst.setString(6, value2);
pst.execute();
JOptionPane.showMessageDialog(null, "Request Send");
}catch(Exception e){
...
}
How to display a message in JPanel from DATABASE?
Since you're inserting into the database, it looks like you want to show the status of the insertion. What I would do is just use a JLabel. No need for a JPanel in a JPanel you need to actually paint the message yourself. You can do something like this. Keep in mind that .excuteUpdate() return an int which is the number of rows affected.
int result = pst.executeUpdate();
if (result == 0) {
statusLabel.setForeground(Color.RED);
statusLabel.setText("Error Inserting record");
} else {
statusLabel.setForeground(Color.GREEN);
statusLabel.setText("Insert Successful");
}
statusLabel being a JLabel that you have added somewhere to your GUI previously

can i write my Sql query in this way [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 8 years ago.
Improve this question
I am wondering can i insert table name in this format
String update = "UPDATE ? SET Status = ? WHERE Name = ?";
stmt.setString(1,tableName);
stmt.setString(2,status);
stmt.setString(3,name);
same for insert and delete statements?
No.
The reason you place question marks in the query (aside from protection against SQL injection) is so that the database can prepare the statement once and use that prepared statement with different parameters. It wouldn't be able to prepare a statement if it doesn't know what table(s) you are using.
The short answer is no. But you can do it this way:
String update = "UPDATE " + tableName + " SET Status = ? WHERE Name = ?";
...
stmt.setString(1,status);
stmt.setString(2,name);
Be aware of the SQL injection though. Be sure your tableName comes from the secure source.
Normally, you would do this as shown below...
String sql = "UPDATE " + tableName " SET Status = ? WHERE Name = ?";
PreparedStatement stmt = null;
try {
stmt = connection.prepareStatement(sql);
stmt.setString(1, status);
stmt.setString(2, name);
stmt.executeUpdate();
} finally {
if (stmt != null) {
stmt.close();
}
}
No you cann't do this because you are definitely using a prepared statement. The reason you can not do this is PreparedStatement is pre-compiled so it needs the table which you are modifing (its data using DML) or structurally (using DDL). If you don't mention the table table name how the statement is going to be pre-compiled?
If you want you can use dynamic SQL but in that case you don't have to use PreparedStatement you can use it using a simpler implementation Statement.
Hope this is helpful !!

Categories

Resources