I'm trying to update some rows using ResultSet. Basically the syntax works as the following:
ResultSet rs = stmt.executeQuery("SELECT * FROM TABLE");
while (rs.next()) {
rs.updateString("something", "something_else");
rs.updateRow();
}
This works fine with simple queries. However, the query I'm trying to execute contains where clause.
For example: SELECT * FROM TABLE WHERE LASTNAME = "A" AND FIRSTNAME = "B"
One solution I can think of is to extract column values from resultset, and then add if-else statement.
For example:
ResultSet rs = stmt.executeQuery("SELECT * FROM TABLE");
while (rs.next()) {
String lastname = rs.getString("lastname");
String firstname = rs.getString("firstname");
if ("A".equals(lastname) && "B".equals(firstname)){
rs.updateString("something", "something_else");
rs.updateRow();
}
}
Is there anything else I can do to solve this issue?
Try this code,
ResultSet rs = stmt.executeQuery("SELECT * FROM TABLE");
while (uprs.next()) {
String lastname = rs.getString("lastname");
String firstname = rs.getString("firstname");
if (lastname.equals("A") && firstname.equals("B")){
rs.updateString("something", "something_else");
rs.updateRow();
}
Related
UPDATE
i changed sql query (just test it and it works on sql but still not working with java dont no why :(
Hi how to add get result on jLabel with this code? and is it possible to show results on jTable?
private void searchTeacherActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
String sql = ("select student_ID, firstName, afterName FROM student JOIN studentHom ON course = studentHom_ID WHERE prefect = ?
try {
pst = connection.prepareStatement(sql);
pst.setString(1, larareSoka.getText());
pst.execute();
} catch (Exception e) {
JOptionPane.showMessageDialog(rootPane, e);
}
}
First don't use (?) you can just use ?:
select ... where course.courseStart = ? and course.corse.end = ?
Second you have to set parametters to your query in your case you have tow, so you have to use :
pst.setString(1, value_of_courseStart);
pst.setString(2, value_of_corse.end);
Third to get results you have to use ResultSet like this :
ResultSet result = preparedStatement.executeQuery();
if (result.next()) {
String firstname = result.getString(1);
//----------------------------------^
//...same for the other columns
}
or you can use the name of column like this:
ResultSet result = preparedStatement.executeQuery();
if (result.next()) {
String firstname = result.getString("firstName");
//--------------------------------------^^
//...same for the other columns
}
Note
If you want to get multiple result you ca use while instead of if.
Your query is a little wired why you are using :
course.courseStart = (?) and course.corse.end = (?)
//no point---^ ^------Why this point here
Did you mean course.courseStart = ? and course.corseEnd = ?
ResultSet rs = pst.executeQuery();
String firstname = rs.getString("firstname");
..
jLable.setText(firstname);
...You need to read the data from the resultset
Hi I am new here and I need some help in populating other jcomboboxes. All I wanted is that if I will select Last name from the first combobox, the other combobox will be populated by First names of patients/persons that has same Lastnames same as the Middlename. I hope you can help me
here's the pic:sample
As of now this is the code that I got to get values from database and put it on the first jcombobox:
public void lastname(){
try{
Connection con = (Connection)DriverManager.getConnection("jdbc:mysql://localhost/javaclinic", "root","");
String sql1 = "select * from patient";
PreparedStatement pst1 = con.prepareStatement(sql1);
ResultSet rs1 = pst1.executeQuery();
while(rs1.next()){
lastn.addItem(rs1.getString("PLastname"));
}
}
catch(SQLException e){
}
}
Modify the sql to search database , if user enter any value like:
String sql1 = "SELECT * FROM patient";
if(lastName.equals(""))
{
sql1 +=" WHERE PLastName = '" + lastName + "'";
}
private void Update_table(){
try {
String sql= "select Firstname, Lastname,ID_number from regmembers";
ps = conn.prepareStatement(sql);
rs = ps.executeQuery();
jTable_regMembers.setModel(DbUtils.resultSetToTableModel(rs));
jTable_regMembers.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
jTable_regMembers.getTableHeader().setReorderingAllowed(false);
jTable_regMembers.getTableHeader().setResizingAllowed(false);
} catch(Exception e){
JOptionPane.showMessageDialog(null, e+"");
}
}
how can i display the data of registered members of my database and put it into jtextfield, newbie question?
iterate over the resultset object and set the values obtained
ResultSet resultSet = ps.executeQuery();
if (resultSet.next()) {
do {
// perform your logic
yourTextfield.setText(rs.getString("yourColumnName"));
} while(resultSet.next());
You should be able to execute a simple query and use the value in the Result set to set your textfield.
String sql = "Select count(*) from regmembers";
ps = conn.prepareStatement(sql);
rs = ps.executeQuery();
textfield.setText(rs.getInt(1));
Hope this works the way you wanted it to
I have a static query as Select * from Emp where Empid in (?) and I have that value of (?). I am not able to place that value. Please guide me. Let me know, I f anything else is required.
Try this java code:
public boolean yourMethod(String yuorValue) {
String sql = "select * from user where fieldName = ?";
PreparedStatement stmt = connection.prepareStatement(sql);
stmt.setString(1, yuorValue);//fieldvalue(1), Your passing value
ResultSet rs = stmt.executeQuery();
return rs.next();
}
I have a database with the following layout
databasename:macfast
table name:users
columns
id,
user_name,
password,
fullName
I want to retrieve all the values from the column user_name and check each values with another one string which is already retrieved from a TEXTFIELD.But I can't(NullpointerException). Please help me.
public void deleteFclty() {
PreparedStatement stmt = null;
ResultSet rs = null;
String username=removeText.getText();
String qry = "SELECT user_name From users ";
try {
stmt = (PreparedStatement) connection.prepareStatement(qry);
rs = stmt.executeQuery();
while (rs.next()) {
String check=(rs.getString("user_name"));
System.out.println(check);
if(check.equals(username)){
Util.showErrorMessageDialog("EQUAL");
}else{
Util.showErrorMessageDialog("NOT EQUAL");
}
}
}catch (SQLException ex) {
Logger.getLogger(RemoveFaculty.class.getName()).log(Level.SEVERE, null, ex);
}
}
Problem is with the prepared statement (you don't put id into statement, which should be there instead of question mark).
Also I would recommend to do condition as "username.equals(check)", that can prevent null pointer exception.
"SELECT user_name From users where id=?"
This query has a parameter and you're not setting any value to it. Use PreparedStatement#setInt() or similar to set it, e.g.:
stmt.setInt(1, 1);
The problem is with PreparedStatement as you are using Question mark ? in your query for that you have to set the Id value.
String qry = "SELECT user_name From users where id=?";
stmt = (PreparedStatement) connection.prepareStatement(qry);
stmt.setInt(1,1001);
rs = stmt.executeQuery();
For your question in comment below:
List<String> values = new ArrayList();
while (rs.next()) {
values.add(0,rs.getString(<>))
}
// here do whatever you want to do...
// for example
for ( String value: values )
{
// check if value == TEXTFIELD
// if true then do something..
// else don't do anything
}