MySQL jdbc增删改查

步骤

public class MySql {

    public static void main(String[] args) {
        //连接数据库的驱动
        String driver = "com.mysql.jdbc.Driver";
        //URL指向要访问的数据库名
        String url = "jdbc:mysql://localhost:3306/calzz";
        //MYSQL的用户名
        String user = "root";
        //MySQL的密码
        String password = "123456";

        try {
            Class.forName(driver);//加载驱动
            Connection connection = DriverManager.getConnection(url,user,password);//连接数据库
            if(!connection.isClosed()){
                //数据库操作类
                Statement statement = connection.createStatement();
                String sql;
                //执行语句
                //增
//              sql = "insert into student (name,age,sex) values('李四',23,1)";
//              statement.execute(sql);
//              //删
//              sql = "delete from student where name = '张三'";
//              statement.execute(sql);
                //改
//              sql = "update student set age = 23,sex = 0 where name ='李四'";
//              statement.execute(sql);
                //查     使用其他方法
                sql = "select name,age,sex from student where age>10";
//              statement.execute(sql);
                ResultSet resultSet = statement.executeQuery(sql);
                resultSet.first();//先把游标移到第一位
                while(!resultSet.isAfterLast()){//判断是否移到最后
                    String name = resultSet.getString("age");
                    System.out.println(name);
                    resultSet.next();
                }
                statement.close();
                connection.close();
            }else{
                System.out.println("请打开数据库连接");
            }
        } catch (ClassNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
可以通过 JDBC 连接 MySQL 数据库,并实现增删改查操作。以下是一个简单的示例代码: ```java import java.sql.*; public class JdbcExample { static final String JDBC_DRIVER = "com.mysql.jdbc.Driver"; static final String DB_URL = "jdbc:mysql://localhost:3306/test"; static final String USER = "root"; static final String PASS = "password"; public static void main(String[] args) { Connection conn = null; Statement stmt = null; try { // 注册 JDBC 驱动器 Class.forName(JDBC_DRIVER); // 打开连接 System.out.println("连接数据库..."); conn = DriverManager.getConnection(DB_URL, USER, PASS); // 执行查询 System.out.println("创建查询..."); stmt = conn.createStatement(); String sql = "SELECT id, name, age FROM student"; ResultSet rs = stmt.executeQuery(sql); // 处理结果集 while (rs.next()) { // 获取数据 int id = rs.getInt("id"); String name = rs.getString("name"); int age = rs.getInt("age"); // 显示数据 System.out.print("ID: " + id); System.out.print(", Name: " + name); System.out.println(", Age: " + age); } // 关闭连接 rs.close(); stmt.close(); conn.close(); } catch (SQLException se) { // 处理 JDBC 错误 se.printStackTrace(); } catch (Exception e) { // 处理 Class.forName 错误 e.printStackTrace(); } finally { // 关闭资源 try { if (stmt != null) { stmt.close(); } } catch (SQLException se) { } try { if (conn != null) { conn.close(); } } catch (SQLException se) { se.printStackTrace(); } } } } ``` 上述代码实现了查询操作,如果要实现增删改操作可以使用 PreparedStatement 来执行 SQL 语句。例如,插入数据的示例代码如下: ```java // 创建 PreparedStatement 对象 String sql = "INSERT INTO student (name, age) VALUES (?, ?)"; PreparedStatement pstmt = conn.prepareStatement(sql); // 设置参数 pstmt.setString(1, "Tom"); pstmt.setInt(2, 20); // 执行更新 int rows = pstmt.executeUpdate(); // 关闭资源 pstmt.close(); ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值