java数据库连接


一、查询数据库的连接。

代码:

package com.xxx.jdbc;

import java.sql.*;

public class TestJdbc {
    public static void main(String[] args) throws ClassNotFoundException, SQLException {
        //配置信息
        String url="jdbc:mysql://localhost:3306/jdbc?useUnicode=true&characterEncoding=utf-8";
        String username="root";
        String password="1127";
        //1.加载驱动
        Class.forName("com.mysql.cj.jdbc.Driver");
        //连接数据库,代表数据库
        Connection connection= DriverManager.getConnection(url,username,password);
        //3.向数据库发送SQL的对象Statement:CRUD
        Statement statement=connection.createStatement();
        //4.编译SQL
         String sql="select *from users";
       String sql1="delete from users where id=4";
       //受影响的行数,增删改都是用executeUpdate即可
       // int i=statement.executeUpdate(sql1);
        //5.执行查询sql,返回一个ResultSet:结果集
        ResultSet resultSet= statement.executeQuery(sql);
        while(resultSet.next()){
            System.out.println("id+"+resultSet.getObject("id"));
            System.out.println("name+"+resultSet.getObject("name"));
            System.out.println("password+"+resultSet.getObject("password"));
            System.out.println("email+"+resultSet.getObject("email"));
            System.out.println("birthday+"+resultSet.getObject("birthday"));
        }
        //6.关闭连接,释放资源
        resultSet.close();
        statement.close();
        connection.close();
    }
}

二、增删改数据库

代码:

package com.xxx.jdbc;
import java.sql.*;
public class TestJDBC3 {
    public static void main(String[] args) throws ClassNotFoundException, SQLException {
        //配置信息
        String url="jdbc:mysql://localhost:3306/jdbc?useUnicode=true&characterEncoding=utf-8";
        String username="root";
        String password="1127";
        //加载驱动
        Class.forName("com.mysql.cj.jdbc.Driver");
        //连接数据库
        Connection connection=DriverManager.getConnection(url,username,password);
        //执行sql语句
         String  sql="insert into users(id,name,password,email,birthday) values(?,?,?,?,?)";
        //预编译
        PreparedStatement preparedStatement=connection.prepareStatement(sql);
        preparedStatement.setInt(1,4);
        preparedStatement.setString(2,"狂神焦");
        preparedStatement.setString(3,"123456");
        preparedStatement.setString(4,"2728443650@qq.com");
        preparedStatement.setDate(5,new Date(new java.util.Date().getTime()));
        Statement statement=connection.createStatement();
        //执行sql
        int i=preparedStatement.executeUpdate();
        if(i>0){
            System.out.println("插入成功@");
        }
        //6.关闭连接,释放资源(一定要做)先开后关
        preparedStatement.close();
        connection.close();
    }
}

三、事务数据库

代码

package com.xxx.jdbc;

import java.lang.reflect.Parameter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;

public class TestJDBC2 {
    public static void main(String[] args) throws ClassNotFoundException, SQLException {
        //配置信息
        String url="jdbc:mysql://localhost:3306/jdbc?useUnicode=true&characterEncoding=utf-8";
        String username="root";
        String password="1127";
        //加载驱动
        Class.forName("com.mysql.cj.jdbc.Driver");
        //连接数据库
        Connection connection= DriverManager.getConnection(url,username,password);
        //通知数据库开启事务,false开启
        connection.setAutoCommit(false);

        String sql="update account set money=money-100 where name='A'";

        PreparedStatement preparedStatement=connection.prepareStatement(sql);
        preparedStatement.executeUpdate();

        connection.commit();//以上两条sql都执行成功了,就提交事务
        System.out.println("success");

        connection.rollback();//如果出现异常,就通知数据库回滚事务

    }
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值