3.9

所花时间:40分钟

代码量:27

博客篇:1

了解到的知识点

android数据库连接

1.导入jar包,将jar包复制到build文件夹中,然后as library

2.建表

3.建立连接

package com.example.myapplication;

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

public class DBHelpOpen {
    private static String diver="com.mysql.jdbc.Driver";
    //加入utf-8是为了后面往表中输入中文,表中不会出现乱码的情况
    private static String url = "jdbc:mysql://localhost:3306/db1?characterEncoding=utf-8";
    private static String user = "root";//用户名
    private static String password = "1234";//密码

    public static Connection getConn(){
        Connection conn = null;
        try {
            Class.forName(diver);
            conn = (Connection) DriverManager.getConnection(url,user,password);//获取连接
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return conn;
    }
}

  

posted @ 2024-03-09 21:08  umiQa  阅读(26)  评论(0编辑  收藏  举报