So i'm working on flashcards app and got stuck. I need textfield that get word from my DB, shows it on the screen and change to the next one after user press button. Problem is that when i'm using resultset to set textfield it shows only the last row from DB. I've got no idea how to get first value from DB and then change it to the next after user pres button.
That's part of my button controller:
ResultSet res = st.executeQuery("SELECT fiszka_eng FROM "+LoginHandler.GetLogin()+"");
while(res.next())
{
String textENG = res.getString("table");
TextField.setText(textENG);
//Here i want to wait till the button is pressed and after - change textENG to next row
}
Because you iterate over whole ResultSet object thus setting textENG to last value from db. Maybe add textENG to List and then on button click show next element of that list.
Related
I am trying to order new items to appear on top but it seems I am not able to reach the receiver id in order for me to add it to the query , My current query does not archive the desired result since nothing changes on the recyclerview whenever i run the app.
please help
this is my database structure
enter image description here
this is my reference
enter image description here
Here is my query
enter image description here
I have created a program that connects to a server database and retrieves information based on certain search criteria entered by the user, which is then placed into a JTable.
The user would like to be able to click on the column headers of the JTable and sort the data accordingly.
This is the code which I have but unfortunately it does nothing when clicking on the header:
pst = conn.prepareStatement(sql);
rs = pst.executeQuery();
if (rs.next()) {
tblR.setModel(DbUtils.resultSetToTableModel(rs));
tblR.setAutoCreateRowSorter(true);
} else {
JOptionPane.showMessageDialog(null, "No matches were found according to your search criteria. Please make sure all entered data is correct.");
}
Does the line tblR.setAutoCreateRowSorter(true) not work in this case?
How can i get it to work?
I found my own error.
I had not placed my JTable in a JScrollpane and therefore the method was not working.
I am pretty basic to link from Java to Database.
I link item from Database (I use MS.Access) to Java table (JTable)
but when I delete in JTable using this code
int numRows = tblweng.getSelectedRows().length;
for(int i=0; i<numRows ; i++ )
((DefaultTableModel)tblweng.getModel()).removeRow(tblweng.getSelectedRow());
it deletes only in Table but not in Database. So how I can remove both of them at the same time I click "Remove Button".
Please guide me but don't go too deep I am just a basic here. Thank in advance.
1. click Jtable // row selected
2. get data form dep_Table like
int a = dep_Table.getSelectedRow();
String b = String.valueOf(dep_Table.getValueAt(a, 1));
3. What data you want in you get and store in String
4. Connect Database
5. use Delete Query and Delete Data From Database
6. Reload Table Again
These Few basic Step is Enough for You.. I Think..
I have a long piece of code in java which uses selenium webdriver and firefox to test my website. Pardon me if I can't reproduce it here. It has an infinite while loop to keep doing its function repeatedly. Thats what its supposed to do. Also, I don't use multi threading.
Sometimes, it gets stuck. I use a windows system and the code runs on command prompt. When it gets stuck, no errors or exceptions are thrown. Its something like "it hangs" (only the window in which the code runs hangs). Then I have to use CTRL + C . Sometimes it resumes working after that, other times it gets terminated and I restart it. It works fine but after some loops it "hangs" again. Also, I've noticed that its usually during the execution of one of the methods querying mysql database.
The code runs an infinite loop. Each time, it queries the mysql database, fetches a value(whose 'status' field is not 'done') from a particular table (one value in each loop) and proceeds with testing with this value.At the end of the loop, the table is updated (the column 'status' is set to 'done' for that value). When there are no new values having 'status' not equal to 'done' in that particular table, it should ideally display "NO NEW VALUE". However, after all the values have been used, it simply takes up the last used value (even though its status is updated to 'done' at the end of previous loop) and goes ahead. I then have to terminate the execution and run the code again. This time when the infinite loop begins, it queries the database and correctly displays "NO NEW VALUE", queries again, displays the message again and so on(which is what it should do)
I close the sql connection using con.close().
It appears that after running the loop for a few times, some resource is getting exhausted somewhere. But this is only a wild guess.
Can anyone suggest what the problem is and how do I fix it ?
Below is a relevant piece of code :
try{
String sql = "select something from somewhere where id = ? and is_deleted = '0';";
System.out.println("\n"+sql + "\n? = " + pID);
PreparedStatement selQuery1 = conn.prepareStatement(sql);
selQuery1.setString(1, pID);
ResultSet rs1 = selQuery1.executeQuery();
//Extract data from result set
while(rs1.next() && i1<6){
//do something
}//end while loop
String sql2 = "select something2 from somewhere2 where id = ? and is_deleted = '0';";
System.out.println("\n"+sql2 + "\n? = " + pjID);
PreparedStatement selQuery2 = conn.prepareStatement(sql2);
selQuery2.setString(1, pjID);
ResultSet rs2 = selQuery2.executeQuery();
//Extract data from result set
while(rs2.next() && i1<6){
//do something
}//end while loop
System.out.println("\nDone.");
conn.close();
}catch (SQLException e) {
flag=false;
}
Please note that no exceptions are thrown anywhere. The window in which the code is running just freezes (that too once in while) after displaying both the query statements.
I forgot to close the query and the resultset. Just closing the connection should implicitly close the query and resultset but it doesn't work always.
I also faced the same problem recently. But in my case the issue was with indexes. I am just pointing out here so that it can be helpful to other folks.
In my case I am fetching the menu items from MenuMaster table from database. So after successfully log in, I am hitting a database to fetch the menu items using MySQL connector driver. Here I need to fetch parent menu with their child menus. In my query, in where clause I have not used any primary key or Unique key. So, it was taking a long time. So just make an index of that key, and it worked as charm...
I am creating an Android app to help actors rehearse from a script. The user can select a character from the script to rehearse as. The script will then be shown to the user up until his selected character's first line. When the user decides to proceed, their current line will appear, as well as the rest of the script up to their next line. The user can proceed again and so on.
I have the whole play stored in an SQLite database and I populate custom ListViews by executing queries on the database.
My question is how can we execute a query on the database, but stop returning items once we have found a certain item (e.g. the character's name)? I store the results into a Cursor then populate the ListView with the following method:
private void fillData() {
mFrom = new String[] { PlayDbAdapter.KEY_CHARACTER,
PlayDbAdapter.KEY_LINE };
mTo = new int[] { R.id.textCharacter, R.id.textLine };
// Now create an array adapter and set it to display using our row
SimpleCursorAdapter adapter = new SimpleCursorAdapter(this,
R.layout.play_list_layout, mCursor, mFrom, mTo);
setListAdapter(adapter);
}
If you require anymore information or code, please let me know.
Thank you!
It's far more efficient to return chunks of the script in a Cursor and then search the Cursor for the actor's first line.
Another way to do it would be to return line "context" for a character. Have the database store an index into the lines. Return a Cursor containing only the character's lines, then do a second query that returns 10 lines before and after the character's lines.