idea连mysql

操作mysql --crud:增删改查-- insert、delete、update、select
 获取连接:1.准备sql: 1.insert、delete、update 2.select
      2.执行sql: 1.insert、delete、update 2.select
DBUtil:工具类,通常都以util结尾
方法:
 获取mysql的连接、关闭连接
  加驱动:Class.forName(“com.mysql.jdbc.Driver”);(从第三方依赖包里下下来的)
 操作mysql方法、查询数据方法
  需要先准备sql连接:prepareStatement,再执行SQL:execute

/**
     * 获取mysql 连接
     * @param url
     * @param user
     * @param passwd
     * @return
     */
    public static Connection getConnection(String url, String user, String passwd){
        Connection connection=null;
        try {
            Class.forName("com.mysql.jdbc.Driver");
            try {
                connection = DriverManager.getConnection(url, user, passwd);
                //建立连接
            } catch (SQLException e) {
                e.printStackTrace();
            }
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        return connection;
    }
    /**
     * 关闭连接
     * @param conn
     */
    public static void closeConnection(Connection conn){
        if(conn !=null){
            try {
                conn.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }

 /**
     * 操作mysql
     * @param conn
     * @param sql
     */
    public static void exeSQL(Connection conn,String sql){
        System.out.println("sql=>:"+sql);
        //加一个反应,看sql语句运行到哪里
        try {
            PreparedStatement pstmt = conn.prepareStatement(sql);
            pstmt.executeUpdate();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }
    /**
     * 查询数据
     * @param conn
     * @param sql
     */
     //void→返回值类型为空,如果有返回值,则要把void换成返回的值的类型
    public static ResultSet selectSQL(Connection conn,String sql){
        System.out.println("sql=>:"+sql);
        ResultSet resultSet=null;
        try {
            PreparedStatement pstmt = conn.prepareStatement(sql);
            resultSet=pstmt.executeQuery();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return resultSet;
    }

JDBCAPP:主代码

public static void main(String[] args) {
        String url = "jdbc:mysql://ip:3306/库名";    //sql地址
        String user="root";                                  //sql用户
        String passwd = "123456";                           //sql密码
        //1.获取mysql的连接
        Connection connection = DBUtils.getConnection(url, user, passwd);
        //2.操作mysql
        //1.insert delete update
        //可以使用dbeaver快速写语法:选中你要更改的数据库下的表,右键,选择你要操作的语法,copy过来,然后更改里面的字段  
        //**注意copy时不要加分号
        //sql语句放到java里把双引号换成单引号""→'',防止符号冲突
//        String sql="INSERT INTO bigdata.emp\n" +
//                "(empno, ename, job, mgr, hiredate, sal, comm, deptno)\n" +
//                "VALUES(1234, 'zs', 'sale', 5678, current_timestamp, 100, 100, 1);\n";
//        DBUtils.exeSQL(connection,sql);

//        String sql ="UPDATE bigdata.emp\n" +
//                "SET ename='lebulang' where ename='JAMES'\n";
//        DBUtils.exeSQL(connection,sql);  //调用工具类下的方法
        //2.select查询
        String sql="SELECT empno, ename, job, mgr, hiredate, sal, comm, deptno\n" +
                "FROM bigdata.emp";
        ResultSet resultSet = DBUtils.selectSQL(connection, sql);  //调用工具类下的方法(这个方法里有返回值),返回结果
        try {
            while(resultSet.next()){       //迭代器:判断读取的下一行数据是否为空,若为空则跳出
                int empno = resultSet.getInt(1);
                String ename = resultSet.getString(2);
                String job = resultSet.getString(3);
                int mgr = resultSet.getInt(4);
                Date hiredate = resultSet.getDate(5);
                int sal = resultSet.getInt(6);
                int comm = resultSet.getInt(7);
                int deptno = resultSet.getInt(8);
                //可以全用String类型,可以全部转换成string类型输出
                System.out.println(empno+","+ename+","+job+","+mgr+","+hiredate+","+sal+","+comm+","+deptno);
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        //3.关闭连接
        DBUtils.closeConnection(connection);
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值