JDBC连接

复制代码
    public static void main(String[] args) {
//        JDBC
        Connection conn=null;
        Statement sta=null;
        try {
//   1.加载驱动
//       jdbc5  com.mysql.jdbc.Driver
//         jdbc8  com.mysql.cj.jdbc.Driver
        String driver_m="com.mysql.cj.jdbc.Driver";
            Class.forName(driver_m);

//  2.获取数据库连接
//            "jdbc:mysql://localhost/test01?" localhost:本地连接  test01:数据名
            String url_mysql = "jdbc:mysql://localhost/hzh0904?" + "useUnicode=ture&characterEncoding=UTF-8&useSSL=false&serverTimezone=GMT%2B8";
//            用户名
            String user_name="root";
//            密码
            String user_pass="root";
//            获取连接
            conn = DriverManager.getConnection(url_mysql,user_name,user_pass);
//   3.设置事务(非必要)
        conn.setAutoCommit(false);//手动提交
//   4.创建语句对象
            sta=conn.createStatement();

//   5.执行sql语句
// 增删改 用executeUpdate 接收用 int(影响了几条数据)
            String sql ="insert into student values (21,'一一',12,'女',90,2)";
            String sql3 ="delete from  student where id='21'";
            int count = sta.executeUpdate(sql3);
            System.out.println("插入了"+count+"条数据");
复制代码
// 查询用 executeQuery 接收用 ResultSet 
String sql="select * from student";
ResultSet re= sta.executeQuery(sql);
复制代码
//   6.提交事务
            conn.commit();








        } catch (ClassNotFoundException | SQLException e) {

            e.printStackTrace();
        }finally {
//            7.关闭连接

            try {
                sta.close();
                conn.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
    }
相关推荐
马 孔 多 在下雨4 小时前
安卓开发popupWindow的使用
android
asfdsfgas4 小时前
从 SSP 配置到 Gradle 同步:Android SDK 开发中 Manifest 合并冲突的踩坑记录
android
zhaoyufei1334 小时前
RK3399 11.0关闭调试串口改为普通RS232通信串口
android·驱动开发
消失的旧时光-19434 小时前
Kotlin 协程最佳实践:用 CoroutineScope + SupervisorJob 替代 Timer,实现优雅周期任务调度
android·开发语言·kotlin
UWA6 小时前
为什么Android游戏画面在30帧运行时有抖动现象
android·游戏
锐湃6 小时前
Android车载多媒体开发MediaSession框架理解
android
yueqc16 小时前
Android 通信机制简析
android·binder·handle
qq_7174100110 小时前
FAQ05047:在进入camera或者在camera中切换场景时,出现“很抱歉,相机已停止运行”
android
KevinWang_11 小时前
Activity Result API 的缺点
android
奔跑中的蜗牛66612 小时前
直播APP架构升级和性能优化:WebView 容器化
android