In column_1 is just numbers from 1-10 with 10 rows. I am trying to find the biggest number and take that and eventually add it to another column.
private ResultSet nrs;
nrs = stmt.executeQuery("SELECT MAX(column_1) FROM table");
while(nrs.next()){
biggestNum = nrs.getInt("column_1");
}
It keeps on throwing error 'Column "column_1" not found'.
Please help.
Replace nrs.getInt("column_1") with nrs.getInt(1)
You haven't specified a name for the value you selected, so the database will choose its own. You don't know what that name will be, so just use the column number.
Not really familiar with java but i am with sql. column_1 does not exist cause it is already aggregated. try this:
private ResultSet nrs;
nrs = stmt.executeQuery("SELECT MAX(column_1) as maxColumn1 FROM table");
while(nrs.next()){
biggestNum = nrs.getInt("maxColumn1");
}
But im pretty sure there is a better way to do this
Related
I have this on a jFrame Button:
label.setVisibility(false);
ResultSet rs = state.executeQuery("SELECT Str_Column FROM Table WHERE Str_Column = '"+ jTextField.getText() +"'");
//check for the existance
if(rs.getString("Str_Column").equals(jTextField.getText())){
label.setVisibility(true);
}
else{
new frameForSucceedInput.setVisibility(true);
}
if the if-case goes true or not always displays the succeed window and thats what i don't want to happen, it tells something about ResultSet null cause conflicts so then i tried another way:
if(state.execute("SELECT Str_Column FROM Table WHERE Str_Column = '"+ jTextField.getText() +"'")){
label.setVisibility(true);
}
else{
new frameForSucceedInput.setVisibility(true);
}
the state.execute("SQL") method says:
return true if the query contains results, return false if the query is a update or no contain results
i've tried rs.next() and a lot of things like instead of else do another if(rs==null) but does not work, please help
A quite generic and lighter way is to use the SQL count function .
select count(*) from table where str_Column = ?
You will get that way one record with one value and if the value is higher than 0 then you can show the frame.
Note:
You should use prepared statement so you can set parameters in the query (did you notice the question mark?) instead of concatenating the elements of the query.
You shouldn't mix the code related to the presentation (Swing) with the code related to the DB...but I think you will learn it later.
I have table called mpi which contains 23 columns. I have introduced the search field with button for every column where user can enter the query to fetch the records using query
query="select * from mpi where Genus ='"+genus+"'
Now I want to fetch records by giving keywords using LIKE %% but it is not working and not giving any records but if type type the full name it is working perfectly. Here is the code
String uname=request.getParameter("uname");
String full="%"+uname+"%";
dbconn=new DatabaseConnection();
conn=dbconn.setConnection();
pstmt=conn.prepareStatement("select * from mpi where Genus LIKE ?");
pstmt.setString(1, full);
res=pstmt.executeQuery
Could any one tell me where is the mistake and why I am not getting the records when I use half keyword like %keyword%.
It works (apart from the missing parentheses) and the approach with a prepared statement is entirely correct.
However I have seen a couple of code pieces like that, and always the problem lay with variables mix-up or not closing, or simple oversight. Better declare as close as possible.
try (ResultSet res = pstmt.executeQuery()) {
while (res.next()) {
..
}
} // Automatically closes res.
Also handle the life-cycle of pstmt correctly, with closing.
I was not too long ago studying java, so maybe I have a stupid question.
I have a table, where i store some info.
And i want to check in this table on availability of some information (it will be only one line), and if yes - take from this line, the information in a particular column, if no - do another thing.
I really don`t know how to do this.
My idea was something like this: at first - check the table:
SELECT * FROM tablename WHERE nickname = "kvant";
then if true, do another query with searching info.
and do this with condition if\else. but all my attempts not turn.
I hope for your help, sorry for my awry English.
Check if the data exists like this:
IF EXISTS(SELECT * FROM tablename WHERE nickname = "kvant")
BEGIN
--value is found so go ahead
END
ELSE
--value not found
BEGIN
END
One simple way is to query the database and get the result. If result!=Null {//do something} else {//Do something}
(Can use try - catch as well)
Pure java possible solution:
Try to get that value in the query. Get the ResultSet rs of that query and:
if(rs.next()) {
String value = rs.getString("value"); //Assuming the column is value and that it is a String.
//Do whatever you want with the value
} else {
//The other thing
}
If you can not take the value from the first ResultSet, do another query in the if.
write in sql like select case exists (select * from yourtable where condition) then 'dothis thing' else 'dootherthing' from 'yourtable' where 'yourcondition'
since you wanted to run some other query after you know you have some conditon exists.
I am currently trying to make an update application (Java based) that the user can go through and view the existing database entries (MySQL) and edit them if need be... I was wondering how to get the information for a specific entry (ie 12-1589 which is an example of what the ID or primary key would be) and fill in the text boxes with all of the information from said entry.... I may just need to walk away from the computer for a bit because i may be over-thinking it, but I don't know...
mainly i am unsure with the exact code that you would use to move to that entry and retrieve the data from just that entry.... I know how to step trough a database one entry at a time, but i would rather just jump to a specific row based off of an id number (such as above 12-1589) if at all possible....
I just tried this and i recieved an error.... The error was:
"Unknown column '12-1859' in 'where clause'"
con = DriverManager.getConnection(host, uName, uPass);
stmt = con.createStatement();
String sql = "SELECT * FROM Load_Sheet WHERE Load_Number = 12-1859 limit 1";
rs = stmt.executeQuery(sql);
String BC = rs.getString("BC");
If anyone could give me a hand with that is going wrong i would appreciate it...
I just started getting another error along with the other one... it is :
"illegal operation on empty result set"
Though the result sets are not empty so my guess is, is that i am missing a step somewhere....
What you need is a simple WHERE statement if i understood correctly your question.
SELECT * FROM table_name WHERE entry_col = "12-1589" LIMIT 1
LIMIT 1 is only added so that the MySql query only returns a single row.
i'm working with java to extract values of a time column in table in mysql.
the code below show the query i do send.
String query="select id,time from table where Hour(time)<=32 ";
ResultSet res = stmt.executeQuery(query);
while (res.next()) {
String id = res.getString(1);
Time tc = res.getTime("time");
System.out.println(tc);
}
the time values can be negative (-56:00:00 meaning that we surpassed a certain delay.
the problem is that I get: java.sql.SQLException: java.sql.SQLException: Bad format for Time '-05:48:49' in column 2.
thanks for your help.
If the conversion done by the ResultSet implementation does not work for negative time values then you still can read the value as a String and implement your custom method to convert the String to a Date (and vice versa):
String query="select * from table where Hour(time)<=32 ";
ResultSet res = stmt.executeQuery(query);
while (res.next()) {
String id = res.getString(1);
Time tc = convertToDate(res.getString("time"));
System.out.println(tc);
}
// ....
}
private Time convertToDate(String s) {
// implement magic here
}
As answered in your previous question you need to store and handle it as seconds which is stored as a signed integer.
The time type cannot be negative. You also cannot do math on a varchar/string and massaging it forth and back to a workable format as suggested by Andreas_D would only add unnecessary overhead. A signed integer is really the best datatype you can use for this. Use PreparedStatement#setInt() to store it and use ResultSet#getInt() to obtain it.
Maybe this answer is so late. But you can solve it just concating a string to the field you want.
I mean:
select id,concat(time,'') from table where Hour(time)<=32
Regards
Saul Hidalgo.
I think the problem is on the query itself.
When you run direcly the query [select * from table where Hour(time)<=32] does it not return you an error? I imagine the error is on the where clause [Hour(time)<=32]
The resultset does not have any information about the where clause. It just returns all the columns.
You need to check the columns return to check if you are not returning some strange type.