aspose file tools*
The moose likes JDBC and the fly likes ResultSets Lose Second half of getString Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Databases » JDBC
Bookmark "ResultSets Lose Second half of getString" Watch "ResultSets Lose Second half of getString" New topic
Author

ResultSets Lose Second half of getString

Shawn Stark
Greenhorn

Joined: Jan 02, 2008
Posts: 7
When I insert to my result set, then move back a row the previouse records are only displaying 2 characters of each column. It seems the number of characters displayed when resultSet size was smaller was higher, not sure though I must test it again.

More info on problem found in comments above problematic method.

This is my Adapter to control my database requests

I have modified this post to reflect the working version.
I swear I put it back the way it was when it wasn't working.
but it works now. I was about to start trying to use refetch.

import java.sql.*;
import java.awt.*;
import javax.swing.*;

public class contactAdapt {
private Contact contactHold = new Contact();
private boolean test;
private ResultSet contacts = null;
private String url = "jdbcdbc:ContactData";
private String allSQL = "select * from contact";
private Statement stmnt = null;
private Connection conn = null;
private int row;
private int last;
private int idNum;
private int cur;


/** Creates a new instance of contactAdapt */
public contactAdapt() {

}

// connects and sets the length of result set to working variable.
// The statement is created to produce a scrollable resultSet

public void setConnection()
{
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection conn = DriverManager.getConnection(url);
stmnt = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,
ResultSet.CONCUR_UPDATABLE);
contacts = stmnt.executeQuery(allSQL);
contacts.last();

last = contacts.getRow();
}

catch(ClassNotFoundException ex){
System.err.println("Cannot find the database driver classes.");
System.err.println(ex);
}
catch(SQLException ex){
System.err.println(ex);

}
}

// DON'T THINK THIS WORKS \/

public void refreshContacts()
{
try{
contacts = stmnt.executeQuery(allSQL);
contacts.last();
last = contacts.getRow();
}
catch(SQLException ex){

System.err.println(ex);
}
}

// Returns a Contact object from current cursor position of resultSet

public Contact getCurRec()
{
try{
contactHold.setfName(contacts.getString("FirstName"));
contactHold.setlName(contacts.getString("LastName"));
contactHold.setID(contacts.getInt("ContactId"));
contactHold.setAddr(contacts.getString("Address"));
contactHold.setState(contacts.getString("State"));
contactHold.setCity(contacts.getString("City"));
contactHold.setZip(contacts.getString("Zip"));
contactHold.setInit(contacts.getString("MiddleName"));
}

catch(SQLException ex){
//System.err.println(ex);
JOptionPane.showMessageDialog(null, ex);
}

return contactHold;

}

// Returns an object of type Contact from first row of resultSet

public Contact getFirstRec()
{
try{
contacts.first();
contactHold.setfName(contacts.getString("FirstName"));
contactHold.setlName(contacts.getString("LastName"));
contactHold.setID(contacts.getInt("ContactId"));
contactHold.setAddr(contacts.getString("Address"));
contactHold.setState(contacts.getString("State"));
contactHold.setCity(contacts.getString("City"));
contactHold.setZip(contacts.getString("Zip"));
contactHold.setInit(contacts.getString("MiddleName"));
}

catch(SQLException ex){
//System.err.println(ex);
JOptionPane.showMessageDialog(null, ex);
}

return contactHold;

}

// moves cursor up one position and returns Contact object
// The number of times this can be used is controled in
// the contactAdapt implementing class by only allowing execution
// of this method if this.getCurRow < this.getLength
// Notice I only set length when adding or opening connection

public Contact getNextRec()
{
try{
contacts.next();
contactHold.setfName(contacts.getString("FirstName"));
contactHold.setlName(contacts.getString("LastName"));
contactHold.setID(contacts.getInt("ContactId"));
contactHold.setAddr(contacts.getString("Address"));
contactHold.setState(contacts.getString("State"));
contactHold.setCity(contacts.getString("City"));
contactHold.setZip(contacts.getString("Zip"));
contactHold.setInit(contacts.getString("MiddleName"));
}
catch(SQLException ex){
JOptionPane.showMessageDialog(null, ex);
}
return contactHold;
}

