Solved

Java Code print to database?

Posted on 2013-06-05
8
327 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
  • 4
  • 2
8 Comments
 
LVL 35

Accepted Solution

by:
mccarl earned 500 total points
Comment Utility
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 26

Expert Comment

by:mrcoffee365
Comment Utility
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 35

Expert Comment

by:mccarl
Comment Utility
@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
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 

Author Comment

by:lrbrister
Comment Utility
mccarl

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

What would you like me to do?
0
 
LVL 35

Expert Comment

by:mccarl
Comment Utility
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
Comment Utility
Thanks
0
 
LVL 35

Expert Comment

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

Featured Post

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Join & Write a Comment

This article is meant to give a basic understanding of how to use R Sweave as a way to merge LaTeX and R code seamlessly into one presentable document.
JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

771 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

12 Experts available now in Live!

Get 1:1 Help Now