• 常用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
  • 相关阅读:
    bit、byte、word的区别
    JavaScript
    品牌的意义
    Java开发
    Java:关于implements Serializable的警告问题
    文件路径:/和的区别
    Iterator
    parameter和argument的区别
    Java中private、protected、public和default的区别
    Java异常
  • 原文地址:https://www.cnblogs.com/o-andy-o/p/3301020.html
Copyright © 2020-2023  润新知