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

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • dmstn
    New Member
    • Feb 2007
    • 16

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

    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. ;)
  • dmstn
    New Member
    • Feb 2007
    • 16

    #2
    Come on. Just tell me if this can be done and if yes, how it can be done. I need help.

    Comment

    • Banfa
      Recognized Expert Expert
      • Feb 2006
      • 9067

      #3
      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.

      Comment

      • dmstn
        New Member
        • Feb 2007
        • 16

        #4
        I didn't mean to be rude. It's just that I need help as soon as possible.

        Comment

        • JosAH
          Recognized Expert MVP
          • Mar 2007
          • 11453

          #5
          Have a look at the java.sql.Prepar edStatement interface.

          kind regards,

          Jos

          Comment

          • dmstn
            New Member
            • Feb 2007
            • 16

            #6
            Thanks for the heads up.
            I'm having a look at that at the moment. I think that the getParameterMet aData() 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:

            Code:
            import java.sql.*;
            import java.util.Scanner;
            
            class Sample {
            
                public static void main (String[] args) {
            
            
                    /* variables declaration */
                    Connection con = null ;
                    Statement stmt = null;
                    ResultSet rs = null;
                    String url = "jdbc:odbc:mydata";
                    DatabaseMetaData meta = null;
            
                    try {
                    // Step 1: Load the ODBC driver.
                        Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
                            System.out.println("Driver Loaded successfully!");
                    } catch(Exception e1) {
                            System.out.println("An error occur while loading the Driver : "+e1.getMessage());
                    System.out.println("Check ODBC");
                    }
            
                    try {
                    // Step 2: Establish the connection to the database.
                    con = DriverManager.getConnection(url,"","");
                            System.out.println("Connection Established!");
            
                    } catch(Exception e2) {
                    System.out.println("Could not establish the Connection : "+e2.getMessage());
                            System.out.println("Check username or password");
                }
            
                try {
                    // Step 3: Get the Customer Code and Delete that customer from the database
            		Scanner x = new Scanner(System.in);
            		stmt = con.createStatement();
                    rs = stmt.executeQuery("Select * from Pelates ");
                    System.out.print("Please enter Client Number:");
            		int number = x.nextInt();
            		int code = 0;
            
                    while (rs.next()) {
            		code = rs.getInt("Code_Pel");
            
            		}
            
            		if(code == number) {
            			PreparedStatement pstmt = con.prepareStatement("Delete From Pelates Where Code_Pel = ? ");
               			pstmt.setInt(1, number);
            
            			System.out.println("Client Deleted");
            		}
            
            		rs.close();
            
            
            		stmt.close();
                    con.close();
                    } catch(Exception e3) {
                    System.out.println("Could not get data : "+e3.getMessage());
                }
                }
            }
            Step 3 is important.

            Comment

            Working...