Question:

How do I do an SQL Server query in java?

by  |  earlier

0 LIKES UnLike

After I connect to SQL Server using JDBC and Java, how do I then execute a query in Java. Is there a tutorial that will show me how?

 Tags:

   Report

3 ANSWERS


  1. You can try this page for some guidance:

    http://sqlzoo.net/java.htm


  2. yes there are tuts

    http://www.tutorialized.com/view/tutoria...


  3. Do it like...

    Statement stmt      = null;

            ResultSet rset      = null;

            String queryString  = "SELECT name, date_of_hire, monthly_salary " +

                                  "FROM   emp " +

                                  "ORDER BY name";

            try {

                System.out.print("  Creating Statement...\n");

                stmt = con.createStatement ();

                System.out.print("  Opening ResultsSet...\n");

                rset = stmt.executeQuery(queryString);

                int counter = 0;

                

                while (rset.next()) {

                    System.out.println();

                    System.out.println("  Row [" + ++counter + "]");

                    System.out.println("  ---------------------");

                    System.out.println("      Name             -> " + rset.getString(1));

                    System.out.println("      Date of Hire     -> " + rset.getString(2));

                    System.out.println("      Monthly Salary   -> " + rset.getFloat(3));

                }

                System.out.println();

                System.out.print("  Closing ResultSet...\n");

                rset.close();

                System.out.print("  Closing Statement...\n");

                stmt.close();

            } catch (SQLException e) {

                e.printStackTrace();

            }

    Do not forget to close connections

    Cheers:)

Question Stats

Latest activity: earlier.
This question has 3 answers.

BECOME A GUIDE

Share your knowledge and help people by answering questions.