I want to fetch last value in a column in a row in a derby db table. Can someone help me?
SELECT *
FROM dbo.rawImport
WHERE number= ( select max(number)
from dbo.rawImport)
Try this one if you are trying to get last id (numeric value)
SELECT MAX(COLUMN_NAME)
FROM TABLE_NAME
thats it.
And if you want to get whole data of last row then call getRowCount() method and select the record at that reference. check out code example.
getRowCount() method:
public int getRowCount(String tableName) {
int count = 0;
try {
res = st.executeQuery("SELECT COUNT(*) FROM " + tableName);
while (res.next()) {
count = res.getInt(1);
}
} catch (SQLException ex) {
ex.printStackTrace();
JOptionPane.showMessageDialog(null, "Table not found record getting");
}
return count;
}
It will return an int value so use it as
SELECT * FROM TABLE_NAME WHERE ID = getRowCount(TABLE_NAME);
And the result is your last record.
Related
private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
String del_user = JOptionPane.showInputDialog(null,"Enter the Username To be deleted :" ,"Delete User !", JOptionPane.WARNING_MESSAGE);
try {
stmt = con.createStatement();
String quer = "delete from Udet where username='"+del_user+"'";
rs=stmt.executeQuery(quer);
int count=0;
while(rs.next())
{
count++;
}
if(count==1)
{
JOptionPane.showMessageDialog(null,"The user has been deleted");
}
else
{
JOptionPane.showMessageDialog(null,"No such User Exists");
}
} catch(Exception e){}
}
THE QUERY EXECUTES FINE ! THE RECORD GETS DELETED BUT THE LINES AFTER QUERYEXECUTION ARE NOT EXECUTING ...
the JOptionPane will work after the try block but then the value of count wont be determined...
A DELETE statement doesn't return a ResultSet and therefore you should use the method stmt.executeUpdate(quer); instead of stmt.executeQuery(quer);.
One way you can see how many results were deleted from the query is run a SELECT COUNT(*) query on the table before and after the DELETE using the executeQuery() method which will both times return a ResultSet with a single result containing how many records the table contains. After running it before and after the DELETE you can compare the 2 numbers.
The result will probably be a String but you can just use int integer = Integer.parseInt(String); to get the int from it. The code should look like this:
stmt = "SELECT COUNT(*) AS Count FROM table_name";
int initialSize = 0;
rs = stmt.executeQuery(stmt);
while(rs.next){
initialSize = Integer.parseInt(rs.getString("Count");
}
Run the DELETE query and then repeat the COUNT query passing the int into a new variable (for this answer I'll call it newSize) so the number of changes the DELETE made would be:
int changesMade = initialSize - newSize;
Here is my java code:
public int function(String name)
{
int i=0;
int id=0;
try
{
String get_id="SELECT id FROM table_name JOIN tbl_2 ON tbl_name.pk_id = tbl_2.fk_name_id where active_flag=1 and updated_at <= NOW() - INTERVAL 3 MINUTE";
Statement stmt=(Statement) conn.createStatement();
ResultSet rs = stmt.executeQuery(get_id);
while (rs.next())
{
id= rs.getInt("id");
String update="UPDATE table_name SET active_flag=NULL WHERE id="+id+"";
stmt.executeUpdate(update);
}
i=1;
}
catch(Exception e)
{
System.out.println(e);
}
return i;
}
while running this, I am getting SQL Exception:
java.sql.SQLException: Operation not allowed after ResultSet closed
Please help me to perform this operation in an alternative and simple way. I just want to update the same table with the value obtained from first SQL Query, as there is n number of values in
table and the value change dynamically, I used while loop. please help me to fix this. Thanks in advance.
Just rewrite the update sql statement to:
UPDATE table_name
SET active_flag=NULL
WHERE EXISTS
(
SELECT
NULL
FROM
table_name as tbl
JOIN tbl_2
ON tbl_name.pk_id = tbl_2.fk_name_id
where tbl.active_flag=1
and tbl.updated_at <= NOW() - INTERVAL 3 MINUTE
AND tbl.id=table_name.id
)
i was writing some codes. i set my Primary Key is varchar. and i want to make it autoincrement like FT001. my code was working but until it FT10 it cant increment again. i dont know the codes or query is wrong.
here is my code:
private void autoNumber(){
try{
String autonumber = "SELECT MAX(right(no_faktur,1)) FROM transaksi";
Connection con = Koneksi.getConnection();
Statement statement = (Statement) con.createStatement();
ResultSet r = statement.executeQuery(autonumber);
while (r.next()){
if (r.first() == false){
textFaktur.setText("FT001");
}else{
r.last();
int faktur = r.getInt(1)+1;
String number = String.valueOf(faktur);
int fakturlong = number.length();
for (int i = 0; i<2-fakturlong;i++){
number = "00"+number;
}
textFaktur.setText("FT"+number);
}
}
statement.close();
r.close();
}catch(SQLException e){
}
}
when value FT10 is inserted, your query (SELECT MAX(right(no_faktur,1)) FROM transaksi) is still returning 9, since you are looking for the MAX value of 1 character, which will always be 9.
You can use that column as auto increment, but you should specify the field as number not text.
Create that table as
CREATE TABLE transaksi(
no_faktur Int NOT NULL AUTO_INCREMENT,
name CHAR(30) NOT NULL,
PRIMARY KEY (no_faktur )
);
Then you don't need to manually find the last no_faktur inserted. the table itself automatically increment that field each time you insert records into the table
I think i got the answer:
Ps=con.preparestatement("select * from tablename");
Resultset rs=ps.execute query();
if(rs.last())
{
String aidv ="TAR" 1;
}
else
{
int id=RS.getRow();
id ;
aidv="TAR" id;
}
I am trying to delete a row from the database, but the control always goes into the else part below. I tried to execute the same delete statement on the database and it worked there.
I have verified all the column names and the table name are correct.
Can someone guide if I am doing anything wrong here?
Thanks.
public String delAd(String x, String y, String z){
String result = "";
int rowcount = 0;
try{
PreparedStatement ps = con.prepareStatement("DELETE FROM dbname.tablename WHERE iname = ? AND idesc = ? AND seller = ?");
ps.setString(1,x);
ps.setString(2,y);
ps.setString(3,z);
rowcount = ps.executeUpdate();
if(rowcount > 0){
result = "true";
System.out.println("Delete Successful");
}else{
result = "false: Value could not be deleted from the database";
}
}catch(Exception e){
e.printStackTrace();
}
return result;
}
Check in your table if there is any row which satifies the query. simple way to test is do a select statement with the values live below.
SELECT * FROM TABLENAME WHERE INAME='WATEVER' AND IDESC='WATEVER';
it is obvious from the output that there are no rows that are satisfying your query thats why control is jumping to else.
How I can count how many times a value appear in a table using jdbc? I have 200 possible values and 4 columns for records in the table.
May be by
Select count(*) from table where item = 'value';
If you want count for all 200 values then you can try:
select item,count(*) from table group by item;
Demo code:--
try {
java.sql.Statement s = conn.createStatement();
java.sql.ResultSet r = s.executeQuery("select item,count(*) from table group by item;");
while (r.next()) {
System.out.println(r.getString(1) + " "
+ r.getString(2));
}
} catch (Exception e) {
System.out.println(e);
System.exit(0);
}