// Returns a Contact Object for last row of resultSet

public Contact getLastRec()
{
try{
contacts.last();
contactHold.setfName(contacts.getString("FirstName"));
contactHold.setlName(contacts.getString("LastName"));
contactHold.setID(contacts.getInt("ContactId"));
contactHold.setAddr(contacts.getString("Address"));
contactHold.setState(contacts.getString("State"));
contactHold.setCity(contacts.getString("City"));
contactHold.setZip(contacts.getString("Zip"));
contactHold.setInit(contacts.getString("MiddleName"));
}
catch(SQLException ex){
JOptionPane.showMessageDialog(null, ex);
}
return contactHold;
}

//Moves cursor position back one position and returns a Contact object
//The number of times this method is used is controlled from
//implementing class by only allowing Execution if this.getCurRow > 1

public Contact getPrevRec()
{
try{
contacts.previous();
contactHold.setfName(contacts.getString("FirstName"));
contactHold.setlName(contacts.getString("LastName"));
contactHold.setID(contacts.getInt("ContactId"));
contactHold.setAddr(contacts.getString("Address"));
contactHold.setState(contacts.getString("State"));
contactHold.setCity(contacts.getString("City"));
contactHold.setZip(contacts.getString("Zip"));
contactHold.setInit(contacts.getString("MiddleName"));
}
catch(SQLException ex){
JOptionPane.showMessageDialog(null, ex);
}
return contactHold;
}

//returns the curent cursor position
//used to control execution of other methods

public int getCurRow()
{
try{
row = contacts.getRow();
}
catch(SQLException ex){
JOptionPane.showMessageDialog(null, ex);
}
return row;

// return length of resultSet
// must be used to control getNextRec or when you reach end
// of resultSet you will trigger an exception

public int getLength()
{
return last;
}


//I don't believe I am currently using this method
//But I may need to with add/update/delete
//For right now the initial value assigned at setConnection is fine

public void setLength()
{
try{

contacts.last();
last = contacts.getRow();
}
catch(SQLException ex){

System.err.println(ex);
}
}

/***********************************************************************
This is where My problems begin,
The statements commented out in this method
kind of fix it but break it again...

This method work without commented out statements but all of my returned strings are cut short in every method after the execution of this method.

When the close and reconnect statements arn't commented out I can not see my added record as I do with these statements commented out.

When commented out \/
Simialar to update method connection seems to take time to load resultSet but I don't believe my program should proceed until this is done. What I meen is I am displaying the old data for a couple of method calls before it shows the right data.

In the implementing class I getCurRec after running this method to display the record I just edited. This all works only all of my data displayed is chopped down to 2 characters. I am not doing in alterations to any STRINGS I am just using the getStatments from my contact object in implementing class to assign strings to text boxes... Works for all methods..

When not commented out \/
Everything displays fine when I reconnect. Only the last entered record is not displayable untill exiting program and restarting. The record is added. When I reconnect this record should be found but is not.
When adding conn.close I recieve an sql error. Maybe because execution not
finished??

Dose the DB driver do its work while your program
continues execution??

I have modified this post to reflect the working version.
I am realy unsure why it works now I thought I tryed everything
but I geuss maybe I didn't.. I swear I put it just like I had it when it didn't work. But it works Now!
************************************************************************/
public void addRec(Contact c)
{
try{
contacts.last();
idNum=contacts.getInt("ContactId")+1;
contacts.moveToInsertRow();
contacts.updateInt("ContactId",idNum);
contacts.updateString("FirstName",c.getfName());
contacts.updateString("LastName",c.getlName());
contacts.updateString("MiddleName",c.getInit());
contacts.updateString("Address",c.getAddr());
contacts.updateString("City",c.getCity());
contacts.updateString("State",c.getState());
contacts.updateString("Zip",c.getZip());
contacts.insertRow();
contacts.moveToCurrentRow();
this.setLength
//used to hold cursor position over to new
//new connection
//cur = contacts.getRow();


//contacts.close();
//stmnt.close();

//this.setConnection();
//contacts.absolute(cur);






}
catch(SQLException ex){
JOptionPane.showMessageDialog(null, ex);
}
}

//works but my data displayed is still the same untill I move
//forward and back a few times.. Without Open and Close Statements
//I think I get a similar result as add method or I just wanted them
//to look the same
// It works now. This post has been modified to reflect changes

public void updateRec(Contact c)
{
try{
contacts.updateString("FirstName",c.getfName());
contacts.updateString("LastName",c.getlName());
contacts.updateString("MiddleName",c.getInit());
contacts.updateString("Address",c.getAddr());
contacts.updateString("City",c.getCity());
contacts.updateString("State",c.getState());
contacts.updateString("Zip",c.getZip());
contacts.updateRow();
// Works now, I don't know what happened!!!
//contactHold = c;
//cur = contacts.getRow();
//this.setLength();
//contacts.close();
//this.setConnection();
//contacts.absolute(cur);



}
catch(SQLException ex){
JOptionPane.showMessageDialog(null, ex);
}




}




}

