Status
Not open for further replies.
2
2011
0
0
gm..

i hv created different table for userdetails..

check the foll. code..

> the data is updated and working fine.
> but its not checking that existing user/book includes in the database or not..



Code:
private void jButton5ActionPerformed(java.awt.event.ActionEvent evt)  {                                 
Connection con=null;
PreparedStatement st;
Statement st1;
ResultSet rs;

        try {
            Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
            con=DriverManager.getConnection("jdbc:odbc:LibraryIS");
        } catch(Exception e) {
            System.out.println("Exception Raised1"+e);
        }

        try {


    st=con.prepareStatement("INSERT INTO IssueDetails(UserId, B_Id,  date_issue, due_date) 

VALUES(?,?,?,?)");
           //st.executeUpdate("UPDATE Books SET Avail='Not Available' WHERE B_Id='"+b_id+"'");
    String UserId=jTextField1.getText();
    String B_Id=jTextField2.getText();
     String date_issue=jTextField5.getText();
     String due_date=jTextField6.getText();

            //String us_id=jTextField1.getText();
            //String b_id=jTextField2.getText();
         st.setString(1,UserId);
          st.setString(2,B_Id);
         st.setString(3,date_issue);
          st.setString(4,due_date);
         st.executeUpdate();

         JOptionPane.showMessageDialog(rootPane, "Book is issued to the user!");


        rs=st.executeQuery("select * from AddUser where UserId='"+UserId+"'");
        if(rs.next())
        {
        }
        else
        {
        JOptionPane.showMessageDialog(rootPane, "User not Available");
        }

         rs=st.executeQuery("select * from Books where B_Id='"+B_Id+"'");
         if(rs.next())
        {
      st.executeUpdate("UPDATE Books SET Avail='Not Available' WHERE B_Id='"+B_Id+"'");

    st.executeUpdate("UPDATE IssueDetails SET UserId='"+UserId+"' where UserId='"+UserId+"'");
    st.executeUpdate("UPDATE IssueDetails SET Bks_Tkn='"+B_Id+"' where UserId='"+UserId+"'");
    st.executeUpdate("UPDATE IssueDetails SET date_issue='"+date_issue+"' where UserId='"+UserId+"'");
    st.executeUpdate("UPDATE IssueDetails SET due_date='"+due_date+"' where UserId='"+UserId+"'");
        }
        else
        {
        JOptionPane.showMessageDialog(rootPane, "Book not  Available");
            }
        }

       catch(SQLException e)
      {
          System.out.println("SQL Exception Raised1");


      }
           

}
 
3 comments
try this:
private void jButton5ActionPerformed(java.awt.event.ActionEvent evt) {
Connection con=null;
PreparedStatement st;
Statement st1;


try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
con=DriverManager.getConnection("jdbc:odbc:LibraryIS");
} catch(Exception e) {
System.out.println("Exception Raised1"+e);
}

try {


st=con.prepareStatement("INSERT INTO IssueDetails(UserId, B_Id, date_issue, due_date)

VALUES(?,?,?,?)");
//st.executeUpdate("UPDATE Books SET Avail='Not Available' WHERE B_Id='"+b_id+"'");
String UserId=jTextField1.getText();
String B_Id=jTextField2.getText();
String date_issue=jTextField5.getText();
String due_date=jTextField6.getText();

//String us_id=jTextField1.getText();
//String b_id=jTextField2.getText();
st.setString(1,UserId);
st.setString(2,B_Id);
st.setString(3,date_issue);
st.setString(4,due_date);
st.executeUpdate();

JOptionPane.showMessageDialog(rootPane, "Book is issued to the user!");


ResultSet rs=st.executeQuery("select * from AddUser where UserId='"+UserId+"'");
if(rs.next())
{
}
else
{
JOptionPane.showMessageDialog(rootPane, "User not Available");
}

ResultSet rs=st.executeQuery("select * from Books where B_Id='"+B_Id+"'");
if(rs.next())
{
st.executeUpdate("UPDATE Books SET Avail='Not Available' WHERE B_Id='"+B_Id+"'");

st.executeUpdate("UPDATE IssueDetails SET UserId='"+UserId+"' where UserId='"+UserId+"'");
st.executeUpdate("UPDATE IssueDetails SET Bks_Tkn='"+B_Id+"' where UserId='"+UserId+"'");
st.executeUpdate("UPDATE IssueDetails SET date_issue='"+date_issue+"' where UserId='"+UserId+"'");
st.executeUpdate("UPDATE IssueDetails SET due_date='"+due_date+"' where UserId='"+UserId+"'");
}
else
{
JOptionPane.showMessageDialog(rootPane, "Book not Available");
}
}

catch(SQLException e)
{
System.out.println("SQL Exception Raised1");


}


}
 
its not working frnd :)

i hv done some editing..

now the query checks the existing book/user..

but now adding of data in the table is not occurring and updating that books table - available to not available!

Code:
Connection con=null;
PreparedStatement st1;
Statement st2,st;
ResultSet rs;

    String UserId=jTextField1.getText();
    String B_Id=jTextField2.getText();
     String date_issue=jTextField5.getText();
     String due_date=jTextField6.getText();

        try {
            Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
            Connection c=DriverManager.getConnection("jdbc:odbc:LibraryIS");
            Statement s=c.createStatement();

        //st3=con.createStatement();
       rs=s.executeQuery("select * from AddUser where UserId='"+UserId+"'");
        if(rs.next())
        {
        }
        else
        {
        JOptionPane.showMessageDialog(rootPane, "User not Available");
        }

       rs=s.executeQuery("select * from Books where B_Id='"+B_Id+"'");

                if(rs.next())
        {
     // st.executeUpdate("UPDATE Books SET Avail='Not Available' WHERE B_Id='"+B_Id+"'");

/*    st1.executeUpdate("UPDATE IssueDetails SET UserId='"+UserId+"' where UserId='"+UserId+"'");
   st1.executeUpdate("UPDATE IssueDetails SET Bks_Tkn='"+B_Id+"' where UserId='"+UserId+"'");
    st1.executeUpdate("UPDATE IssueDetails SET date_issue='"+date_issue+"' where UserId='"+UserId+"'");
    st1.executeUpdate("UPDATE IssueDetails SET due_date='"+due_date+"' where UserId='"+UserId+"'");*/
        }
        else
        {
        JOptionPane.showMessageDialog(rootPane, "Book not Available");
        }
           //st.executeUpdate("UPDATE Books SET Avail='Not Available' WHERE B_Id='"+b_id+"'");
        }

         catch(Exception e) {
            System.out.println("Exception Raised1"+e);
        }

       try{
     st1=con.prepareStatement("INSERT INTO IssueDetails(UserId, B_Id, date_issue, due_date) VALUES(?,?,?,?)");
     st1.executeUpdate("UPDATE Books SET Avail='Not Available' WHERE B_Id='"+B_Id+"'");


         st1.setString(1,UserId);
          st1.setString(2,B_Id);
         st1.setString(3,date_issue);
         st1.setString(4,due_date);
        st1.executeUpdate();

        // st=con.createStatement();

         JOptionPane.showMessageDialog(rootPane, "Book is issued to the user!");
        }


       catch(Exception e)
      {
          System.out.println("Exception Raised2");
        }
 
Status
Not open for further replies.
Back
Top