java操作mysql实例 让代码跑起来

用java操作mysql实例,代码测试通过无误,将数据库换成相应的配置即可
让代码先跑起来再说!

package mysql;

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


public class Demo01 {

    public static void testJDBC() {
        try {
            Class.forName("com.mysql.jdbc.Driver");
            System.out.println("OK,Find the Driver");
        } catch (ClassNotFoundException e) {
            System.out.println("找不到驱动类,加载驱动失败");
            e.printStackTrace();
        }
    }

    public static void connectMysql() {
        String url = "jdbc:mysql://192.168.16.241:3306/xxx";
        String username = "root";
        String password = "xxx";
        try {
            Connection con = DriverManager.getConnection(url,username,password);
            System.out.println("-----Connection OK------");

            Statement statement = con.createStatement();
            String sql = "select * from stg_ad_uv_day where log_date='2015-01-15'";
            ResultSet res = statement.executeQuery(sql);
            String uv_type = null;
            String adp_type = null;
            while(res.next()) {
                uv_type = res.getString("uv_type");
                adp_type = res.getString("adp_type");
                System.out.println("uv_type:" + uv_type + "\t" + "adp_type:" + adp_type);
            }

        } catch (Exception e) {
            System.out.println("数据库连接失败");
            e.printStackTrace();
        }
    }

    public static void findCount() {
        String url = "jdbc:mysql://192.168.16.241:3306/microlens_master";
        String username = "root";
        String password = "microlens";
        try {
            Connection con = DriverManager.getConnection(url,username,password);
            System.out.println("-----Connection OK------");

            Statement statement = con.createStatement();
            String sql = "select count(*) from stg_ad_uv_day where log_date='2015-01-15'";
            ResultSet res = statement.executeQuery(sql);
            while (res.next()) {
                int Num = res.getInt(1);
                System.out.println(Num);
            }

            if (res != null) { //关闭记录集
                try {
                    res.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            if(statement != null) { //关闭声明
                try {
                    statement.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            if(con != null) { //关闭连接对象
                try {
                    con.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        } catch (Exception e) {
            System.out.println("数据库连接失败");
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        findCount();

    }
}

你可能感兴趣的:(java,mysql,数据库,实例)