SQL ERRORS WILL HAPPEN IF THE CONTACT OBJECT PASSED
TO UPDATE OR ADD METHODS IS ASSIGNED NULL VALUES.

[ January 03, 2008: Message edited by: Shawn Stark ]

[ January 03, 2008: Message edited by: Shawn Stark ]
[ January 03, 2008: Message edited by: Shawn Stark ]
Shawn Stark
Greenhorn

Joined: Jan 02, 2008
Posts: 7
This is the contact class I use

public class Contact
{
private String fName;
private String initial;
private String lName;
private String addr;
private String city;
private String zip;
private String State;
private String homePhone;
private String workPhone;
public static char female = 'f';
private String picture;
private int idNum;


/** Creates a new instance of Contact */
public Contact()
{
}
public Contact(int id, String fn, String in, String ln, String addre,String c,String s,String z,String hPh, String wPh, String pic)
{
idNum = id;
fName = fn;
initial = in;
lName = ln;
addr = addre;
city = c;
zip = z;
State = s;
homePhone = hPh;
workPhone = wPh;
picture = pic;


}
public void setInit(String in)
{
initial = in;
}

public void setID(int id)
{
idNum = id;
}

public int getID()
{
return idNum;
}
public void setfName(String fn)
{
fName = fn;
}
public String getfName()
{
return fName;
}
public void setlName(String ln)
{
lName = ln;
}
public String getlName()
{
return lName;
}
public void setAddr(String adr)
{
addr = adr;
}

public String getAddr()
{
return addr;
}

public void setCity(String cty)
{
city = cty;
}

public String getCity()
{
return city;
}

public void setZip(String zp)
{
zip = zp;
}

public String getZip()
{
return zip;
}

public void setState(String st)
{
State = st;
}
public String getState()
{
return State;
}

public void setHomePhone(String hp)
{
homePhone = hp;
}

public String getHomePhone()
{
return homePhone;
}

public void setWorkPhone(String wp)
{
workPhone = wp;
}

public String getWorkPhone()
{
return workPhone;
}

public void setPicture(String p)
{
picture = p;
}

public String getPicture()
{
return picture;
}



public String getInit()
{
return initial;
}






}
Shawn Stark
Greenhorn

Joined: Jan 02, 2008
Posts: 7
This is the class I use to display my info..
Analyze the methods...
you will not be able to copy this and compile it because
the java beans form file is not included

