sign in | join about | help | sitemap
Connecting Tech Pros Worldwide
dmstn's Avatar

Use of Java Variables In A SQL Server Database. Help!!!


Question posted by: dmstn (Newbie) on April 10th, 2007 12:40 PM
My question is:
Is there a way to use the value of a java variable in a SQL query??
I mean. There is a java program. Can I retrieve the value of that variable and use it in a SQL query? Your help will be so much appreciated.

Thanks in advance. ;)
5 Answers Posted
dmstn's Avatar
dmstn April 10th, 2007 04:51 PM
Newbie - 16 Posts
#2: Re: Use of Java Variables In A SQL Server Database. Help!!!

Come on. Just tell me if this can be done and if yes, how it can be done. I need help.
Banfa's Avatar
Banfa April 10th, 2007 09:54 PM
AdministratorVoR - 5,249 Posts
#3: Re: Use of Java Variables In A SQL Server Database. Help!!!

Quote:
Originally Posted by dmstn
Come on. Just tell me if this can be done and if yes, how it can be done. I need help.
Please do not be impatient, it is rude. Please do remember that the people answering you are not necessarily in the same timezone as you are.
dmstn's Avatar
dmstn April 11th, 2007 10:28 AM
Newbie - 16 Posts
#4: Re: Use of Java Variables In A SQL Server Database. Help!!!

I didn't mean to be rude. It's just that I need help as soon as possible.
JosAH's Avatar
JosAH April 11th, 2007 11:46 AM
Moderator - 8,354 Posts
#5: Re: Use of Java Variables In A SQL Server Database. Help!!!

Have a look at the java.sql.PreparedStatement interface.

kind regards,

Jos
dmstn's Avatar
dmstn April 12th, 2007 10:26 AM
Newbie - 16 Posts
#6: Re: Use of Java Variables In A SQL Server Database. Help!!!

Thanks for the heads up.
I'm having a look at that at the moment. I think that the getParameterMetaData() function could help me retrieve the value of that variable and use it in the SQL query. I tried to use it but I can't find a solution. Okay I'll post my code:

This java program connects to a SQL Server Database. It wants us to insert a Customer Code and then delete that customer. That's what I've done so far:

Expand|Select|Wrap|Line Numbers
  1. import java.sql.*;
  2. import java.util.Scanner;
  3.  
  4. class Sample {
  5.  
  6.     public static void main (String[] args) {
  7.  
  8.  
  9.         /* variables declaration */
  10.         Connection con = null ;
  11.         Statement stmt = null;
  12.         ResultSet rs = null;
  13.         String url = "jdbc:odbc:mydata";
  14.         DatabaseMetaData meta = null;
  15.  
  16.         try {
  17.         // Step 1: Load the ODBC driver.
  18.             Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
  19.                 System.out.println("Driver Loaded successfully!");
  20.         } catch(Exception e1) {
  21.                 System.out.println("An error occur while loading the Driver : "+e1.getMessage());
  22.         System.out.println("Check ODBC");
  23.         }
  24.  
  25.         try {
  26.         // Step 2: Establish the connection to the database.
  27.         con = DriverManager.getConnection(url,"","");
  28.                 System.out.println("Connection Established!");
  29.  
  30.         } catch(Exception e2) {
  31.         System.out.println("Could not establish the Connection : "+e2.getMessage());
  32.                 System.out.println("Check username or password");
  33.     }
  34.  
  35.     try {
  36.         // Step 3: Get the Customer Code and Delete that customer from the database
  37.         Scanner x = new Scanner(System.in);
  38.         stmt = con.createStatement();
  39.         rs = stmt.executeQuery("Select * from Pelates ");
  40.         System.out.print("Please enter Client Number:");
  41.         int number = x.nextInt();
  42.         int code = 0;
  43.  
  44.         while (rs.next()) {
  45.         code = rs.getInt("Code_Pel");
  46.  
  47.         }
  48.  
  49.         if(code == number) {
  50.             PreparedStatement pstmt = con.prepareStatement("Delete From Pelates Where Code_Pel = ? ");
  51.                pstmt.setInt(1, number);
  52.  
  53.             System.out.println("Client Deleted");
  54.         }
  55.  
  56.         rs.close();
  57.  
  58.  
  59.         stmt.close();
  60.         con.close();
  61.         } catch(Exception e3) {
  62.         System.out.println("Could not get data : "+e3.getMessage());
  63.     }
  64.     }


Step 3 is important.
Reply
Not the answer you were looking for? Post your question . . .
196,952 members ready to help you find a solution.
Join Bytes.com

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 196,952 network members.
Post your question now . . .
It's fast and it's free

Popular Articles

Top Java Contributors