会员可以在此提问,百战程序员老师有问必答
对大家有帮助的问答会被标记为“推荐”
看完课程过来浏览一下别人提的问题,会帮你学得更全面
截止目前,同学们一共提了 132971个问题

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

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技术(旧) 22306楼
Python 全系列/第二阶段:Python 深入与提高/文件处理 22308楼
JAVA 全系列/第二十一阶段:分布式医疗云平台/系统管理前后端开发(旧) 22309楼
JAVA 全系列/第一阶段:JAVA 快速入门/变量、数据类型、运算符 22310楼
JAVA 全系列/第八阶段:Linux入门到实战/Maven 22312楼
Python 全系列/第二阶段:Python 深入与提高/文件处理 22314楼
Python 全系列/第二阶段:Python 深入与提高/模块 22315楼
JAVA 全系列/第三阶段:数据库编程/SQL 语言 22316楼
JAVA 全系列/第一阶段:JAVA 快速入门/变量、数据类型、运算符 22317楼
JAVA 全系列/第一阶段:JAVA 快速入门/JAVA入门和背景知识 22319楼

课程分类

百战程序员微信公众号

百战程序员微信小程序

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