Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.sql.*;
- public class JavaToMySQL {
- private static final String url;
- static {
- url = "jdbc:mysql://localhost:3306/world?autoReconnect=true&useSSL=false";
- }
- private static final String user = "root";
- private static final String password = "root";
- private static Connection con;
- private static Statement stmt;
- private static ResultSet rs;
- public static void main(String args[]) {
- String query = "select count(*) from city";
- try {
- con = DriverManager.getConnection(url, user, password);
- stmt = con.createStatement();
- 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 {
- try { con.close(); } catch(SQLException se) { }
- try { stmt.close(); } catch(SQLException se) { }
- try { rs.close(); } catch(SQLException se) { }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement