473,395 Members | 1,702 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

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

16
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. ;)
Apr 10 '07 #1
5 1989
dmstn
16
Come on. Just tell me if this can be done and if yes, how it can be done. I need help.
Apr 10 '07 #2
Banfa
9,065 Expert Mod 8TB
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.
Apr 10 '07 #3
dmstn
16
I didn't mean to be rude. It's just that I need help as soon as possible.
Apr 11 '07 #4
JosAH
11,448 Expert 8TB
Have a look at the java.sql.PreparedStatement interface.

kind regards,

Jos
Apr 11 '07 #5
dmstn
16
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.
Apr 12 '07 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: Shawn | last post by:
I am getting the following error with a Java Applet being served out by IIS over HTTPS/SSL using a Verisign certificate: java.lang.NoClassDefFoundError: javax/help/HelpSetException at...
0
by: JShurmatz | last post by:
If anyone can shed some light on this problem I would greatly appreciate it. I am unsuccessfully trying to use a database connnection retrieved from a pool configured using Java System Web...
3
by: Scott Castillo | last post by:
Is it possible to access an SQL Server database from a Javascript client? I know you can transfer variables from middleware to Javascript client, but was wondering if you can directly access...
1
by: Tracey | last post by:
I have a problem on communication between ASP.NET and Java code. Any advice will be greatly appreciated! Our core processing code and database and database server reside in a remote machine...
3
by: NWx | last post by:
Hi, I write an ASP,NET application who uses SQL Server (MSDE) as Back-end I'm an experienced developer, but haven't used SQL server before and I need some help. Since I don't have full SQL...
1
by: Paul | last post by:
Hi, I want to store particular algorithms written in Java into a database (preferably MySQL) and then be able to accept values such as algorithm type and parameters from a user (via JDBC) which...
0
by: news.onet.pl | last post by:
I hava a problem with communication between Java/Corba server based on JDK ORB with Java/Corba client (applet) based on the same ORB. I`m using IOR to localize server. client`s ORB i initialize...
4
by: lligogogo | last post by:
Hi, I am new on java function. I created a FORM in asp page, the FORM have three switch. When user selects UPDATE, first thing have to validate is check if this user id is in the control table or...
9
by: TC | last post by:
Like a lot of database developers, I often choose Jet for the back- end. I'm starting to worry about what will happen when Jet is deprecated. Ostensibly, Jet users like me must switch to SQL Server...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.