Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

How to call a procedure in Java.

Posted on 2013-06-08
5
Medium Priority
?
364 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 1000 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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
The viewer will learn how to implement Singleton Design Pattern in Java.
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
Suggested Courses

604 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