Solved

How to call a procedure in Java.

Posted on 2013-06-08
5
361 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 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: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

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

Suggested Solutions

Title # Comments Views Activity
tomcat administrtor 12 68
Facing this issue for maven proxy setting 2 27
Crystal Reports Licensing Questions 4 65
Java Inheritance super keyword use 8 38
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…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
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.

726 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