/* @author Shawn Stark
*/
import java.awt.*;
import javax.swing.*;
import java.util.*;
import java.sql.*;
public class contactFrame extends javax.swing.JFrame {

// curRec is used to control what record is displayed in displayDetails
//method
// also controls what record is updated in update Method in this class
// curRec should keep its aquired state throughout run of program.
// I just retrieve cursor position of resultSet

// curPage is used to control what records are preloaded from DB in
//dataLoad method in this class
// curPage is unused as of yet but has definate potential.

// I am working on an in and out type read and display process.
// Example; a 5 element array, e1 = e2, e2 = e3, e3 = e4, e4 = e5, e5
//nextRead
// initial data load would load all 5 elements and set var page = 5
//method nextRec would add 1, method
// nextpage would add 5. Though I could load one at a time it would
//probably be less efficient.
// If I had to ask my dataBase server every time I wanted another file I
//would wast effort !)
// The only time I need actual record is when editing thus record should
//be locked to all other
// edit access before editing in the system I am explaining, this should
//always be done.

// I have created a contact adaptor with methods to manipulate
//contactData database
// I add/ update and create an ID for new records based on the ID of the
//last record
// in the result set. When I create my delete I will not be changing ID
//#s of any other records.
// or I will have troubles with my joins..With the methods in my adaptor
//class one could create
// a page in page out type view structure of the data like I explain
//with curPage variable.
// JUST BECAUSE YOU DONT SEE the ID in the running program dose not meen
//I dont assign it.
// I am pondering if searching result set for a record is more
//appropriate than doing that work
// with a query.

// My next addition will be a table with loaded values and a page button.
// I will have buttons to sort by fName, ID, lName, city, state, zip
// After this is accomplished I will beginn work on result sets from
//joined tables.

// I intend to create a menu where you can select
// sales orders, purchase orders, inventory, clients, vendors, staff,
// sales and purchase orders are the views which require joins


int curRec = 0;
// int curPage = 0;
boolean recOps = true;
boolean recOps2 = true;
boolean addOp = false;
contactAdapt cntct = new contactAdapt();
Contact contactHold2 = new Contact();
Contact contactHold = new Contact();



/** Creates new form contactFrame */
public contactFrame() {
initComponents();

this.setTitle("Contact information");
// Initial data objects created and assigned to an object array
// element
// In the instance one would like to assign a large number of
//objects, one holding object and a loop would suffice.
cntct.setConnection();
addRec.setVisible(true);
edtRec.setVisible(true);
updRec2.setVisible(false);
contactHold = cntct.getFirstRec();
curRec = cntct.getCurRow();
displayDetails();

}
/**********************************************************************
/** This method is called from within the constructor to
* initialize the form.
* WARNING: Do NOT modify this code. The content of this method is
* always regenerated by the Form Editor.
*/
// <editor-fold defaultstate="collapsed" desc=" Generated Code ">
*************************************************************************/

private void lastRecActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
contactHold = cntct.getLastRec();
curRec = cntct.getCurRow();
displayDetails();
}

private void firstRecActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
contactHold = cntct.getFirstRec();
curRec = cntct.getCurRow();
displayDetails();
}

private void updRec2ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
if (addOp == true){
addRec();
addViewSwitch();
contactHold = cntct.getLastRec();
curRec = cntct.getCurRow();
addOp = false;
displayDetails();
}
else if (addOp == false){
updateRec();
editViewSwitch();
contactHold = cntct.getCurRec();
curRec = cntct.getCurRow();
displayDetails();
}
}

private void addRecActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
addViewSwitch();



}
private void updRecActionPerformed(java.awt.event.ActionEvent evt){}


private void edtRecActionPerformed(java.awt.event.ActionEvent evt) {

editViewSwitch();





}
private void addViewSwitch(){
if (recOps2 == true){
modeLbl.setText("****************Add*******************");
fNameTxtFld.setText("");
mInitTxtFld.setText("");
lNameTxtFld.setText("");
addrTxtFld.setText("");
cityTxtFld.setText("");
statecmbbx1.setSelectedIndex(1);
zipTxtFld.setText("");
statecmbbx1.setEnabled(true);
fNameTxtFld.setEditable(true);
mInitTxtFld.setEditable(true);
lNameTxtFld.setEditable(true);
addrTxtFld.setEditable(true);
cityTxtFld.setEditable(true);
zipTxtFld.setEditable(true);
lastRec.setEnabled(false);
prevRec.setEnabled(false);
nextRec.setEnabled(false);
firstRec.setEnabled(false);
updRec2.setText("Save Record");
addRec.setText("Cancel Add");
updRec2.setVisible(true);
addRec.setVisible(true);
edtRec.setVisible(false);
recOps2 = false;
addOp = true;

}

else{
modeLbl.setText("****************View*****************");
statecmbbx1.setEnabled(false);
fNameTxtFld.setEditable(false);
mInitTxtFld.setEditable(false);
lNameTxtFld.setEditable(false);
addrTxtFld.setEditable(false);
cityTxtFld.setEditable(false);
zipTxtFld.setEditable(false);
lastRec.setEnabled(true);
prevRec.setEnabled(true);
nextRec.setEnabled(true);
firstRec.setEnabled(true);
updRec2.setText("Update Record");
addRec.setText("Add Record");
addRec.setVisible(true);
edtRec.setVisible(true);
updRec2.setVisible(false);
recOps2 = true;




displayDetails();
}


}


