码迷,mamicode.com
首页 > 数据库 > 详细

JDBC增加、更新、删除数据

时间:2018-05-07 00:47:25      阅读:215      评论:0      收藏:0      [点我收藏+]

标签:turn   ati   tco   roo   access   建立连接   col   catch   lex   

JDBC增加、更新、删除数据

st.executeUpdate(sql) 进行插入、更新、删除操作
返回的是受影响的记录的条数

注意:输入的sql语句中,vachar类型记住加单引号

完整代码如下:

public class JDBCTest {
    //建立连接
    public static Connection getConnection(){
        Connection conn=null;
        try {
            Class.forName("com.mysql.cj.jdbc.Driver").newInstance();
            conn=(Connection)DriverManager.getConnection("jdbc:mysql://localhost:3306/test?useSSL=FALSE&serverTimezone=UTC","root","xb199795");
        } catch (InstantiationException | IllegalAccessException | ClassNotFoundException | SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return conn;
    }
    //插入数据
    public static void insert() {
        String sql="insert into tbl_user(name,password,email)"+
                   "values(‘xiongda‘,‘123‘,‘xiongda@qq.com‘)";
        Connection conn =getConnection();
        try {
            Statement st=conn.createStatement();
            int count =st.executeUpdate(sql);
            System.out.println("插入了"+count+"条记录!");
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
    //更新数据
    public static void update() {
        String sql="update tbl_user set email=‘xiongda@163.com‘ where name=‘xiongda‘";
        Connection conn =getConnection();
        try {
            Statement st=conn.createStatement();
            int count =st.executeUpdate(sql);
            System.out.println("更新了"+count+"条记录!");
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
    //删除数据
    public static void delete() {
        String sql="delete from tbl_user where name=‘xiongda‘";
        Connection conn =getConnection();
        try {
            Statement st=conn.createStatement();
            int count =st.executeUpdate(sql);
            System.out.println("删除了"+count+"条记录!");
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}

 

JDBC增加、更新、删除数据

标签:turn   ati   tco   roo   access   建立连接   col   catch   lex   

原文地址:https://www.cnblogs.com/xtuxiongda/p/9000444.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!