2021-05-27

这篇博客介绍了如何使用Java JDBC进行数据库的基本操作,包括查询、增加、修改和删除数据。示例代码详细展示了如何连接MySQL数据库,预编译SQL语句以防止SQL注入,并通过PreparedStatement设置参数,最后执行SQL并获取结果。
摘要由CSDN通过智能技术生成

jdbc

查询语句使用

select数据库名from表名id=?来查

public static void main(String[]args){
    try{
        //加载驱动
        Class.forName("com.mysql.jdbc.Driver");
        String username = "root";
        Stirng password = "root";
        String url = "jdbc:mysql://localhost:3306/kgcnews";
        //创建链接
        Connection connection = DriverManager.getConnect(usrl,username,password);
        //SQL语句
        String sql = "select username from news_user where id = ?";
        //创建执行者
        PreparedStatement preparedStatement = connection.preparedStatement(sql);
        //设置参数
        preparedStatement.setInt(1,3);
        ResultSet resultSet = preparedStatement.executeQuery();
        while(resultSet.next()){
            System.out.print(resultSet.getString(username));
        }
    }catch (ClassNotFoundException e) {
            e.printStackTrace();
    } catch (SQLException throwables) {
            throwables.printStackTrace();
    }
}

增加数据

增加的语句是insert into 表名 value(增加的数据)

public static void main(String[]args){
    try{
        //加载驱动
        Class.forName("com.mysql.jdbc.Driver");
        String username = "root";
        Stirng password = "root";
        String url = "jdbc:mysql://localhost:3306/kgcnews";
        //创建链接
        Connection connection = DriverManager.getConnect(usrl,username,password);
        //SQL语句
        String sql = "insert into news_user value(4,'wangyuling',""12345","123@456")";
        //创建执行者
        PreparedStatement preparedStatement = connection.preparedStatement(sql);
        //设置参数
        int i = preparedStatement.executeUpdate();
        System.out.print("改变的行数有"+i)
    }catch (ClassNotFoundException e) {
            e.printStackTrace();
    } catch (SQLException throwables) {
            throwables.printStackTrace();
    }
}

修改数据

sql语句使用时update 表名 set 内容=? where id=?;

public static void main(String[]args){
    try{
        //加载驱动
        Class.forName("com.mysql.jdbc.Driver");
        String username = "root";
        Stirng password = "root";
        String url = "jdbc:mysql://localhost:3306/kgcnews";
        //创建链接
        Connection connection = DriverManager.getConnect(usrl,username,password);
        //SQL语句
        String sql = "update news_user set username=? where id=?";
        //创建执行者
        PreparedStatement preparedStatement = connection.preparedStatement(sql);
        //设置参数
        preparedStatement.setString(1,"Hello,world");
        preparedStatement.setInt(2,3)
        int i = preparedStatement.executeUpdate();
        System.out.print("改变的行数有"+i)
    }catch (ClassNotFoundException e) {
            e.printStackTrace();
    } catch (SQLException throwables) {
            throwables.printStackTrace();
    }
}

删除数据

删除数据是

delete from 表名 where id=?

public static void main(String[]args){
    try{
        //加载驱动
        Class.forName("com.mysql.jdbc.Driver");
        String username = "root";
        Stirng password = "root";
        String url = "jdbc:mysql://localhost:3306/kgcnews";
        //创建链接
        Connection connection = DriverManager.getConnect(usrl,username,password);
        //SQL语句
        String sql = "delete from news_user where id =?";
        //创建执行者
        PreparedStatement preparedStatement = connection.preparedStatement(sql);
        //设置参数
        preparedStatement.setInt(1,4)
        int i = preparedStatement.executeUpdate();
        System.out.print("改变的行数有"+i)
    }catch (ClassNotFoundException e) {
            e.printStackTrace();
    } catch (SQLException throwables) {
            throwables.printStackTrace();
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值