会员可以在此提问,百战程序员老师有问必答
对大家有帮助的问答会被标记为“推荐”
看完课程过来浏览一下别人提的问题,会帮你学得更全面
截止目前,同学们一共提了 133322个问题
JAVA 全系列/第四阶段:数据库与AI协同技术实战/SQL 语言 1036楼
JAVA 全系列/第四阶段:数据库与AI协同技术实战/Oracle 数据库的使用 1037楼
JAVA 全系列/第四阶段:数据库与AI协同技术实战/Oracle 数据库的使用 1038楼
JAVA 全系列/第四阶段:数据库与AI协同技术实战/SQL 语言 1039楼
JAVA 全系列/第四阶段:数据库与AI协同技术实战/JDBC技术(旧) 1040楼
JAVA 全系列/第四阶段:数据库与AI协同技术实战/JDBC技术(旧) 1041楼

public class JDBCtest06 {

        public void run(String login_name,String login_pwd) {
            Connection conn = null;
            Statement stat = null;

            try {
                //注册驱动
                Class.forName("com.mysql.jdbc.Driver");
                //创建链接;
                conn = DriverManager.getConnection(
                        "jdbc:mysql://localhost:3306/text2?useUnicode=true&characterEncoding=utf-8", "root", "123456");
                String sql = "insert into t_user(login_name,login_pwd) values('"+login_name+"','"+login_pwd+"');";
                //获取数据库操作对象,发送执行sql语句的对象
                stat = conn.createStatement();
                int re = stat.executeUpdate(sql);
                System.out.println(re);
                System.out.println(conn);
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                if (conn != null) {
                    try {
                        conn.close();
                    } catch (SQLException e) {
                        e.printStackTrace();
                    }
                }
                if (stat != null) {
                    try {
                        stat.close();
                    } catch (SQLException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
    public static void main(String[] args) {
        JDBCtest06 text=new JDBCtest06();
            text.run(dd,ee);//这个dd,ee为什么会报错?
    }
}


JAVA 全系列/第四阶段:数据库与AI协同技术实战/JDBC技术(旧) 1042楼
JAVA 全系列/第四阶段:数据库与AI协同技术实战/Oracle 数据库的使用 1044楼
JAVA 全系列/第四阶段:数据库与AI协同技术实战/Oracle 数据库的使用 1045楼
JAVA 全系列/第四阶段:数据库与AI协同技术实战/JDBC技术(旧) 1046楼
JAVA 全系列/第四阶段:数据库与AI协同技术实战/JDBC技术(旧) 1047楼
JAVA 全系列/第四阶段:数据库与AI协同技术实战/MySQL数据库 1048楼

package JDBC;

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

public class JdbcUtil {

    private static String driver;

    private static String jdbcUrl;

    private static String user;

    private static String password;


    static {
        //读取Properties文件
        ResourceBundle bundle = ResourceBundle.getBundle("src/JDBC/jdbc");
        driver = bundle.getString("driver");
        jdbcUrl = bundle.getString("jdbcUrl");
        user = bundle.getString("user");
        password = bundle.getString("password");

        try {
            //链接数据库
            Class.forName(driver);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }

    /**
     * 获取Connection对象
     * @return
     */
    public static Connection getConnection(){
        Connection connection = null;
        try {
            connection = DriverManager.getConnection(jdbcUrl,user,password);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return connection;
    }

    /**
     * 关闭Statement
     * @param statement
     */
    public static void closeStatement(Statement statement){
        if(statement!=null){
            try {
                statement.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }

    /**
     *   关闭Connection
     * @param connection
     */
    public static void closeConnection(Connection connection){
        if(connection!=null){
            try {
                connection.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * 关闭所有资源
     * @param statement
     * @param connection
     */
    public static void closeAll(Statement statement,Connection connection){
        if(statement!=null){
            try {
                statement.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        if(connection!=null){
            try {
                connection.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }

    }


}


package JDBC;

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

import static JDBC.JdbcUtil.closeAll;
import static JDBC.JdbcUtil.getConnection;

public class jdbcTest02 {
    public static void main(String[] args) {
        jdbcTest02 jdbcT = new jdbcTest02();
        jdbcT.insertDepartments("教学部",19,19);
    }

    public void insertDepartments(String department_name,int department_id,int location_id){
        Connection connection = null;
        Statement statement = null;
        try {
            connection = JdbcUtil.getConnection();
            statement = connection.createStatement();
            String sql = "insert into departments value("+department_id+",'"+department_name+"',"+location_id+")";
            System.out.println(sql);
            statement.executeUpdate(sql);
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            closeAll(statement, connection);
        }
    }

}

老师能帮忙看一下哪里的问题嘛

JAVA 全系列/第四阶段:数据库与AI协同技术实战/JDBC技术(旧) 1050楼

课程分类

百战程序员微信公众号

百战程序员微信小程序

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