• 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)
0
DOCDGA
Asked:
DOCDGA
  • 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.
0
 
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.
0
 
hforhirenpatelCommented:
First copy paste below code in your IDE and add below jar file to your project.
1>mysql-connector-java-5.1.7-bin.jar
2>jxl.jar
3>slf4j-log4j12-1.5.11.jar

note:
1> change 172.18.100.67 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;
            
            Class.forName("com.mysql.jdbc.Driver");
            con = DriverManager.getConnection("jdbc:mysql://172.18.100.67:3306/dbname","dbusername","dbpassword");
            st = con.createStatement();
            
            rs = st.executeQuery("select pluscode from icc");
            
            while(rs.next()){
                  
                  System.out.println(rs.getString(1));
                  
                  ps = con.prepareStatement("select fname from register where countrycode = ?");
                  
                  System.out.println(rs.getString(1));
                  
                  ps.setInt(1, rs.getInt(1));
                  
                  System.out.println(rs.getString(1)+"query : "+ps.toString());
                  
                  rs1 = ps.executeQuery();
                  
                  while(rs1.next()){
                        System.out.println("final result is:"+rs1.getString(1));      
                  }
                  
                  
            }
            
            
            
      }

}
0
 
DOCDGAAuthor Commented:
I appreciate the help
0
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