Solved

Java Code print to database?

Posted on 2013-06-05
8
338 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 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 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
Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

 

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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
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
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.
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…

734 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