Java JDBC MySQL Connection getcount()

Here you can find the source of getcount()

Description

getcount

License

Apache License

Declaration

public static int getcount() 

Method Source Code


//package com.java2s;
//License from project: Apache License 

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

public class Main {
    public static int getcount() {
        PreparedStatement statement = null;
        ResultSet rs = null;//from   w  ww .ja  v  a 2 s .  co m
        Connection conn = getConnection();

        try {
            String sql = "SELECT count(1) from user ";
            statement = conn.prepareStatement(sql);
            rs = statement.executeQuery();
            int count = 0;
            if (rs.next()) {
                count = rs.getInt(1);
            }
            return count;
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } finally {
            try {
                rs.close();
                statement.close();
                conn.close();
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        return 0;

    }

    public static Connection getConnection() {
        Connection conn = null;
        try {

            Class.forName("com.mysql.jdbc.Driver");

            conn = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/desk", "root", "root");
            return conn;
        } catch (Exception e) {

            e.printStackTrace();
            return null;
        }

    }
}

Related

  1. getConnection(String dbFile)
  2. getConnection(String dbUrl, String dbName, String userName, String password)
  3. getConnection(String ip, String dataBaseName, String password, String username)
  4. getConnection(String ip, String db, String user, String passWord)
  5. getConnetion(String url, String username, String password)
  6. getDBConn()
  7. getDbConnection()
  8. getDBConnection()
  9. getDBConnection(String database)