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

package com.bjsxt;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;

public class JdbcTest {
    Connection conn = null;
    Statement state = null;

    //向Departments表中添加一条数据
    public void insertDepartments(String department_name,int location_id){
        try {
            //注册驱动
            Class.forName("com.mysql.jdbc.Driver");
            //创建连接
            conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/bjsxt?useUnicode=true&characterEncoding=UTF-8","root","root");
            //执行SQL
            //如果数据以参数形式传入,需进行字符串拼接
            String sql = "insert into departments values(default,'"+department_name+"','"+location_id+"')";
            //String sql = "insert into departments values(default,'研发部',8)";
            //
            state = conn.createStatement();
             int flag = state.executeUpdate(sql);
            System.out.println(flag);
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            if (state != null){
                try {
                    state.close();
                } catch (SQLException throwables) {
                    throwables.printStackTrace();
                }
            }
            if(conn != null){
                try {
                    conn.close();
                } catch (SQLException throwables) {
                    throwables.printStackTrace();
                }
            }
        }
    }
    //更新departments表中的department_id为4的数据,将部门名称改为教学部,location_id修改为6
    public void updateDepartments(String department_name,int location_id,int department_id){
        Connection conn = null;
        Statement state = null;
        try {
            //注册驱动
            Class.forName("com.mysql.jdbc.Driver");
            conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/bjsxt?useUnicode=true&characterEncoding=UTF-8","root","root");
            state = conn.createStatement();
            String sql = "update departments d set d.department_name = '"+department_name+"',d.location_id ="+location_id+"where d.department_id = "+department_id;
            int flag = state.executeUpdate(sql);
            System.out.println(flag);
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            if (state != null){
                try {
                    state.close();
                } catch (SQLException throwables) {
                    throwables.printStackTrace();
                }
            }
            if (conn != null) {
                try {
                    conn.close();
                } catch (SQLException throwables) {
                    throwables.printStackTrace();
                }
            }
        }
    }

    public static void main (String[] args){
        JdbcTest test = new JdbcTest();
        //test.insertDepartments("研发部",8);
        test.updateDepartments("教学部",6,11);
    }
}

image.png

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

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

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技术(旧) 248楼
JAVA 全系列/第三阶段:数据库编程/SQL 语言 249楼
JAVA 全系列/第三阶段:数据库编程/SQL 语言 250楼
JAVA 全系列/第三阶段:数据库编程/SQL 语言 252楼
JAVA 全系列/第三阶段:数据库编程/Oracle 数据库的使用 254楼

屏幕截图 2023-08-19 172332.png

JAVA 全系列/第三阶段:数据库编程/JDBC技术 255楼

课程分类

百战程序员微信公众号

百战程序员微信小程序

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