Solved

Java Code print to database?

Posted on 2013-06-05
8
330 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
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 35

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
Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

 

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 35

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 35

Expert Comment

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

Featured Post

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Returning data in a different format 8 45
triggered use of sp_send_dbmail failure 2 21
Message not shown 5 27
Sql Query Datatype 2 14
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
This video teaches viewers about errors in exception handling.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

943 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

7 Experts available now in Live!

Get 1:1 Help Now