jdbc 操作 mysql 增删改查

public class AtfireDao {    
    // 增加操作    
    public void insert(Note note) throws Exception {    
        String sql = "INSERT INTO note(id,title,author,content) VALUES(note_sequ.nextVal,?,?,?)" ;    
        PreparedStatement pstmt = null ;    
        DataBaseConnection dbc = null ;    
        dbc = new DataBaseConnection() ;    
        try {    
            pstmt = dbc.getConnection().prepareStatement(sql) ;    
            pstmt.setString(1,note.getTitle()) ;    
            pstmt.setString(2,note.getAuthor()) ;    
            pstmt.setString(3,note.getContent()) ;    
            pstmt.executeUpdate() ;    
            pstmt.close() ;    
        } catch (Exception e) {    
            // System.out.println(e) ;    
            throw new Exception("操作中出现错误!!!") ;    
        } finally {    
            dbc.close() ;    
        }    
    }    
    // 修改操作    
    public void update(Note note) throws Exception {    
        String sql = "UPDATE note SET title=?,author=?,content=? WHERE id=?" ;    
        PreparedStatement pstmt = null ;    
        DataBaseConnection dbc = null ;    
        dbc = new DataBaseConnection() ;    
        try {    
            pstmt = dbc.getConnection().prepareStatement(sql) ;    
            pstmt.setString(1,note.getTitle()) ;    
            pstmt.setString(2,note.getAuthor()) ;    
            pstmt.setString(3,note.getContent()) ;    
            pstmt.setInt(4,note.getId()) ;    
            pstmt.executeUpdate() ;    
            pstmt.close() ;    
        } catch (Exception e) {    
            throw new Exception("操作中出现错误!!!") ;    
        } finally {    
            dbc.close() ;    
        }    
    }    
    // 删除操作    
    public void delete(int id) throws Exception {    
        String sql = "DELETE FROM note WHERE id=?" ;    
        PreparedStatement pstmt = null ;    
        DataBaseConnection dbc = null ;    
        dbc = new DataBaseConnection() ;    
        try {    
            pstmt = dbc.getConnection().prepareStatement(sql) ;    
            pstmt.setInt(1,id) ;    
            pstmt.executeUpdate() ;    
            pstmt.close() ;    
        } catch (Exception e) {    
            throw new Exception("操作中出现错误!!!") ;    
        } finally {    
            dbc.close() ;    
        }    
    }    
    // 按ID查询,主要为更新使用    
    public Note queryById(int id) throws Exception {    
        Note note = null ;    
        String sql = "SELECT id,title,author,content FROM note WHERE id=?" ;    
        PreparedStatement pstmt = null ;    
        DataBaseConnection dbc = null ;    
        dbc = new DataBaseConnection() ;    
        try {    
            pstmt = dbc.getConnection().prepareStatement(sql) ;    
            pstmt.setInt(1,id) ;    
            ResultSet rs = pstmt.executeQuery() ;    
            if(rs.next()) {    
                note = new Note() ;    
                note.setId(rs.getInt(1)) ;    
                note.setTitle(rs.getString(2)) ;    
                note.setAuthor(rs.getString(3)) ;    
                note.setContent(rs.getString(4)) ;    
            }    
            rs.close() ;    
            pstmt.close() ;    
        } catch (Exception e) {    
            throw new Exception("操作中出现错误!!!") ;    
        } finally {    
            dbc.close() ;    
        }    
        return note ;    
    }    
    // 查询全部    
    public List queryAll() throws Exception {    
        List all = new ArrayList() ;    
        String sql = "SELECT id,title,author,content FROM note" ;    
        PreparedStatement pstmt = null ;    
        DataBaseConnection dbc = null ;    
        dbc = new DataBaseConnection() ;    
        try {    
            pstmt = dbc.getConnection().prepareStatement(sql) ;    
            ResultSet rs = pstmt.executeQuery() ;    
            while(rs.next()) {    
                Note note = new Note() ;    
                note.setId(rs.getInt(1)) ;    
                note.setTitle(rs.getString(2)) ;    
                note.setAuthor(rs.getString(3)) ;    
                note.setContent(rs.getString(4)) ;    
                all.add(note) ;    
            }    
            rs.close() ;    
            pstmt.close() ;    
        } catch (Exception e) {    
            System.out.println(e) ;    
            throw new Exception("操作中出现错误!!!") ;    
        } finally {    
            dbc.close() ;    
        }    
        return all ;    
    }    
    // 模糊查询    
    public List queryByLike(String cond) throws Exception {    
        List all = new ArrayList() ;    
        String sql = "SELECT id,title,author,content FROM note WHERE title LIKE ? or AUTHOR LIKE ? or CONTENT LIKE ?" ;    
        PreparedStatement pstmt = null ;    
        DataBaseConnection dbc = null ;    
        dbc = new DataBaseConnection() ;    
        try {    
            pstmt = dbc.getConnection().prepareStatement(sql) ;    
            pstmt.setString(1,"%"+cond+"%") ;    
            pstmt.setString(2,"%"+cond+"%") ;    
            pstmt.setString(3,"%"+cond+"%") ;    
            ResultSet rs = pstmt.executeQuery() ;    
            while(rs.next()) {    
                Note note = new Note() ;    
                note.setId(rs.getInt(1)) ;    
                note.setTitle(rs.getString(2)) ;    
                note.setAuthor(rs.getString(3)) ;    
                note.setContent(rs.getString(4)) ;    
                all.add(note) ;    
            }    
            rs.close() ;    
            pstmt.close() ;    
        } catch (Exception e) {    
            System.out.println(e) ;    
            throw new Exception("操作中出现错误!!!") ;    
        } finally {    
            dbc.close() ;    
        }    
        return all ;    
    }    
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是jdbc连接MySQL进行增删改查的示例: 1. 连接MySQL数据库 ```java import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException; public class MySQLConnection { public static void main(String[] args) { String url = "jdbc:mysql://localhost:3306/zlr?serverTimezone=GMT&useSSL=false"; String username = "your_username"; String password = "your_password"; try { Connection connection = DriverManager.getConnection(url, username, password); System.out.println("Connected to MySQL database!"); } catch (SQLException e) { System.out.println("Connection failed! Error: " + e.getMessage()); } } } ``` 2. 插入数据 ```java import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.SQLException; public class InsertData { public static void main(String[] args) { String url = "jdbc:mysql://localhost:3306/zlr?serverTimezone=GMT&useSSL=false"; String username = "your_username"; String password = "your_password"; try { Connection connection = DriverManager.getConnection(url, username, password); String insertQuery = "INSERT INTO your_table (column1, column2, column3) VALUES (?, ?, ?)"; PreparedStatement preparedStatement = connection.prepareStatement(insertQuery); preparedStatement.setString(1, "value1"); preparedStatement.setString(2, "value2"); preparedStatement.setString(3, "value3"); int rowsAffected = preparedStatement.executeUpdate(); System.out.println(rowsAffected + " row(s) inserted!"); } catch (SQLException e) { System.out.println("Insert failed! Error: " + e.getMessage()); } } } ``` 3. 更新数据 ```java import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.SQLException; public class UpdateData { public static void main(String[] args) { String url = "jdbc:mysql://localhost:3306/zlr?serverTimezone=GMT&useSSL=false"; String username = "your_username"; String password = "your_password"; try { Connection connection = DriverManager.getConnection(url, username, password); String updateQuery = "UPDATE your_table SET column1 = ? WHERE condition_column = ?"; PreparedStatement preparedStatement = connection.prepareStatement(updateQuery); preparedStatement.setString(1, "new_value"); preparedStatement.setString(2, "condition_value"); int rowsAffected = preparedStatement.executeUpdate(); System.out.println(rowsAffected + " row(s) updated!"); } catch (SQLException e) { System.out.println("Update failed! Error: " + e.getMessage()); } } } ``` 4. 删除数据 ```java import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.SQLException; public class DeleteData { public static void main(String[] args) { String url = "jdbc:mysql://localhost:3306/zlr?serverTimezone=GMT&useSSL=false"; String username = "your_username"; String password = "your_password"; try { Connection connection = DriverManager.getConnection(url, username, password); String deleteQuery = "DELETE FROM your_table WHERE condition_column = ?"; PreparedStatement preparedStatement = connection.prepareStatement(deleteQuery); preparedStatement.setString(1, "condition_value"); int rowsAffected = preparedStatement.executeUpdate(); System.out.println(rowsAffected + " row(s) deleted!"); } catch (SQLException e) { System.out.println("Delete failed! Error: " + e.getMessage()); } } } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值