Question

Need help with the following: [i'm using SQL Server 2008] Create a function that accepts a...

Need help with the following: [i'm using SQL Server 2008]

Create a function that accepts a zipcode string (5 characters) as input and returns 1 if thw zipcode is in the zipcode table and 0 if it is not . also create a test file that demonstrates using the function.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

I am assuming that the table that contains zipcode information is "location_info" table. Please replace the username, password and databaseName in the defined static variables.

public class TestSQL {
  
    public static void main(String args[]) throws SQLException {
        LocationInfo tsql = new LocationInfo();
        tsql.doesZipCodeExists("12345");
    }
}


class LocationInfo {
  
    public final static String databaseName = "databaseName";
    public final static String userName = "user";
    public final static String password = "pass";
  

    public int doesZipCodeExists(String zipCode) throws SQLException {
        Statement statement = connectToDB(databaseName, userName, pass);
        ResultSet result = statement.executeQuery("SELECT count(*) as occurences FROM location_info_table WHERE zip_code = '"+zipCode+"';");

        int isZipCodeAvailable = 0;
        if (result.next()) {
            int count = result.getInt("occurencces");
            if (count >= 1) {
                isZipCodeAvailable = 1;
            }
        }
        result.close();
        statement.close();
        return isZipCodeAvailable;
    }
  
  
    public Statement connectToDB(String database, String user, String password) {
        try {

      String url = "jdbc:sqlserver://localhost:1443;databaseName="+database;
      Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
      Connection con = DriverManager.getConnection(url, user, password);

            return con.createStatement();
        } catch (ClassNotFoundException | SQLException e) {
            System.out.println(e);
        }
        return null;
    }
}

Add a comment
Know the answer?
Add Answer to:
Need help with the following: [i'm using SQL Server 2008] Create a function that accepts a...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Please I need help to create a website, using HTML5, that connects to Microsoft SQL Server...

    Please I need help to create a website, using HTML5, that connects to Microsoft SQL Server This what I came up with but I don’t know how to get it to connect to my server? Please help <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml" > <head>     <title>Untitled Page</title> <script src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>     <script>     $(document).ready(function(){         $("#Button1").click(function(){         var database = openDatabase($("#Text1").val(), "1.0", "Contact Manager", 200000);                  });       });     </script> </head> <body> <span>Enter DataBase Name: <input id="Text1" type="text" /></span> <span> <input...

  • Complete the following using Microsoft® SQL Server® 2016: Write SQL scripts in Microsoft® SQL Server® for...

    Complete the following using Microsoft® SQL Server® 2016: Write SQL scripts in Microsoft® SQL Server® for OLTP that include: A Data Definition Language (DDL) script creating the four tables with appropriate data types, primary and foreign keys Data Manipulation Language (DML) scripts that insert a minimum of five records into each table Select scripts showing the full contents of each table Write and run a test script for Step Two. Save a screenshot of the results. Create a 6-page Technical...

  • Create the function front33 that accepts a string as an argument. The first three letters of...

    Create the function front33 that accepts a string as an argument. The first three letters of the string are added to the front of the string and at the end of the string. If the string length is less than 3, use whatever chars are present. The function takes a string character and returns a new string taking the first three characters of the input string and adding them to the front and the back of the string. (must be...

  • I need help with my C homework. My teacher has developed a server that generates random...

    I need help with my C homework. My teacher has developed a server that generates random characters and numbers. I need to create a program that takes what is generated and put it in standard input form and find the amount of bytes using the sizeof() function. Also, if a random end of file occurs then it should generate a message and how many bytes have come through. Here is an example: or https://gyazo.com/3468f05cf5d50fc62d8f2ac6d988acef Please help soon! Thank You!

  • Need Java help: 1. Create a Java program that accepts input String input from a user...

    Need Java help: 1. Create a Java program that accepts input String input from a user of first name. 2. Create a Java program that accepts input String input from a user of last name. 3. Concatenate the Strings in a full name variable and print to the console.

  • Homework 4 – Create your own Function Create your own function in C that accepts one...

    Homework 4 – Create your own Function Create your own function in C that accepts one input number and returns a double number. The themes for the functions should be one of the following:  Calculates 3.14 times of the square of input number. For example, if 2 is input then 12.56 should be returned. (e.g. 3.14*2*2 = 12.56)  Divides the number by 3 and returns the result. For example, if 6 was input then 2.0 should be returned....

  • Please write the code using matlab 1) i. Create an anonymous function named optimist, that accepts...

    Please write the code using matlab 1) i. Create an anonymous function named optimist, that accepts a single variable as input i Create a row vector Tthat represents the number of seconds in two minutes, starting ii. Call the function optimist on the vector T, store the result in variable R1 and returns the double of that variable from one and ending at a hundred and twenty v. Create an anonymous function named pessimist, that accepts a single variable as...

  • 8. Exercise 5.6 Switch Statements Objective: Create a switch statement that accepts an integer input from...

    8. Exercise 5.6 Switch Statements Objective: Create a switch statement that accepts an integer input from 1 to 12 and returns a String of the related month name. For any other number, return Invalid Number. Steps: Create a file named Ex_5_6.java and appropriate class name and main() method. Create a method using the following signature: pubilc static String monthNumberToString(int monthNumber) In the main() method, add in the following code: System.out.println(monthNumberToString(1)); This will allow you to test your code. Modify the...

  • Please help! I'm really confused. Please answer question 1 with annual worth analysis and questio...

    MATLAB Please fix the error. Thank you!! Given the following piecewise function, f(x) Create a function M-file, PieceZig, that accepts one number as input, evaluates the piecewise function at that number & returns this as output. Do not allow the user to enter an armay of x values Examples Piecezig (1e) Piecezig (2) ans 2 Your Function 1 function fx PieceZ1g(x) 5 elseif ((-5 <= x)&&(x<= 5)) fx = x; 7 elseifx> 5 end end Test 1 Pretest) Test 2...

  • For this question you need to create a function in Octave called MyFunction that accepts two...

    For this question you need to create a function in Octave called MyFunction that accepts two inputs, a list L and an integer K, and returns two values minsum and maxsum. A template function including the function prototype (first line) has already been created for you to use (do not change this). I will only mark the contents of the MyFunction.m file. Any code added to the main.m file is for your testing only and will not be marked. Your...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT