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

Deadly 提交于 2019-11-28 05:09:02

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 第一个就是博主的私人博客.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!