Advertisement
Guest User

asddasasdasd

a guest
Sep 1st, 2016
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.56 KB | None | 0 0
  1. import java.sql.*;
  2.  
  3. public class Labb {
  4.     public static void main(String[] args) {
  5.         try
  6.         {
  7.           // create the mysql database connection
  8.           String myDriver = "com.mysql.jdbc.Driver";
  9.          
  10.           //specify the database to connect
  11.           String myUrl = "jdbc:mysql://localhost/studentcontract";
  12.          
  13.           //connect to the database
  14.           Class.forName(myDriver);
  15.           Connection conn = DriverManager.getConnection(myUrl, "root", "");
  16.          
  17.           // create the SQL select statement.
  18.          
  19.                   String query = "SELECT AVG(HEIGHT) FROM student";
  20.  
  21.           // create the java statement
  22.           Statement st = conn.createStatement();
  23.          
  24.           // execute the query; the result of the execution is stored in an object of ResultSet class
  25.           ResultSet rs = st.executeQuery(query);
  26.          
  27.           // there can be more than one result. so, we have to go through each data. in each round, only one record will be readed.
  28.           while (rs.next())
  29.           {
  30.               int studentId = rs.getInt("studentId");
  31.               String firstName = rs.getString("firstname");
  32.               String lastName = rs.getString("lastname");
  33.               int age = rs.getInt("age");
  34.               float height = rs.getInt("height");
  35.  
  36.              
  37.                
  38.               System.out.format("%s\n",height);
  39.                  
  40.                
  41.           }
  42.           st.close();
  43.         }
  44.         catch (Exception e)
  45.         {
  46.           System.err.println("Got an exception! ");
  47.           System.err.println(e.getMessage());
  48.           e.printStackTrace();
  49.         }
  50.  
  51.     }
  52.  
  53. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement