Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Java Code print to database?

Posted on 2013-06-05
8
Medium Priority
?
353 Views
Last Modified: 2013-06-10
I have a line of code in a Jar file I'm looking at in eclipse.

There is inbound data (resp) that is getting sent to a command prompt screen

String resp = cnx.getRawResponse();
System.out.println(resp);

I need some code (preferably not just a link) that will send the data to a SQL Server 2005 database stored procedure INSTEAD of the window.
0
Comment
Question by:lrbrister
[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
  • 4
  • 2
8 Comments
 
LVL 36

Accepted Solution

by:
mccarl earned 2000 total points
ID: 39224429
Firstly, you need to get a hold of the JDBC driver for SQL Server. The following link should help...

http://msdn.microsoft.com/en-us/sqlserver/aa937724.aspx

And then you will need to include the "sqljdbc.jar" file in your project. After that the following code snippet should give you the idea (proper architecture and exception handling I will leave up to you!)

String resp = cnx.getRawResponse();

Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
Connection con = DriverManager.getConnection("jdbc:sqlserver://localhost:1433;databaseName=XXXX;user=XXXX;password=XXXX");    // Change localhost, port number, and XXXX to the correct values for your database

CallableStatement cs = con.prepareCall("{call MyStoredProcedure(?)}");
cs.setString(1, resp);
cs.execute();

Open in new window

0
 
LVL 27

Expert Comment

by:mrcoffee365
ID: 39225838
You can get the specific SQL Server jdbc driver, or you can just use the jdbcodbc driver which comes with your installation of java.

You can use CallableStatement, or you can use normal jdbc connection calls.

You can call a stored procedure, but the likelihood is high that you just want to get data with straight SQL.

To use the jdbcodbc driver, you'll need to create an ODBC spec on the Windows machine for your SQL Server db.  Call the ODBC "Testdb" .  Then your code would look something like this:

import java.sql.*;

public class DatabaseExample {
  public static void main(String args[]) {
   Connection con = null;
   Statement stmt = null;
   ResultSet rs = null;

    try {
      Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
    } catch (Exception e) {
      System.out.println("JDBC-ODBC driver failed to load.");
      return;
    }

    try {
      con = DriverManager.getConnection("jdbc:odbc:Testdb", "username", "password");
      stmt = con.createStatement();
      String plainSQL = "Select Id, Name from NameTable";
     rs = stmt.executeQuery(plainSQL);
    while( rs.next() ) {
         String tableId = rs.getString("Id");
    }
    } catch (Exception e) {
      System.out.println(e);
    } finally {
       if( rs != null ) rs.close();  if( stmt != null ) stmt.close();  if( con != null ) con.close();
}
  }
}
0
 
LVL 36

Expert Comment

by:mccarl
ID: 39227747
@lrbrister - Can you explain how the accepted answer helped you to "send the data to a SQL Server 2005 database stored procedure" when it only deals with how to query data out of a database?
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 

Author Comment

by:lrbrister
ID: 39228697
mccarl

You're absolutely right.
I clicked on wrong answer by mistake.

What would you like me to do?
0
 
LVL 36

Expert Comment

by:mccarl
ID: 39229149
No worries then. There should be a "Request Attention" button above, near your original question, where you can send a message to a Moderator. They can easily re-open the question again, so that you can close in the way you intend. :)
0
 

Author Closing Comment

by:lrbrister
ID: 39234391
Thanks
0
 
LVL 36

Expert Comment

by:mccarl
ID: 39236607
No worries, glad to help. And thanks for sorting out the points, much appreciated! :)
0

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

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

This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
When trying to connect from SSMS v17.x to a SQL Server Integration Services 2016 instance or previous version, you get the error “Connecting to the Integration Services service on the computer failed with the following error: 'The specified service …
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

722 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