Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package hello_world;
- import java.sql.*;
- import java.util.ArrayList;
- public class sqlStuff {
- /** This method returns the number of columns in a query. */
- private static int getColCount(ResultSet rs) {
- int x = 1;
- for (;x < 10; x++) {
- try {
- System.out.println(rs.getObject(x));
- } catch (SQLException e) {
- //e.printStackTrace();
- //System.out.println("Found an error at: " + x);
- break;
- }
- }
- //System.out.println("Stopping at: " + (x - 1));
- return x - 1;
- }
- /** This method returns an ArrayList of objects that can be printed, each object representing one element of the returned query. */
- private static ArrayList<Object> showObj(ResultSet rs, int columnCount) {
- System.out.println(columnCount);
- ArrayList<Object> rowEntries = new ArrayList<Object>();
- for (int x = 1; x <= columnCount; x++) {
- try {
- System.out.println(rs.getObject(x));
- rowEntries.add(rs.getObject(x));
- } catch (SQLException e) {
- //DOES NOTHING ON ERROR
- }
- }
- System.out.println(rowEntries);
- return rowEntries;
- }
- /** This method shows the results of a query. */
- public static void showResults() {
- try{
- Class.forName("com.mysql.jdbc.Driver");
- Connection con=DriverManager.getConnection(
- "jdbc:mysql://localhost:3306/employees","root","PASSWORD");
- Statement stmt=con.createStatement();
- ResultSet rs = stmt.executeQuery("select t.title, max(s.salary) from titles t join salaries s on t.emp_no = s.emp_no group by t.title;");
- int columnCount = 0;
- while(rs.next())
- columnCount = getColCount(rs);
- //columnCount = getColCount(rs);
- //System.out.println(columnCount);
- //System.out.println(getColCount(rs));
- System.out.println(showObj(rs,columnCount));
- con.close();
- }
- catch(Exception e){
- System.out.println(e);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement