Android安卓开发 数据库第三方mysql库使用教程

第三方包引入 mysql-connector-java-8.0.18.jar可以在网络上下载 

也可以关注公众号【猴头客】回复【mysql_jar】获取下载链接

下面是公众号截图

 

工具类

package com.anguo.gisapp.utils;


import com.liuan.lib.liuanlibrary.utils.ToastUtils;

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

public class DbUtils {


    private static Connection conn = null;
    static PreparedStatement statement = null;

    // connect to MySQL
    static {
        String url = "jdbc:mysql://localhost:3307/liuan_mall?characterEncoding=UTF-8&serverTimezone=Asia/Shanghai";
        String username = "root";
        String password = "root"; // 加载驱动程序以连接数据库
        try {
            Class.forName("com.mysql.jdbc.Driver");
            conn = DriverManager.getConnection(url, username, password);
        }
        //捕获加载驱动程序异常
        catch (ClassNotFoundException cnfex) {
            System.err.println(
                    "装载 JDBC/ODBC 驱动程序失败。");
            cnfex.printStackTrace();
        }
        //捕获连接数据库异常
        catch (SQLException sqlex) {
            System.err.println("无法连接数据库");
            sqlex.printStackTrace();
        }
    }

    static void close() {
        try {
            if (conn != null && !conn.isClosed()) {
                conn.close();
            }
        } catch (SQLException e) {
            System.out.println("关闭数据库问题 :");
            e.printStackTrace();
        }
    }


    // execute selection language
    static ResultSet selectSQL(String sql) {
        ResultSet rs = null;
        try {
            statement = conn.prepareStatement(sql);
            rs = statement.executeQuery(sql);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return rs;
    }

    // execute insertion language
    boolean exceSQL(String sql) {
        try {
            statement = conn.prepareStatement(sql);
            statement.executeUpdate();
            conn.commit();
            conn.close();
            return true;
        } catch (SQLException e) {
            System.out.println("插入数据库时出错:");
            e.printStackTrace();
        } catch (Exception e) {
            System.out.println("插入时出错:");
            e.printStackTrace();
        }
        return false;
    }


    public static void main(String[] args) {


        ResultSet set = selectSQL("select * from la_channel");
        try {
            while (true) {

                if (!set.next()) break;

                System.out.println(set.getString("channel") + "   " + set.getString("referer") + "   " + set.getString("create_time"));

            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        close();

    }
}

 

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

安果移不动

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值