Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.sql.*;
- public class Labb {
- public static void main(String[] args) {
- try
- {
- // create the mysql database connection
- String myDriver = "com.mysql.jdbc.Driver";
- //specify the database to connect
- String myUrl = "jdbc:mysql://localhost/studentcontract";
- //connect to the database
- Class.forName(myDriver);
- Connection conn = DriverManager.getConnection(myUrl, "root", "");
- // create the SQL select statement.
- String query = "SELECT * FROM student";
- // create the java statement
- Statement st = conn.createStatement();
- // execute the query; the result of the execution is stored in an object of ResultSet class
- ResultSet rs = st.executeQuery(query);
- // there can be more than one result. so, we have to go through each data. in each round, only one record will be readed. int count=0;
- int count=0;
- double sum =0;
- while (rs.next())
- {
- double height = (double) rs.getDouble("height");
- sum=sum+height;
- count++;
- }
- sum=sum/count;
- System.out.print(sum);
- st.close();
- }
- catch (Exception e)
- {
- System.err.println("Got an exception! ");
- System.err.println(e.getMessage());
- e.printStackTrace();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement