I'm fairly new to Java and I was wondering how I could get all of the columns of a returned via MySQL query. For instance,
SELECT * FROM `login`
And then I had this code to return the results:
while (rs.next()) {
for (byte i = 1; i < 10; i++) {
result = result+" "+rs.getString(i);
}
}
I want 10 in the for loop to be the maximum number of columns because of dynamic MySQL queries. Is there a simple way to do this? Thanks.
Although, as duffymo suggested, it's better to spell out the columns you want to receive in the query, you can use ResultSet metadata to fetch the number of columns returned:
ResultSetMetaData rsmd = rs.getMetaData();
int numberOfColumns =rsmd.getColumnCount();
while (rs.next()) {
for (byte i = 1; i <= numberOfColumns; i++) {
result = result+" "+rs.getString(i);
}
}
Yes, you'd limit the fields that are returned. That's a SQL issue, not a Java issue. Change your query to something like this:
select this, that, theothercolumn from mytable;
Then you can use this to limit it dynamically:
rs.getMetaData().getColumnCount()
I'd recommend not using SELECT *. If you know the columns you want, spell them out. It'll make your code more robust in the face of column changes and reduce the number of bytes on the wire.
Related
I was trying to understand how PagingState works with Statement in Cassandra. I tried with a sample that inserts few 1000s of records into database and tried reading the same from DB with fetch size set to 10 and using paging state. This is working perfectly fine. Here is my sample junit code:
#Before
public void setup() {
cassandraTemplate.executeQuery("create table if not exists pagesample(a int, b int, c int, primary key(a,b))");
String insertQuery = "insert into pagesample(a,b,c) values(?,?,?)";
PreparedStatement insertStmt = cassandraTemplate.getConnection().prepareStatement(insertQuery);
for(int i=0; i < 5; i++){
for(int j=100; j<1000; j++){
cassandraTemplate.executeQuery(insertStmt, new Object[]{i, j, RandomUtils.nextInt()});
}
}
}
#Test
public void testPagination() {
String selectQuery = "select * from pagesample where a=?";
String pagingStateStr = null;
for(int run=0; run<90; run++){
ResultSet resultSet = selectRows(selectQuery, 10, pagingStateStr, 1);
int fetchedCount = resultSet.getAvailableWithoutFetching();
System.out.println(run+". Fetched size: "+fetchedCount);
for(Row row : resultSet){
System.out.print(row.getInt("b")+", ");
if(--fetchedCount == 0){
break;
}
}
System.out.println();
PagingState pagingState = resultSet.getExecutionInfo().getPagingState();
pagingStateStr = pagingState.toString();
}
}
public ResultSet selectRows(String cql, int fetchSize, String pagingState, Object... bindings){
SimpleStatement simpleStatement = new SimpleStatement(cql, bindings);
statement.setFetchSize(fetchSize);
if(StringUtils.isNotEmpty(pagingState)){
statement.setPagingState(PagingState.fromString(pagingState));
}
return getSession().execute(simpleStatement);
}
When I execute this program, I see that every iteration in testPagination is exactly printing 10 records. But here is what the documentation says:
Note that setting a fetch size doesn’t mean that Cassandra will
always return the exact number of rows, it is possible that it
returns slightly more or less results.
I am not really able to understand why Cassandra will return not exactly the same number of rows as specified in fetch size. Is this the case when there is no where clause provided in the query? Will it return exact number of records when a query is constrained on a partition key? Please clarify.
From the CQL protocol specification:
Clients should also not assert that no result will have more than result_page_size results. While the current implementation always respect the exact value of result_page_size, we reserve ourselves the right to return slightly smaller or bigger pages in the future for performance reasons
So it's good practice to always rely on getAvailableWithoutFetching instead of the page size, in case Cassandra changes its implementation in the future.
public int countBookings() throws SQLException{
ResultSet rs=null;
PMDBController db=new PMDBController();
int rowCount=0;
db.getConnection();
String dbQuery="SELECT COUNT(User) AS UserCount FROM INSTRUCTORBOOKING WHERE USER ='"+instructorId+"'";
rs=db.readRequest(dbQuery);
try{
if(rs.next()){
instructorId=rs.getString("UserCount");
}
}catch(Exception e){
e.printStackTrace();
}
rs.last();
rowCount=rs.getRow();
db.terminate();
return rowCount;
}
Basically what this method is supposed to do is count the number of rows gotten from the database. However, it always returns 1 no matter what is inside. Help!
It seems you have a problem in your query. Since you only select 1 user you will always get a count of 1.
"SELECT COUNT(User) AS UserCount FROM INSTRUCTORBOOKING WHERE USER ='"+instructorId+"'"
Try removing your WHERE clause? Maybe that's not exactly what you want, but we can't see your data model from just one query.
rowCount = rs.getInt("UserCount"); instead of instructorId = rs.getString("UserCount"); would do the trick. Or in other words --- you read the number of rows but into variable instructorId.
The number of rows will always be 1. It's the count i.e. the value of that row you need to look at as your query is designed to return the count of rows and not the actual rows.
SELECT COUNT(User) AS UserCount FROM INSTRUCTORBOOKING WHERE USER ='"+instructorId+"'"
You have wrongly interpreted that the number of rows would be the count you are looking for.
I have a SQL query as shown below.
SELECT O_DEF,O_DATE,O_MOD from OBL_DEFINITVE WHERE OBL_DEFINITVE_ID =?
A collection of Ids is passed to this query and ran as Batch query. This executes for 10000
times for retriveing values from Database.(Some one else mess)
public static Map getOBLDefinitionsAsMap(Collection oblIDs)
throws java.sql.SQLException
{
Map retVal = new HashMap();
if (oblIDs != null && (!oblIDs.isEmpty()))
{
BatchStatementObject stmt = new BatchStatementObject();
stmt.setSql(SELECT O_DEF,O_DATE,O_MOD from OBL_DEFINITVE WHERE OBL_DEFINITVE_ID=?);
stmt.setParameters(
PWMUtils.convertCollectionToSubLists(taskIDs, 1));
stmt.setResultsAsArray(true);
QueryResults rows = stmt.executeBatchSelect();
int rowSize = rows.size();
for (int i = 0; i < rowSize; i++)
{
QueryResults.Row aRow = (QueryResults.Row) rows.getRow(i);
CoblDefinition ctd = new CoblDefinition(aRow);
retVal.put(aRow.getLong(0), ctd);
}
}
return retVal;
Now we had identified that if the query is modified to
add as
SELECT O_DEF,O_DATE,O_MOD from OBL_DEFINITVE WHERE OBL_DEFINITVE_ID in (???)
so that we can reduce it to 1 query.
The problem here is MSSQL server is throwing exception that
Prepared or callable statement has more than 2000 parameter
And were struck here . Can some one provide any better alternative to this
There is a maximum number of allowed parameters, let's call it n. You can do one of the following:
If you have m*n + k parameters, you can create m batches (or m+1 batches, if k is not 0). If you have 10000 parameters and 2000 is the maximum allowed parameters, you will only need 5 batches.
Another solution is to generate the query string in your application and adding your parameters as string. This way you will run your query only once. This is an obvious optimization in speed, but you'll have a query string generated in your application. You would set your where clause like this:
String myWhereClause = "where TaskID = " + taskIDs[0];
for (int i = 1; i < numberOfTaskIDs; i++)
{
myWhereClause += " or TaskID = " + taskIDs[i];
}
It looks like you are using your own wrapper around PreparedStatement and addBatch(). You are clearly reaching a limit of how many statements/parameters can be batched at once. You will need to use executeBatch (eg every 100 or 1000) statements, instead of having it build up until the limit is reached.
Edit: Based on the comment below I reread the problem. The solution: make sure you use less than 2000 parameters when building the query. If necessary, breaking it up in two or more queries as required.
This is the code I am working on:
if(connection.doDatabaseRead(findSQL))
{
ResultSet retRES = connection.getResultSet();
int i = 0;
// did we find anything
while( retRES.next() )
{
//read result from query
suiteNum.add(retRES.getString(i)); // this is the problem
i++;
//let other threads breathe
Thread.yield();
}
}
suiteNum is a string vector
When I try to add the database results to the vector the code crashes with this error.
java.sql.SQLException: Column Index out of range, 0 > 1.
I have the same piece of code working elsewhere in the program but I use real numbers like 0, 1 and 2 instead of i and it works fine.
As I do not know how many results the database request will have I need it to be dynamic but it will only work hard coded.
How can I make it work with i ?
The argument to getString is the column index, not the row index as you seem to think. The function returns the value of the given column in the current row, while next advances the cursor to the next row.
You probably mean:
suiteNum.add(retRES.getString(1));
in which case you can lose i altogether.
Java ResultSet objects are 1-indexed in this regard. The first element is at 1, not 0. See the javadoc.
EDIT: That's true too, but indeed the problem is this appears to be used as a row index! it's certainly the column.
This is your problem:
i = 0;
...
retRES.getString(i);
ResultSet.getString(i) gets a String from column number i
You want something like
while(retRes.next()) {
add(retRes.getString(1);
}
column index starts from 1
As I do not know how many results the database request will have I need it to be dynamic but it will only work hard coded. How can I make it work with i
ResultSetMetaData rsMetaData = rs.getMetaData();
int numberOfColumns = rsMetaData.getColumnCount();
See Also
ResultSetMetaData
Let your i start with 1 as specified in the API docs
if(connection.doDatabaseRead(findSQL))
{
ResultSet retRES = connection.getResultSet();
int i = 1;
// did we find anything
while( retRES.next() )
{
//read result from query
suiteNum.add(retRES.getString(i)); // this is the problem
i++;
//let other threads breathe
Thread.yield();
}
}
I'm trying to get the new rating from an UPDATE statement in java
int userID = 99;
String sql = "UPDATE table SET rating=rating+1 WHERE user_REF="+userID;
statement.executeUpdate(sql);
I can just do another SELECT statement, but isn't there a better way to retrieve the value or row while updating?
In short, No, there is no way to do this with ANSI standard SQL.
You have three options:
1) Do it in two queries - the update, then the select
2) Create a stored procedure that will execute the update and then return the select
3) Use a DB-specific extension, such as the PostgreSQL RETURNING clause
Note that options 2) and 3) are database-specific.
In MySQL:
$query1 = 'UPDATE `table` SET rating = (#rating:= rating) + 1 WHERE id = 1';
$query2 = 'select #rating';
thanks for the replies everybody, i ended up doing it like this:
int userID = 99;
String sql = "SELECT id, rating FROM table WHERE user_REF="+userID;
ResultSet rs = statement.executeQuery(sql);
rs.first();
float oldRating = rs.getFloat("rating");
float newRating = oldRating +1;
rs.updateFloat("rating", newRating);
rs.updateRow();
return newRating;
that way it (or at least seems so) does only one query to find the correct row, or am i wrong?
In PostgreSQL there is RETURNING clause
See: http://www.postgresql.org/docs/8.3/interactive/sql-update.html
Be cautious that most solutions are database dependent (Whether or not you want database independence in your application ofcourse matters).
Also one other solution you could try is to write a procedure and execute it as follows
my_package.updateAndReturnRating(refId, cursor record).
Of course this may/may not make the solution itself complicated but worth an "evaluation" atleast.