Whats wrong with this sql code? Syntax should be ok or not?
public void setArtikelIdRegalfach(ArrayList <Integer> arikel_ID, int Regal_ID) { // Setzt die Regalfach ArtikelID
try {
String query = "UPDATE WARENLISTE SET ARTIKEL_ID Values (" + arikel_ID + ") WHERE Regal_ID = " + Regal_ID;
st.executeUpdate(query);
}
catch(Exception e) {
System.out.println(e);
}
}
Update statements in SQL do not take a VALUES clause. On top of this, you should be using a prepared statement. Here is a corrected version:
String sql = "UPDATE WARENLISTE SET ARTIKEL_ID = ? WHERE Regal_ID = ?";
try (Connection conn = DriverManager.getConnection("...");
PreparedStatement ps = conn.prepareStatement(sql)) {
ps.setInt(1, arikel_ID);
ps.setInt(2, Regal_ID);
ps.executeUpdate();
}
catch (SQLException e) {
System.err.format("SQL State: %s\n%s", e.getSQLState(), e.getMessage());
}
catch (Exception e) {
e.printStackTrace();
}
Related
When I am trying to get values from database the result set is giving me false and the values are null.
public boolean login()
{
try
{
String p="SELECT * FROM DEMONEW WHERE EMAIL=? AND PASSWORD=?";
pstmt = con.prepareStatement(p);
pstmt.setString(1,EMAIL);
pstmt.setString(2,PASSWORD);
System.out.println("email :"+EMAIL + "and" +"password:"+PASSWORD);
ResultSet res = pstmt.executeQuery();
System.out.println("res :"+ res.next());
while(res.next())
{
NAME = res.getString(1);
System.out.println("NAME FROM MODEL :" +NAME);
AGE=res.getString(2);
System.out.println("age FROM MODEL :" +AGE);
LOCATION=res.getString(3);
System.out.println("email FROM MODEL :" +EMAIL);
EMAIL=res.getString(4);
System.out.println("location FROM MODEL :" +LOCATION);
return true;
}
}
catch (SQLException e)
{
e.printStackTrace();
}
return false;
}
I am expecting the values in database but getting null.
Hello guys whenever I try to delete a row using a radiobutton, I get both try and catch messages, when it is supposed to be just 1 of them, I have this code
Here's my calling button method
if(request.getParameter("btnEliminar") != null)
{
String value;
int codParse;
OC_DAO objDAO = new OC_DAO();
valor = request.getParameter("rbSel");
codParse = Integer.parseInt(valor);
objDAO.DeleteRow(codParse);
}
Here's my java code
public void DeleteRow(int codDet)
{
try
{
cn = Conexion.getConexion();
pt = cn.prepareStatement("DELETE "
+ "FROM detalleProd "
+ "WHERE codDet = ?");
pt.setInt(1, codDet);
pt.executeUpdate();
System.out.println("ROW DELETED ON CODDET: " + codDet);
rs.close();
pt.close();
cn.close();
}
catch(Exception exc)
{
System.out.println("Error while deleting");
System.out.println(exc.toString());
}
}
And here's my log
Información: ROW DELETED ON CODDET: 48
Información: Error while deleting
Información: java.lang.NullPointerException
The reason is due to rs.close();, you have not set value of rs,it's null and can not be closed,you just need to remove this line of code.
Your code seems very strange,I do not have see where you declare rs,it will compile error in your IDE.
can anyone help with retrieving value from ms access db
the problem with auto number, I want to show it in Jtextfield when I click over a record into the table..
I asked before this question, but no one answer me.
Please help me with this problem as soon as posible.
try {
int row = table_BP.getSelectedRow();
String Table_Clicked = table_BP.getModel().getValueAt(row, 0).toString();
String sql = "Select * from Blood_pressure where Patient_Id='" + Table_Clicked + "'";
ps = con.prepareStatement(sql);
rs = ps.executeQuery();
if (rs.next()) {
try {
String add1 = rs.getString("BP_Test_ID").toString();
txt_testNo.setText(add1);
String add2 = rs.getString("Patient_Id");
txt_PID.setText(add2);
String add3 = rs.getString("Date_Of_test");
DC_Date.setDateFormatString(add3);
String add4 = rs.getString("Blood_presure");
txt_BP.setText(add4);
String add5 = rs.getString("Remarks");
TA_Remarks.setText(add5);
} catch (Exception e) {
JOptionPane.showMessageDialog(null, "sdsdsd");
}
}
} catch (Exception e) {
JOptionPane.showMessageDialog(null, "nnnnnnnnnnnnnnnnnnnn");
}
I am trying to pass value from Jcombobox and I used Like Operator in order to retrieve data from Database ..
I need to retrieve all data related to a chosen month from combobox
String sql = "SELECT * FROM Treatment WHERE Treatment_Date LIKE '?%'";
try {
ps = con.prepareStatement(sql);
ps.setString(1, cmb.getSelectedItem().toString());
rs = ps.executeQuery();
table_PatuentReg.setModel(DbUtils.resultSetToTableModel(rs));
} catch (Exception e) {
JOptionPane.showMessageDialog(null, e.getMessage());
}
String sql = "SELECT * FROM Treatment WHERE Treatment_Date LIKE '"+cmb.getText().toString()+"%'";
Use this format.
this will help more...
You need to change the query and add the % in the prepared statement value where we are setting the value for string.
String sql = "SELECT * FROM Treatment WHERE Treatment_Date LIKE ?";
try {
ps = con.prepareStatement(sql);
ps.setString(1, cmb.getSelectedItem().toString()+"?");
rs = ps.executeQuery();
table_PatuentReg.setModel(DbUtils.resultSetToTableModel(rs));
} catch (Exception e) {
JOptionPane.showMessageDialog(null, e.getMessage());
}
Try putting it directly in the ? mark sign, like so
String sql = "SELECT * FROM Treatment WHERE Treatment_Date LIKE '"+cmb.getSelectedItem().toString()+"%'";
try {
ps = con.prepareStatement(sql);
rs = ps.executeQuery();
table_PatuentReg.setModel(DbUtils.resultSetToTableModel(rs));
} catch (Exception e) {
JOptionPane.showMessageDialog(null, e.getMessage());
}
I'm trying to run two SQL statements (MSSQL 2005), asynchronously in a background worker. However, when I call the EndExecuteNonQuery method on the first SqlCommand I get a 'SQL syntax error near' error.
Here is my code:
try
{
SqlCommand sqlCmd = uow.DataLayer.CreateCommand() as SqlCommand;
sqlCmd.CommandText = "DELETE FROM dbo.EligibilityRecordKeyValue WHERE EligibilityRecord IN " +
"(SELECT EligibilityRecord FROM dbo.EligibilityRecord WHERE Organization = '" + map.Organization.Oid + "')";
IAsyncResult result = sqlCmd.BeginExecuteNonQuery();
while (!result.IsCompleted)
{
worker.ReportProgress(0, "Deleting existing record keys");
System.Threading.Thread.Sleep(200);
}
count = sqlCmd.EndExecuteNonQuery(result);
}
catch (SqlException ex)
{
}
catch (InvalidOperationException ex)
{
}
finally
{
worker.ReportProgress(2, String.Format("Existing {0} records keys deleted.", count));
}
try
{
SqlCommand sqlCmd = uow.DataLayer.CreateCommand() as SqlCommand;
sqlCmd.CommandText = "DELETE FROM dbo.EligibilityRecord WHERE Organization = '" + map.Organization.Oid + "'";
IAsyncResult result = sqlCmd.BeginExecuteNonQuery();
while (!result.IsCompleted)
{
worker.ReportProgress(0, "Deleting existing records");
System.Threading.Thread.Sleep(200);
}
count = sqlCmd.EndExecuteNonQuery(result);
}
catch (SqlException ex)
{
}
catch (InvalidOperationException ex)
{
}
finally
{
worker.ReportProgress(5, String.Format("Existing {0} records deleted.", count));
}
It fails on the first count = sqlCmd.EndExecuteNonQuery(result);
Ok, adding WAITFOR DELAY's to both SQL commands seems to have resolved the issue.
sqlCmd.CommandText = String.Format("WAITFOR DELAY '00:00:05'; DELETE FROM dbo.EligibilityRecordKeyValue WHERE EligibilityRecord IN " +
"(SELECT EligibilityRecord FROM dbo.EligibilityRecord WHERE Organization = '{0}')", map.Organization.Oid);
sqlCmd.CommandText = String.Format("WAITFOR DELAY '00:00:05'; DELETE FROM dbo.EligibilityRecord WHERE Organization = '{0}'", map.Organization.Oid);
Anyone know why this happens?