Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- import java.sql.Statement;
- public class Main {
- // JDBC URL, username and password of MySQL server
- private static final String url = "jdbc:mysql://localhost:3306/java_connect";
- private static final String user = "root";
- private static final String password = "";
- // JDBC variables for opening and managing connection
- private static Connection con;
- private static Statement stmt;
- private static ResultSet rs;
- public static void main(String args[]) {
- String query = "select count(*) from books";
- try {
- // opening database connection to MySQL server
- con = DriverManager.getConnection(url, user, password);
- // getting Statement object to execute query
- stmt = con.createStatement();
- // executing SELECT query
- rs = stmt.executeQuery(query);
- while (rs.next()) {
- int count = rs.getInt(1);
- System.out.println("Total number of books in the table : " + count);
- }
- } catch (SQLException sqlEx) {
- sqlEx.printStackTrace();
- } finally {
- //close connection ,stmt and resultset here
- try { con.close(); } catch(SQLException se) { /*can't do anything */ }
- try { stmt.close(); } catch(SQLException se) { /*can't do anything */ }
- try { rs.close(); } catch(SQLException se) { /*can't do anything */ }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement