Java SQL statements

Posted on 2012-09-17
Last Modified: 2012-09-19
I need help understanding how to use sql statements in java. I have been given the task of creating sql statements in java that should give a result and the next query should use the result in its where portion of its select statement.

Example: Select student_id from student_table
then use results in Select classroom from class_table where student_id = (result from first query)
Question by:DOCDGA
    LVL 35

    Assisted Solution

    Before, worrying about combining the statements, it sounds like you don't have a lot of experience with using Java and Database's in general, and so I would point you to some tutorials to get you started.

    Once you are able to just do some basic things like executing a query and getting some results, then the ultimate goal that you describe in your original question should be easy.

    Author Comment

    Thanks mcarl. I don't have any experience. I just needed some insight because this was a task I was given at work. I will look at the tutorials now.
    LVL 2

    Accepted Solution

    First copy paste below code in your IDE and add below jar file to your project.

    1> change ip to your database server ip
    2> change "dbname" to your database name
    3> change "dbusername" to your database username
    4> change "dbpassword" to your database password
    5> change both of the query and run.

    NOTE: This program is only for mysql database if your database is diff then u have to change driver name.

    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.PreparedStatement;
    import java.sql.ResultSet;
    import java.sql.SQLException;
    import java.sql.Statement;

    public class expertDemo {

           * @param args
           * @throws ClassNotFoundException
           * @throws SQLException
          public static void main(String[] args) throws ClassNotFoundException, SQLException {
                // TODO Auto-generated method stub

                Connection con = null;
                Statement  st = null;
                ResultSet  rs = null;
                ResultSet rs1 = null;
                PreparedStatement ps = null;
                con = DriverManager.getConnection("jdbc:mysql://","dbusername","dbpassword");
                st = con.createStatement();
                rs = st.executeQuery("select pluscode from icc");
                      ps = con.prepareStatement("select fname from register where countrycode = ?");
                      ps.setInt(1, rs.getInt(1));
                      System.out.println(rs.getString(1)+"query : "+ps.toString());
                      rs1 = ps.executeQuery();
                            System.out.println("final result is:"+rs1.getString(1));      


    Author Closing Comment

    I appreciate the help

    Featured Post

    Threat Intelligence Starter Resources

    Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

    Join & Write a Comment

    Suggested Solutions

    The Fluent Interface Design Pattern You can use the Fluent Interface ( design pattern to make your PHP code easier to read and maintain.  "Fluent Interface" is an object-oriented design pattern that r…
    Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
    Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
    Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:

    728 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    20 Experts available now in Live!

    Get 1:1 Help Now