Solved

How to call a procedure in Java.

Posted on 2013-06-08
5
357 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
tx
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Fibonacci challenge 11 82
count8 challlenge 13 84
json format text only 4 65
bitbucket vs gitbucket 3 29
By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
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 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…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

744 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now