private void editViewSwitch(){
if (recOps == true){
modeLbl.setText("****************Edit******************");
statecmbbx1.setEnabled(true);
fNameTxtFld.setEditable(true);
mInitTxtFld.setEditable(true);
lNameTxtFld.setEditable(true);
addrTxtFld.setEditable(true);
cityTxtFld.setEditable(true);
zipTxtFld.setEditable(true);
lastRec.setEnabled(false);
prevRec.setEnabled(false);
nextRec.setEnabled(false);
firstRec.setEnabled(false);
edtRec.setText("Cancel Edit");
updRec2.setVisible(true);
addRec.setVisible(false);
recOps = false;
addOp = false;
}

else{
modeLbl.setText("****************View*****************");
statecmbbx1.setEnabled(false);
fNameTxtFld.setEditable(false);
mInitTxtFld.setEditable(false);
lNameTxtFld.setEditable(false);
addrTxtFld.setEditable(false);
cityTxtFld.setEditable(false);
zipTxtFld.setEditable(false);
lastRec.setEnabled(true);
prevRec.setEnabled(true);
nextRec.setEnabled(true);
firstRec.setEnabled(true);
edtRec.setText("Edit Record");
addRec.setVisible(true);
updRec2.setVisible(false);
recOps = true;
addOp = false;
displayDetails();
}
}
private void prevRecActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
if (curRec > 1)
{
contactHold = cntct.getPrevRec();
curRec=cntct.getCurRow();
displayDetails();

}
}

private void jTextField2ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}

private void nextRecActionPerformed(java.awt.event.ActionEvent evt) {
// load next record

if (curRec < cntct.getLength())
{
contactHold = cntct.getNextRec();
curRec=cntct.getCurRow();
displayDetails();
}



}
// method to display details
private void displayDetails()
{

// picLbl.setIcon(new javax.swing.ImageIcon(contactHold.getPicture()));
String hold = Integer.toString(curRec);
jTextField2.setText(hold);
fNameTxtFld.setText(contactHold.getfName());
mInitTxtFld.setText(contactHold.getInit());
lNameTxtFld.setText(contactHold.getlName());
addrTxtFld.setText(contactHold.getAddr());
cityTxtFld.setText(contactHold.getCity());
statecmbbx1.setSelectedItem(contactHold.getState());
zipTxtFld.setText(contactHold.getZip());

}

private void updateRec()
{
contactHold2.setfName(fNameTxtFld.getText());
contactHold2.setlName(lNameTxtFld.getText());
contactHold2.setAddr(addrTxtFld.getText());
contactHold2.setInit(mInitTxtFld.getText());
contactHold2.setCity(cityTxtFld.getText());
contactHold2.setState(this.stateAbbr());
contactHold2.setZip(zipTxtFld.getText());

cntct.updateRec(contactHold2);



}

