How to use Vector as DataSource? - java

I want to use this vector as DataSource for my Jtable. There are four columns here (ADI, SOYADI, BABA ADI, ANA ADI). ResultSet is adding every row to vector named _kisivector. This is my DataSource.
But I don't want to get whole records at start. I want to get only 5 records from this vector. Then there will be 2 button, back and forward. When I click Forward it will go for other 5 record. And when I click back button, it will go for 5 previous record.
Is there any example for this?
private Vector getSonuc(String _ad){
Vector _kisivektor = new Vector();
PreparedStatement stmt = null;
ResultSet rs = null;
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection conn = DriverManager.getConnection("jdbc:oracle:thin:#xx.xx.xx.xx.:1521:xxxx", "xxx", "xxx");
stmt = conn.prepareStatement("select * from t_gnl_kisi where ADI like ?");
stmt.setString(1, _ad+"%");
rs = stmt.executeQuery();
while (rs.next()) {
_kisivektor.add(rs.getString("ADI"));
_kisivektor.add(rs.getString("SOYADI"));
_kisivektor.add(rs.getString("ANA_ADI"));
_kisivektor.add(rs.getString("BABA_ADI"));
}
stmt.close();
rs.close();
}
catch (Exception e) {
e.printStackTrace();
}
return _kisivektor;
}
}

You can use the solution discussed here,
http://forums.sun.com/thread.jspa?threadID=5425845&tstart=1 (This is on demand fetching)
This is prefetching
http://forums.sun.com/thread.jspa?threadID=5371696
Finally if you want to get batch of data of 5 rows. You can subclass the Data Model and only read 5 rows and keep connection open. When "Back" or "Forward" buttons are pressed you can scroll the resultset to that many records (you anyway are going to have a twoway scrollable result set)

There is a pattern name for this: Value List Handler which is a specific form of Lazy Loading.

Related

Results set returns empty when records exist. Statement works when executed in console, not in code. Why?

