JavaRanch » Java Forums »
Databases »
JDBC
Author
Solution of rs.previous() using sql server
Suji N
Ranch Hand
Joined: Sep 04, 2001
Posts: 35
posted Sep 06, 2001 19:03:00
0
Hai Friends, I have got a good solution for rs.previous() method using sql server 2000 you can make use of this program in your application this program is working perfectly if you have any doubts and clarifications pl mail me this program uses jdk 1.3. sql server 2000 and jdbc odbc driver. Bye ************************************** import java.awt.*; import java.awt.event.*; import java.sql.*; public class UserSearch extends Frame implements ActionListener { Label userIDLbl,userNameLbl,title, label1, label2, label3, label4, label5; TextField userIDTxt,userNameTxt; Button previous,search,nextPage,reset,exit; TextField tf[][]; int i,recPos; boolean nextP,prev,ent; MyConnection myConus1; Statement st,st1,st2; ResultSet rs,rs2; UserSearch() { myConus1 = new MyConnection(); try { st = myConus1.con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_READ_ONLY); st1 = myConus1.con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_READ_ONLY); st2 = myConus1.con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_READ_ONLY); } catch(Exception e) { System.out.println("excep in defining my con objects "+e); } tf=new TextField [6][5]; userIDLbl = new Label(); userNameLbl = new Label(); userNameTxt = new TextField (3); userIDTxt = new TextField (3); title = new Label(); search = new Button(); previous = new Button(); nextPage = new Button(); reset = new Button(); exit = new Button(); label1=new Label(); label2=new Label(); label3=new Label(); label4=new Label(); label5=new Label(); recPos = 1; setLocation(295,115); // width and height setSize(580,518);// size of the window setVisible(true); setLayout(null); search.setLabel("Search"); add(search); search.setBackground(java.awt.Color.lightGray); search.setBounds(480,60,93,24); userIDLbl.setText("UserID"); userIDLbl.setBounds(48,60,60,28); add(userIDLbl); userIDTxt.setCursor(java.awt.Cursor.getPredefinedCursor(java.awt.Cursor.TEXT_CURSOR)); add(userIDTxt); userIDTxt.setFont(new Font("DialogInput", Font.PLAIN, 17)); userIDTxt.setBounds(120,60,108,26); userNameLbl.setText("UserName"); add(userNameLbl); userNameLbl.setBounds(264,60,66,28); userNameTxt.setCursor(java.awt.Cursor.getPredefinedCursor(java.awt.Cursor.TEXT_CURSOR)); add(userNameTxt); userNameTxt.setFont(new Font("DialogInput", Font.PLAIN, 17)); userNameTxt.setBounds(348,60,108,26); title.setText("User Search"); add(title); title.setForeground(java.awt.Color.blue); title.setFont(new Font("Dialog", Font.BOLD, 18)); title.setBounds(252,24,115,19); previous.setLabel("Previous"); add(previous); previous.setBackground(java.awt.Color.lightGray); previous.setBounds(84,444,93,26); nextPage.setLabel("NextPage"); add(nextPage); previous.setEnabled(false); nextPage.setBackground(java.awt.Color.lightGray); nextPage.setBounds(444,444,93,26); nextPage.setEnabled(false); reset.setLabel("Reset"); add(reset); reset.setBackground(java.awt.Color.lightGray); reset.setBounds(264,444,93,26); exit.setLabel("Exit"); add(exit); exit.setBackground(java.awt.Color.lightGray); exit.setBounds(442,486,93,26); label1.setText("UserID"); add(label1); label1.setBounds(55,118,58,22); label1.setForeground(java.awt.Color.blue); label1.setFont(new Font("DialogInput", Font.PLAIN, 15)); label2.setText("Password"); add(label2); label2.setBounds(150,118,74,22); label2.setForeground(java.awt.Color.blue); label2.setFont(new Font("DialogInput", Font.PLAIN, 15)); label3.setText("Name"); add(label3); label3.setBounds(270,118,50,22); label3.setForeground(java.awt.Color.blue); label3.setFont(new Font("DialogInput", Font.PLAIN, 15)); label4.setText("Authority"); add(label4); label4.setBounds(355,118,82,22); label4.setForeground(java.awt.Color.blue); label4.setFont(new Font("DialogInput", Font.PLAIN, 15)); label5.setText("Citizen-Code"); add(label5); label5.setBounds(460,118,110,22); label5.setForeground(java.awt.Color.blue); label5.setFont(new Font("DialogInput", Font.PLAIN, 15)); search.addActionListener(this); nextPage.addActionListener(this); reset.addActionListener(this); exit.addActionListener(this); previous.addActionListener(this); int xPos; for(int i=0;i<6;i++) { xPos=1; for(int j=0;j<5;j++) { tf[i][j] = new TextField (); tf[i][j].setFont(new Font("DialogInput", Font.PLAIN, 17)); tf[i][j].setVisible(false); tf[i][j].setEditable(false); add(tf[i][j]); if(j==2) { tf[i][j].setBounds(48*xPos,50*(i+3),115,25);//from left //gap of tf between row to row // gap starting between tfs in row //height of tf //i+3 = starting of tf columns from height xPos = xPos+1; } else if(j==3) { tf[i][j].setBounds(63*xPos,50*(i+3),40,25); xPos = xPos+1; } else if(j==4) { tf[i][j].setBounds(65*xPos,50*(i+3),135,25);//from left //gap of tf between row to row // gap starting between tfs in row //height of tf //i+3 = starting of tf columns from height xPos = xPos+1; } else { tf[i][j].setBounds(48*xPos,50*(i+3),85,25); xPos = xPos+2; } } } } public void displayRecords() { for(int i=0;i<6;i++) { for(int j=0;j<5;j++) { tf[i][j].setVisible(false); } } show(); try { if(recPos==1) previous.setEnabled(false); rs.absolute(recPos); i=0; do { if (rs.isLast()) { nextPage.setEnabled(false); } for(int j=0;j<5;j++) { tf[i][j].setText(rs.getString(j+1)); tf[i][j].setVisible(true); } i++; }while (i<6 && rs.next()); } catch(Exception e) { System.out.println("Exc in displayrecords() "+e); } invalidate(); show(); } public void actionPerformed(ActionEvent ae) { String sqlString=null; if(ae.getSource()==search) { nextPage.setEnabled(true); previous.setEnabled(false); if(!(userIDTxt.getText().equals("") && userNameTxt.getText().equals(""))) { sqlString = "select * from users where userid like '" + userIDTxt.getText()+ "%' and name like '" + userNameTxt.getText() + "%'"; } if(!(userIDTxt.getText().equals("")) && userNameTxt.getText().equals("")) { sqlString = "select * from users where userid like '" + userIDTxt.getText()+ "%'"; } if(userIDTxt.getText().equals("") && !(userNameTxt.getText().equals(""))) { sqlString = "select * from users where name like '" + userNameTxt.getText()+ "%'"; } if(userIDTxt.getText().equals("") && userNameTxt.getText().equals("")) { sqlString = "select * from users"; } try { if(nextP) { rs.close(); nextP = false; } rs = st.executeQuery(sqlString); nextP=true; displayRecords(); } catch(Exception e) { System.out.println("in search method "+e); } System.out.println(sqlString); } if(ae.getSource()==nextPage) { previous.setEnabled(true); recPos = recPos+6; displayRecords(); show(); } if(ae.getSource()==previous) { nextPage.setEnabled(true); recPos = recPos-6; displayRecords(); show(); } if(ae.getSource()==reset) { for(int i=0;i<6;i++) { for(int j=0;j<5;j++) { tf[i][j].setVisible(false); } } userNameTxt.setText(""); userIDTxt.setText(""); nextPage.setEnabled(false); } if(ae.getSource()==exit) { closeconnection(); System.exit(0); } } public static void main(String args[]) { new UserSearch(); } public void closeconnection() { try { myConus1.con.close(); System.out.println("Con Closed"); } catch(Exception eno) { System.out.println(eno); } } //closeconnection(); } ***************************** import java.sql.*; public class MyConnection { Connection con; MyConnection() { try { Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); con = DriverManager.getConnection("Jdbc:Odbc:sagar","sagar","sagar"); } catch(Exception e) { System.out.println(" in myconnection.java file " +e); } } }
subject: Solution of rs.previous() using sql server