zoukankan      html  css  js  c++  java
  • 常用jdbc操作

    Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
       Connection con = DriverManager.getConnection("jdbc:sqlserver://localhost:1433;Databasename=epet","sa","sa");
       String sql= "insert into title(Id,TitleName,FilePath,Creator) values(?,?,?,?,)";
       PreparedStatement pStatement = con.prepareStatement(sql);
       pStatement.setInt(1, value);
       pStatement.setString(2, value);
       pStatement.setString(3, value);
       pStatement.setString(4, value);
       int row = pStatement.executeUpdate();//增删改
       ResultSet rs = pStatement.executeQuery();//查询

    ResultSetMetaData用法:

    Map<String, String> mm = new HashMap<String, String>();
    Connection conn = ConnectionManager.getInstance().getConnction();
                 //查出列名和注释
                String sql = "select  a.column_name,a.comments  from  user_col_comments  a where table_name='CS_GZRY_JXXX'";
                PreparedStatement pst = conn.prepareStatement(sql);
                ResultSet rs = pst.executeQuery();
                ResultSetMetaData data=rs.getMetaData();
                int numberOfColumns = data.getColumnCount();
                //将列名和对应的注释封装到map中
                for(int i=1;i<=numberOfColumns;i++){
                    while(rs.next()){
                        System.out.println(rs.getString(i) + "  "+rs.getObject(i+1));
                        mm.put(rs.getString(i), rs.getString(i+1));
                    }
                }
    View Code

    批处理多条不同的sql:

    public static  void distributeRole(BigDecimal userid,BigDecimal roleid){
            Connection con = null;
            Statement statement = null;
            ResultSet res = null;
            String sql = "";
            String sql2 = "";
            try {
                sql ="insert into db_user(userid) values (" + userid + ")";
                sql2 = "insert into db_user_role values(" + userid + "," + roleid +")";
                con = DriverManager.getConnection("","sa","");
                //Connection con = DB.getInstance().getConnection();
                if(con != null){
                    System.out.println("连接成功");
                }else{
                    System.out.println("连接失败");
                }
                 con.setAutoCommit(false);
                 statement = con.createStatement();
                 statement.addBatch(sql);
                 statement.addBatch(sql2);
                 int[] re =  statement.executeBatch();
    
                 if(re[0] == 1 && re[1] == 1){
                     System.out.println("分配角色成功");
                 }else{
                     System.out.println("分配角色失败");
                 }
                 con.commit(); 
                 con.setAutoCommit(true);
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
                try {
                    con.rollback();
                } catch (SQLException e1) {
                    // TODO Auto-generated catch block
                    e1.printStackTrace();
                }
            }finally{
                try {
                    statement.close();
                    con.close();
                } catch (SQLException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }
    View Code
  • 相关阅读:
    Java实现 蓝桥杯VIP 算法训练 一元三次方程
    Java实现 蓝桥杯VIP 算法训练 乘法表
    Java实现 蓝桥杯VIP 算法训练 矩阵加法
    Java实现 蓝桥杯VIP 算法训练 一元三次方程
    Java实现 蓝桥杯VIP 算法训练 平方计算
    Java实现 蓝桥杯VIP 算法训练 平方计算
    Java实现 蓝桥杯VIP 算法训练 平方计算
    Java实现 蓝桥杯VIP 算法训练 乘法表
    Java实现 蓝桥杯VIP 算法训练 乘法表
    监管只是压倒网盘业务的一根稻草,但不是主要原因(答案只有一个:成本!)
  • 原文地址:https://www.cnblogs.com/o-andy-o/p/3301020.html
Copyright © 2011-2022 走看看