• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 612
  • Last Modified:

Java SQL statements

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)
  • 2
2 Solutions
mccarlIT Business Systems Analyst / Software DeveloperCommented:
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.
DOCDGAAuthor Commented:
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.
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));      

DOCDGAAuthor Commented:
I appreciate the help
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now