Solved

How to call a procedure in Java.

Posted on 2013-06-08
5
359 Views
Last Modified: 2013-06-29
How to call a procedure in java/
How to test whether a connection is open or closed.
0
Comment
Question by:searchsanjaysharma
  • 3
5 Comments
 
LVL 4

Expert Comment

by:MrC63
ID: 39231240
This is a bit obscure, can you elaborate further?  

- What type of connection are you attempting to test to see if it's open or closed.  
- Are you trying to do this with a Java applet, or with JavaScript in an HTML page?
0
 

Author Comment

by:searchsanjaysharma
ID: 39231428
I am sending the example in dot net.
I want to check whether the connection is closed, if so then only open it.
Secondly how to call a procedure.
0
 

Author Comment

by:searchsanjaysharma
ID: 39231430
Try
            con = New SqlConnection("Data Source=192.168.171.41;Initial Catalog=amis;User Id=sa;Password=XXXX")
            cmd = New SqlCommand
            cmd.CommandType = CommandType.StoredProcedure
            cmd.Connection = con
            cmd.CommandText = "saveuser"
            If con.State = ConnectionState.Open Then con.Close()
            con.Open()
            cmd.Parameters.AddWithValue("@username", txtusername.Text.Trim())
            cmd.Parameters.AddWithValue("@password", txtpassword.Text.Trim())
            cmd.ExecuteNonQuery()
            MessageBox.Show("Record Saved Sucessfully")
        Catch eee As Exception
            MessageBox.Show("Error occured" & eee.ToString())
        Finally
            con.Close()
        End Try
0
 
LVL 16

Accepted Solution

by:
Valeri earned 500 total points
ID: 39233931
1. Connection should be taken in this way:

Context ictx = new InitialContext();
DataSource connectionBroker = (DataSource) ictx.lookup("java:comp/env/jdbc/nameOfYourConnection");
conn = connectionBroker.getConnection();

where "java:comp/env/jdbc/nameOfYourConnection" is the JNDI name describing your connection.

2.
CallableStatement cstmt = null;
          try {
               cstmt = conn.prepareCall("begin yourProcedureName(?,?,?); end;");
               cstmt.setLong(1, param1);
               cstmt.setString(2, param2);
               cstmt.setInt(3, param3);
               cstmt.executeQuery();
           } catch(SQLException e) {
              e.printStackTrace();
           } finally {
                if(cstmt != null) try {cstmt.close();} catch (SQLException e) {e.printStackTrace();}
           }

this is how to call procedure with three parameters: Long, String, Integer

About open and close state of connection I'm using a different approach.
1. getting the connection
2. using this connection a lot of times and finally when I don't need this connection any more I colse it in a finally block, for example at the end of the jsp. So I get it at the beginning of the jsp an close it at the end of the jsp, and it works perfect.
0
 

Author Closing Comment

by:searchsanjaysharma
ID: 39286501
tx
0

Featured Post

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question