import java.sql.*;
public class my_connect {
public static void main(String args[])
{
try{
Class.forName("com.mysql.jdbc.Driver");
Connection con=DriverManager.getConnection(
"jdbc:mysql://localhost:3306/my_tutorial","root","test");
Statement st=con.createStatement();
// Displaying class with number of records
String query1="SELECT class, count(*) as no FROM student GROUP BY class";
ResultSet rs=st.executeQuery(query1);
// Listing class with number of records before the update
while(rs.next())
System.out.println(rs.getString(1)+" "+rs.getInt("no"));
//Update the records
String query2="UPDATE student set class='Seven' where class='Six'";
int no_rows=st.executeUpdate(query2);
System.out.println(no_rows + " rows updated..");
//isting of class with number of records after the update
String query3="SELECT class, count(*) as no FROM student GROUP BY class";
rs=st.executeQuery(query3);
// Listing class with number of records before the update
while(rs.next())
System.out.println(rs.getString(1)+" "+rs.getInt("no"));
con.close();
}catch(Exception e){ System.out.println(e);}
}
}
The output is here
Eight 1
Five 3
Four 9
Nine 2
Seven 10
Six 7
Three 3
7 rows updated..
Eight 1
Five 3
Four 9
Nine 2
Seven 17
Three 3
You can see query1 is used to display the classes with number of rows before the update. Author & Instructor at plus2net
I write and maintain practical tutorials on Python, PHP, SQL, JavaScript, HTML, jQuery, and web development at plus2net. The tutorials focus on clear explanations, working examples, and code that readers can test and adapt while learning.