会员可以在此提问,百战程序员老师有问必答
对大家有帮助的问答会被标记为“推荐”
看完课程过来浏览一下别人提的问题,会帮你学得更全面
截止目前,同学们一共提了 132651个问题
JAVA 全系列/第三阶段:数据库编程/Oracle 数据库的使用 916楼
JAVA 全系列/第三阶段:数据库编程/SQL 语言 919楼
JAVA 全系列/第三阶段:数据库编程/SQL 语言 921楼

老师,这是我对逻辑分页代码实现的一个理解,请问您觉得有什么需要纠正的地方吗?

public void selectDeptPage(int current,int pageRows){
    Connection conn=null;
    Statement state=null;
    ResultSet rs=null;

    try {
        conn=JdbcUtil.getConnection();
        String sql="select * from department";
        state=conn.createStatement();
        rs=state.executeQuery(sql);
        int begin=(current-1)*pageRows; //表示取每页的第一条数据所在位置的索引,最小为0
        int end=current*pageRows;  //表示每页的最后一条数据的位置,这个位置是指一般人的位置思维中的位置,即最后一条数据的索引位置+1,

        int currentNum=0;  //当前位置的计数器
        while (rs.next()){
            if(currentNum>=begin&&currentNum<end){  //当计数器大于或等于每页第一条数据的索引以及小于最后一条数据的位置时,打印出表中指定列的数据
                System.out.println(rs.getInt("department_id")+" "+rs.getString("department_name")+" "+rs.getInt(3));
                if(currentNum==end-1){  
                    break;  //当计数器等于每页最后一条数据的索引值时,表示属于当前页current的数据结果集已经获取完毕,没有必要再继续移动指针获取下一行的结果集,直接结束while循环
                }
            }
            currentNum++;  
        }

    } catch (SQLException e) {
        e.printStackTrace();
    }finally {
        JdbcUtil.closeResource(state,conn,rs);
    }
}


JAVA 全系列/第三阶段:数据库编程/JDBC技术(旧) 924楼

老师,我的这个代码可以成功插入数据,可是在执行成功前会显示异常是怎么回事?

public class JdbcUtil {
    private static String driver;
    private static String url;
    private static String username;
    private static String password;
    
    static {
        ResourceBundle bundle=ResourceBundle.getBundle("jdbc");
        driver=bundle.getString("driver");
        url=bundle.getString("url");
        username=bundle.getString("username");
        password=bundle.getString("password");
        
        try {
            Class.forName(driver);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static Connection getConnection(){
        Connection conn=null;
        try {
            conn= DriverManager.getConnection(url,username,password);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return conn;
    }

    private static void closeStatement(Statement state){
        if(state != null){
            try {
                state.close();
            } catch (Exception e) {
// TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }

    public static void closeConnection(Connection conn){
        if(conn != null){
            try {
                conn.close();
            } catch (Exception e) {
// TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }

    public static void closeResource(Statement state,Connection conn){
        closeStatement(state);
        closeConnection(conn);
    }


}
public class JDBCTest {
    public void insertData(String department_name,int location_id){
        Connection conn=null;
        Statement state=null;
        try {
            conn=JdbcUtil.getConnection();
            conn= DriverManager.getConnection("jdbc:mysql://localhost:3306/zf?useUnicode=true&characterEncoding=utf8","root","zfroot");
            String sql="insert into department value(default ,'"+department_name+"',"+location_id+")";
            state=conn.createStatement();
            int flag=state.executeUpdate(sql);
            System.out.println(flag);
            } catch (Exception e) {
            e.printStackTrace();
        }finally {
            JdbcUtil.closeResource(state,conn);
        }
    }

    public void updateData(String department_name,int location_id,int department_id){
        Connection conn=null;
        Statement state=null;
        try {
            conn=JdbcUtil.getConnection();
            String sql="update department set department_name='"+department_name+"',location_id="+location_id+" where department_id="+department_id+";";
            state=conn.createStatement();
            int flag=state.executeUpdate(sql);
            System.out.println(flag);
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            JdbcUtil.closeResource(state,conn);

        }
    }

    public static void main(String[] args) {
        JDBCTest jdbcTest=new JDBCTest();
//        jdbcTest.updateData("策划部",6,3);
        jdbcTest.insertData("经理部",7);
    }
}

image.png

image.png

JAVA 全系列/第三阶段:数据库编程/JDBC技术(旧) 925楼
JAVA 全系列/第三阶段:数据库编程/SQL 语言 926楼
JAVA 全系列/第三阶段:数据库编程/Oracle 数据库的使用 929楼

课程分类

百战程序员微信公众号

百战程序员微信小程序

©2014-2025百战汇智(北京)科技有限公司 All Rights Reserved 北京亦庄经济开发区科创十四街 赛蒂国际工业园
网站维护:百战汇智(北京)科技有限公司
京公网安备 11011402011233号    京ICP备18060230号-3    营业执照    经营许可证:京B2-20212637