Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

public class Main {
    public static void main(String[] argv) throws Exception {

        int records = 0;

        Class.forName("com.mysql.jdbc.Driver");
        Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/jdbctutorial", "root", "root");

        String sql = "SELECT COUNT(*) FROM mytable";
        PreparedStatement prest = con.prepareStatement(sql);
        ResultSet rs = prest.executeQuery();
        while (rs.next()) {
            records = rs.getInt(1);
        }
        System.out.println("Number of records: " + records);
        con.close();
    }
}