private String stateAbbr()
{
String stat;
switch(statecmbbx1.getSelectedIndex())
{
case 0: stat = "AL"; break;
case 1: stat = "AK"; break;
case 2: stat = "AS"; break;
case 3: stat = "AZ"; break;
case 4: stat = "AR"; break;
case 5: stat = "CA"; break;
case 6: stat = "CO"; break;
case 7: stat = "CT"; break;
case 8: stat = "DE"; break;
case 9: stat = "DC"; break;
case 10: stat = "FM"; break;
case 11: stat = "FL"; break;
case 12: stat = "GA"; break;
case 13: stat = "GU"; break;
case 14: stat = "HI"; break;
case 15: stat = "ID"; break;
case 16: stat = "IL"; break;
case 17: stat = "IN"; break;
case 18: stat = "IA"; break;
case 19: stat = "KS"; break;
case 20: stat = "KY"; break;
case 21: stat = "LA"; break;
case 22: stat = "ME"; break;
case 23: stat = "MH"; break;
case 24: stat = "MD"; break;
case 25: stat = "MA"; break;
case 26: stat = "MI"; break;
case 27: stat = "MN"; break;
case 28: stat = "MS"; break;
case 29: stat = "MO"; break;
case 30: stat = "MT"; break;
case 31: stat = "NE"; break;
case 32: stat = "NE"; break;
case 33: stat = "NV"; break;
case 34: stat = "NH"; break;
case 35: stat = "NJ"; break;
case 36: stat = "NM"; break;
case 37: stat = "NY"; break;
case 38: stat = "NC"; break;
case 39: stat = "ND"; break;
case 40: stat = "MP"; break;
case 41: stat = "OH"; break;
case 42: stat = "OK"; break;
case 43: stat = "OR"; break;
case 44: stat = "PW"; break;
case 45: stat = "PA"; break;
case 46: stat = "PR"; break;
case 47: stat = "RI"; break;
case 48: stat = "SC"; break;
case 49: stat = "SD"; break;
case 50: stat = "TN"; break;
case 51: stat = "TX"; break;
case 52: stat = "UT"; break;
case 53: stat = "VT"; break;
case 54: stat = "VI"; break;
case 55: stat = "VA"; break;
case 56: stat = "WA"; break;
case 57: stat = "WV"; break;
case 58: stat = "WI"; break;
case 59: stat = "WY"; break;
default: stat = "MN"; break;
}
return stat;
}
private void addRec(){
contactHold2.setfName(fNameTxtFld.getText());
contactHold2.setlName(lNameTxtFld.getText());
contactHold2.setAddr(addrTxtFld.getText());
contactHold2.setInit(mInitTxtFld.getText());
contactHold2.setCity(cityTxtFld.getText());
contactHold2.setState(this.stateAbbr());
contactHold2.setZip(zipTxtFld.getText());
cntct.addRec(contactHold2);

}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new contactFrame().setVisible(true);

}
});
}

// Variables declaration - do not modify
private javax.swing.JButton addRec;
private javax.swing.JLabel addrLbl;
private javax.swing.JTextField addrTxtFld;
private javax.swing.ButtonGroup buttonGroup1;
private javax.swing.JTextField cityTxtFld;
private javax.swing.JLabel ctyLbl;
private javax.swing.JButton edtRec;
private javax.swing.JLabel fNameLbl;
private javax.swing.JTextField fNameTxtFld;
private javax.swing.JButton firstRec;
private javax.swing.JDialog jDialog1;
private javax.swing.JPanel jPanel1;
private javax.swing.JPanel jPanel2;
private javax.swing.JTextField jTextField2;
private javax.swing.JLabel lNameLbl;
private javax.swing.JTextField lNameTxtFld;
private javax.swing.JButton lastRec;
private javax.swing.JLabel mInitLbl;
private javax.swing.JTextField mInitTxtFld;
private javax.swing.JLabel modeLbl;
private javax.swing.JButton nextRec;
private javax.swing.JButton prevRec;
private javax.swing.JLabel stateLbl;
private javax.swing.JComboBox statecmbbx1;
private javax.swing.JButton updRec;
private javax.swing.JButton updRec2;
private javax.swing.JLabel zipLbl;
private javax.swing.JTextField zipTxtFld;
// End of variables declaration

}
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: ResultSets Lose Second half of getString
 
Similar Threads
Null data issue
jsp and jdbc
NPE
Connecting to a database through servl
Class.forName error