idea的JDBC简单实现

JDBC全称Java Database Connectivity,是Java语言连接各种数据库的一个标准,今天所做的只是Java连接MySQL的简单增删改查的实现.
1.实现增删改查,我们需要知道JDBC的基本原理:
(1)加载数据库驱动
(2)建立数据库连接
(3)传输数据:创建sql语句并执行
(4)关闭连接
2.要完成对数据库的操作,我们需要一个数据库,我们需要记清的是 数据库名称和 表名以及列名,因为不同的语句对应不同的数据库名称

在这里插入图片描述
之后给这个数据库填几条记录,以便后续操作
在这里插入图片描述
3.建立一个与数据库对应的Java类,方便数据类型的操作

在这里插入图片描述
之后需要构造对应的get、set函数以进行取值写值操作
3.基本的增删改查操作的实现

//加载驱动
String driver = "com.mysql.jdbc.Driver";
//获取数据库位置,一般mysql默认端口为3306,用url,username,password做获取连接的参数
String url = "jdbc:mysql://localhost:3306/7777";
String username = "root";
String password = "123456";
Connection conn = null;
try {
    //加载对应驱动
    Class.forName(driver); 
    //建立7777数据库的连接
    conn = (Connection) DriverManager.getConnection(url, username, password);
} catch (ClassNotFoundException e) {
    e.printStackTrace();
    println("yyy!");
} catch (SQLException e) {
    e.printStackTrace();
}

(1)增加记录

//定义sql语句 insert into 表名 values(对应列信息)
String sql = "insert into 战队信息 values('"+t.getID()+"','"+t.getName()+"','"+t.getScore()+"','"+t.getCaptain()+"','"+t.getNum()+"','"+t.getRank()+"') ";
//用statement调用执行更新操作
try (Statement sta = (Statement) conn.createStatement()) {
    sta.executeUpdate(sql);
    conn.close();
}catch (SQLException e) {
    e.printStackTrace();
}~

(2)删除记录

//定义sql语句 delete from 表名 where 约束条件
String sql = "delete from 战队信息 where 战队名称='" + name + "'";
PreparedStatement pstmt;
try {
    pstmt = (PreparedStatement) conn.prepareStatement(sql);
    pstmt.executeUpdate(sql);
    pstmt.close();
    conn.close();
} catch (SQLException e) {
    e.printStackTrace();
}~

这里需要注意一下:PreparedStatement与Statement,作为练习,我使用了两种方法进行调用.
(3)查询记录
开始是一个展示所有记录

Connection conn = getConn();
//查询全部操作select * from 表名
String sql = "select * from 战队信息";
com.mysql.jdbc.Statement sta=null;
try {
    sta= (Statement) conn.createStatement();
    try (ResultSet rs = sta.executeQuery(sql)) {
        int col = rs.getMetaData().getColumnCount();
 //以为我做的表中列数太多,所以采取了result对象的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();
}~

这里有一个点值得注意一下:
executeQuery适用于select语句
execute用于其他操作
根据语句不通有两种返回结果,一种是数据集合dataset,另一种是影响的列数,所以执行此操作时我使用了resultset作为返回值
模糊查询操作

Connection conn = getConn();
//模糊查询语句 select 列名 from 表名 where 列名 like  ‘%  %’
String sql = "select * from 战队信息 where 战队名称 like '%"+str+"%'";
PreparedStatement pstmt;
try {
        pstmt =  (PreparedStatement)conn.prepareStatement(sql);
        try (ResultSet rs = pstmt.executeQuery(sql)) {
            //作为练习此处采用了另一种结果输出方式
        System.out.println("============================");
        while (rs.next()) {
            System.out.printf(rs.getString("战队编号")+"  ");
            System.out.printf(rs.getString("战队名称")+"  ");
            System.out.printf(rs.getString("战队积分")+"  ");
            System.out.printf(rs.getString("战队队长")+"  ");
            System.out.printf(rs.getString("战队人数")+"  ");
            System.out.printf(rs.getString("战队排名")+"\t");
            System.out.println("\t");
        }
        }
    System.out.println("============================");
} catch (SQLException e) {
    e.printStackTrace();
}~

(4)更改操作

String sql = "update 战队信息 set 战队积分='" + t.getScore() + "' where 战队名称='" + t.getName() + "'";
PreparedStatement pstmt;
try {
    pstmt = (PreparedStatement) conn.prepareStatement(sql);
    pstmt.executeUpdate();
    pstmt.close();
    conn.close();
} catch (SQLException e) {
    e.printStackTrace();
}~
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值