AndroidStudio使用JDBC并利用Mysql6.0进行增删改查

AndroidStudio使用JDBC并利用Mysql6.0进行增删改查

  • 在AndroidStudio创建java项目
  • AndroidStudio引入Mysql jar包
  • 6.0正确的DBHelper写法
  • 完整代码

在AndroidStudio创建java项目

这里写图片描述

这里写图片描述

AndroidStudio引入Mysql jar包

这里写图片描述

这里写图片描述

6.0正确的DBHelper写法

多次调用失败总结出来的 这里就不吐槽了。
  String driver_url="jdbc:mysql://localhost:3306/userdb?serverTimezone=UTC&verifyServerCertificate=false&useSSL=false";

完整代码


public class DBHelper {


    private static Connection con;

    public DBHelper() {
        this.con = getConnection();
    }


    private static Connection getConnection(){

        String driver_class="com.mysql.cj.jdbc.Driver";
        String driver_url="jdbc:mysql://localhost:3306/userdb?serverTimezone=UTC&verifyServerCertificate=false&useSSL=false";
        String database_user="root";
        String database_password="119486119a";
        try {

            Class.forName(driver_class);
            con=DriverManager.getConnection(driver_url,database_user,database_password);


        } catch (ClassNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return con;
    }




    /**
     * 鎻掑叆鏁版嵁
     * @param student
     * @return
     */
    public static int insert(User student) {
        Connection conn =  getConnection();
        int i = 0;
        String sql = "insert into user (iduser,user_count) values(?,?)";
        PreparedStatement pstmt;
        try {
            pstmt = (PreparedStatement) conn.prepareStatement(sql);
            pstmt.setString(1, student.getId()+"");
            pstmt.setString(2, student.getCout_us()+"");
        //    pstmt.setString(3, student.getAge());
            i = pstmt.executeUpdate();
            pstmt.close();
            conn.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return i;



    }




    public static int update(User student) {
        Connection conn = getConnection();
        int i = 0;
        String sql = "update user set user_count='" + student.getCout_us() + "' where iduser='" + student.getId() + "'";
        PreparedStatement pstmt;
        try {
            pstmt = (PreparedStatement) conn.prepareStatement(sql);
            i = pstmt.executeUpdate();
            System.out.println("resutl: " + i);
            pstmt.close();
            conn.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return i;
    }


    public static Integer getAll() {
        Connection conn = getConnection();
        String sql = "select * from user";
        PreparedStatement pstmt;
        try {
            pstmt = (PreparedStatement)conn.prepareStatement(sql);
            ResultSet rs = pstmt.executeQuery();
            int col = rs.getMetaData().getColumnCount();
            System.out.println("============================");
            while (rs.next()) {
                for (int i = 1; i <= col; i++) {
                    System.out.print(rs.getString(i) + "\t");
                    if ((i == 2) && (rs.getString(i).length() < 8)) {
                        System.out.print("\t");
                    }
                }
                System.out.println("");
            }
            System.out.println("============================");
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }


    public static int delete(String name) {
        Connection conn = getConnection();
        int i = 0;
        String sql = "delete from user where iduser='" + name + "'";
        PreparedStatement pstmt;
        try {
            pstmt = (PreparedStatement) conn.prepareStatement(sql);
            i = pstmt.executeUpdate();
            System.out.println("resutl: " + i);
            pstmt.close();
            conn.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return i;
    }


}

main调用代码


public class MyClass {
    public static void main(String[] args) {

        DBHelper.getAll();
        DBHelper.insert(new User(7, 20));
        DBHelper.getAll();
        DBHelper.update(new User(3,10));
        DBHelper.getAll();
        DBHelper.delete(2+"");
        DBHelper.getAll();

//        UserWindow userWindow = new UserWindow();
//        userWindow.showWindow();

        System.out.println("测试中文输出");


    }

}

OK结束 博主大多数时间在 自己的博客网站上写
一般深刻点的都在上面 有兴趣的
百度搜 北京ITEBLOG 第一个就是博主的私人博客.

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值