I am trying to obtain records from my MySQL database that I am not hosting. My code will not produce any results or throw any errors. I am just getting an empty results set even though there are definitely records that exist in the database. In fact, when I write my statement in console, it returns the data. I'm not sure why it isn't working in the code.
What I've tried:
I tested my SQL query statement in console to double check it is correct. The statement works in console, so I don't think this is it.
My SQL connection should be working because I use the same connection method in another part of the code where I am inserting records into the database and it works there. I do close the connection I create there as soon as the method I am getting results from is complete to avoid concurrent connections.
I tried stopping the console connection to make sure the issue wasn't from concurrent connections. I don't think it is, because I completely deleted the data source out of my console and I was still running into the issue. Plus, even when the database exists in my console, I can still add records without running into any issues.
For more info, I am coding this on Intellij Idea. My console is also Intellij's database console. The database is a MySQL database that I do not host on my machine.
OLD CODE, NEW CODE HAS BEEN UPDATED PER COMMENTS (ISSUE STILL ONGOING)
public ObservableList loadUsers(){
Connection conn;
PreparedStatement stmt;
ResultSet rs;
//userList is a global var in this class declared at the top
userList = FXCollections.observableArrayList();
try {
conn = Model.SQL_Connection.connect();
System.out.println(conn);
String resultString = "select userID, userName, lastUpdate, lastUpdatedBy from user";
stmt = conn.prepareStatement(resultString);
rs = stmt.executeQuery();
//used this to check size of resultset
//it always returns 0
System.out.println(rs.getFetchSize());
//for each row
while(rs.next()){
//Iterate Row
ObservableList<String> row = FXCollections.observableArrayList();
//for each column
for(int i=1 ; i<=rs.getMetaData().getColumnCount(); i++){
row.add(rs.getString(i));
}
userList.add(row);
}
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
//convert results if needed
//put results into table
return userList;
}
NEW CODE:
public ObservableList loadUsers(){
//call result set
Connection conn;
PreparedStatement stmt;
ResultSet rs;
userList = FXCollections.observableArrayList();
try {
conn = Model.SQL_Connection.connect();
System.out.println(conn);
String resultString = "select userID, userName, lastUpdate, lastUpdatedBy from user";
stmt = conn.prepareStatement(resultString);
rs = stmt.executeQuery();
//for each row
while(rs.next()){
//Iterate Row
ObservableList<String> row = FXCollections.observableArrayList();
//for each column
for(int i=1 ; i<=rs.getMetaData().getColumnCount(); i++){
row.add(rs.getString(i));
}
userList.add(row);
}
rs.close();
stmt.close();
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
//convert results if needed
//put results into table
return userList;
}
Results from code above are that the connection was successful, not throwing any errors, and that the rs.getFetchSize() is 0. I think this is the right method to use to test but this is my first SQL project so I could be wrong.
If someone knows what I could do to return actual results, I would greatly appreciate it. I've spent quite a few hours looking around, but maybe it's something simple that someone with more experience will be able to spot. I will follow up here if I find a solution.

JComboBox only show one item from database

I'm trying to display database item into a JComboBox and this is my code.
public static void checkItemName(){
Connection conn = SQLite.SQLite();
String sql = "select itemname from item";
try{
Statement statement = conn.createStatement();
ResultSet resultSet = statement.executeQuery(sql);
while (resultSet.next()){
String list = resultSet.getString("itemname");
purcItemName.addItem(list);
conn.close();
}
} catch (SQLException lol){
System.out.println(lol.toString());
}
}
I did declare static JComboBox purcItemName; and purcItemName = new JComboBox();
The method/function will be called then user press login button.
The problem I'm having now is that, it only shows one item while my database has multiple items.
Anyone got an idea why?
Vector v = new Vector();
while (resultSet.next()){
String list = resultSet.getString("itemname");
v.add(list);
}
conn.close();
purcItemName.setModel(new DefaultComboBoxModel(v));
store the data you got from database in a vector object and once that is completed set the vector object in your combobox as a new model. try this one
and don't close the connection inside your loop.
you are closing connection inside resultSet.next() check, put the conn.close() outside, to the end

Exporting large data into CSV from sqlserver using java

I have 9 million records in sqlserver. I am trying to import it into csv files so that I can put that data into mongo db. I have written Java code for sql2csv import. But I have two issue
If I read all the data in list and then try to insert into CSV, I got outofmemorry exception.
If I read line by line and try to insert every line in CSV, it took very long time to export data.
My code is some thing like
List list = new ArrayList();
try {
Class.forName(driver).newInstance();
conn = DriverManager.getConnection(url, databaseUserName, databasePassword);
stmt = conn.prepareStatement("select OptimisationId from SubReports");
result = null;
result = stmt.executeQuery();
// stmt.executeQuery("select * from Subscription_OptimisationReports");
result.setFetchSize(1000);
while (result.next()) {
//System.out.println("Inside while");
SubReportsBean bean = new SubReportsBean();
bean.setOptimisationId(result.getLong(("OptimisationId")));
list.add(bean);
generateExcel(list);
}
//generateExcel(list);
conn.close();
}
Can there be a faster approach to export all data quickly? Or even better if it can directly be exported to mongo instead of csv.
Maybe you should paginate your data by only reading a little at a time by using LIMIT and OFFSET.
select OptimisationId from SubReports OFFSET 0 ROWS FETCH NEXT 1000 ROWS ONLY;
select OptimisationId from SubReports OFFSET 1000 ROWS FETCH NEXT 1000 ROWS ONLY;
select OptimisationId from SubReports OFFSET 2000 ROWS FETCH NEXT 1000 ROWS ONLY;
...
Just keep a counter of the offset.
Another Example
If you use this solution then you'd need to modify your code to append to the end of the Excel file -- don't keep all your results in memory otherwise you'll still run into the OutOfMemoryException.
Definitely when dealing with so much records, collecting all date in a list before dumping in to CSV is bound to fail.
So your solution 2 is the way to go.
Your code seems to correspond to this solution but I think you 've just forgotten to move your list declaration or to empty your list in the loop. You could do :
try {
Class.forName(driver).newInstance();
conn = DriverManager.getConnection(url, databaseUserName, databasePassword);
stmt = conn.prepareStatement("select OptimisationId from SubReports");
result = null;
result = stmt.executeQuery();
// stmt.executeQuery("select * from Subscription_OptimisationReports");
result.setFetchSize(1000);
while (result.next()) {
//System.out.println("Inside while");
SubReportsBean bean = new SubReportsBean();
bean.setOptimisationId(result.getLong(("OptimisationId")));
List list = new ArrayList();
list.add(bean);
generateExcel(list);
}
//generateExcel(list);
conn.close();
}

Reading database data from more than two tables into a Swing table but the frame hangs indefinitely

Am trying to read database data into a Swing table from two database tables but the frame hands indefinitely.
JDBCConnectionPigs c11 = new JDBCConnectionPigs();
try {
c11.createConnection().setAutoCommit(false);
PreparedStatement ps = c11.createConnection().prepareStatement("SELECT * FROM sow_info");
ResultSet rs=ps.executeQuery();
int i1=0, a =0,b=1, c1=2, d1 =3, e= 4,fs=5;
while(rs.next()){
data4 =new ArrayList<>();
data4.add(a, rs.getString("sow_info_id"));
data4.add(b, rs.getString("sow_name"));
data4.add(c1,rs.getString("sow_colour"));
data4.add(d1,rs.getString("sow_breed"));
data4.add(e,rs.getString("sow_date_of_birth"));
PreparedStatement ps1 = c11.createConnection().prepareStatement("SELECT staff_id FROM staff");
ResultSet rst=ps1.executeQuery();
while(rst.last() ){
stafid= rst.getString("staff_id");
}
data4.add(e, stafid);
data5.add(i1, data4);
}
model = new MyTableModel(data5, column1);
jTable2.setModel(model);
c11.createConnection().setAutoCommit(true);
} catch (SQLException ex) {
Logger.getLogger( SowSow.class.getName()).log(Level.SEVERE, null, ex);
}
if(c11!=null){
c11.closeConnection();
}
I need help to know the cause and how I can implement it differently.
If you want only the last staff_id, why do you want to get all the staff_id from the table and iterate over it. Get the latest staff id using a query like below.
SELECT staff_id FROM staff ORDER BY staff_id DESC LIMIT 1
try this code.I hope this working.
PreparedStatement ps1 = c11.createConnection().prepareStatement("SELECT staff_id FROM staff");
ResultSet rst=ps1.executeQuery();
rst.last()
{
stafid= rst.getString("staff_id");
}while(rst.previous());

How do i keep myself DRY and prevent code explosion in this scenario

My application lets a user browse a world map. When the user clicks on a country, a mindmap appears. The user then traverses the nodes of this mindmap. So for example, when user clicks Singapore, a mind map appears. Next, the user clicks on a job function, e.g. Human Resources. This final node redirects the user to another page, in which there is a JSF datatable showing the job profiles in the respective location and function. A typical method which corresponds to each datatable looks like this.
public ResultSet getAll() throws SQLException {
Connection conn = ds.getConnection();
try {
Statement stmt = conn.createStatement();
ResultSet result = stmt.executeQuery("SELECT * FROM JOBPROFILE WHERE LOCATION='SINGAPORE' AND JOBFUNCTION='HUMAN RESOURCES'");
CachedRowSet crs = new CachedRowSetImpl();
crs.populate(result);
return crs;
} finally {
conn.close();
}
}
The problem is that I have 16 locations and 14 functions. This means that I have 224 pages,datatables & methods to write! Maintenance would be a nightmare. Is there some way I could do this programatically so I wouldn't have to repeat most of the code over and over again? The only variance between each method is the SQL query. I'm implementing the world map and mind map in flash, hence I'm not sure as to how can I retrieve the final value the user clicks, and pass it to the backing bean,so that I can call the relevant query, instead of writing 224 different pages, datatables and methods which varies in only one line of code.
You should store the data in the hyperlink. i.e.
foo.com/jobs?country=singapore&jobfunction=humanresources
The JSF can then take that and call a more resuable method:
public ResultSet getAll(String location, String profile) throws SQLException {
Connection conn = ds.getConnection();
try {
Statement stmt = conn.createStatement();
ResultSet result = stmt.executeQuery("SELECT * FROM JOBPROFILE WHERE LOCATION='"+location+"' AND JOBFUNCTION='"profile"'");
CachedRowSet crs = new CachedRowSetImpl();
crs.populate(result);
return crs;
} finally {
conn.close();
}
}
Just remember that you should be escaping these or, better yet, using a prepared statement

Categories

Resources