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();

    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值