import java.sql.*;
public class Example15_1 {
	public static void main(String[] args) {
	   try {
			//  Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/demodb", "root", "ghomi2020");//Establishing connection
        Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/University", "root", "ghomi2020");//Establishing connection

			  System.out.println("Connected With the database successfully");
			  //Creating PreparedStatement object
			  PreparedStatement preparedStatement=connection.prepareStatement("insert into Student values(?,?,?)");
			  //Setting values for Each Parameter
			  preparedStatement.setString(1,"100");
			           preparedStatement.setString(2,"Ali");
			           preparedStatement.setFloat(3, 15.5f);
			          
			           //Executing Query
			           preparedStatement.executeUpdate();
			           System.out.println("data inserted successfully");
			           
			           
			            preparedStatement=connection.prepareStatement("select * from Student");
			           //Creating Java ResultSet object
			           ResultSet resultSet=preparedStatement.executeQuery();
			           System.out.println("Stno      Name     Average");
			           while(resultSet.next()){
			                String stno = resultSet.getString("Stno");
			                String name = resultSet.getString("Name");
			                Float ave = resultSet.getFloat("Ave");
			                //Printing Results
			                System.out.println(stno + "        " + name + "     " + ave);
			           }
			           
			  } catch (SQLException e) {
			  System.out.println("Error while connecting to the database");
 	  }
	}
}