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
Solved

How to call a procedure in Java.

Posted on 2013-06-08
5
360 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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone 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

Suggested Solutions

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…
INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…

829 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