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

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 全系列/第三阶段:数据库编程/JDBC技术(旧) 19816楼
Python 全系列/第一阶段:Python入门/序列 19820楼
Python 全系列/第一阶段:Python入门/控制语句 19821楼
WEB前端全系列/第五阶段:前后端交互/服务器与数据库交互 19822楼

jdbcUtil

  1. package com.bjsxt;
    
    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.SQLException;
    import java.sql.Statement;
    import java.util.ResourceBundle;
    
    /**
     * JDBC工具类
     */
    public class jdbcUtil {
        private static String driver ;
        private static String jdbcUrl ;
        private static String username ;
        private  static String userpassword ;
        static {
            //读取properties文件
            ResourceBundle bundle = ResourceBundle.getBundle("jdbc");
            driver = bundle.getString("driver");
            jdbcUrl=bundle.getString("jdbcUrl");
            username=bundle.getString("username");
            userpassword=bundle.getString("userpassword");
            //驱动注册
            try {
                Class.forName(driver);
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            }
        }
    
            //获取Connection对象
        public static Connection getConnection(){
            Connection conn = null;
            //创建连接
            try {
                conn = DriverManager.getConnection(jdbcUrl,username,userpassword);
            } catch (SQLException e) {
                e.printStackTrace();
            }
            return conn;
        }
    
        //关闭statement
        public static void closeStatement(Statement state){
            if (state != null){
                try {
                    state.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }
        //关闭connection
        public static void closeConnection(Connection conn){
            if(conn != null){
                try {
                    conn.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }
    }
  2. jdbcTset


  3. package com.bjsxt;
    
    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.SQLException;
    import java.sql.Statement;
    
    public class jdbcTest {
        //向department添加一条数据
        public void insertDepartments(String department_name, int location_id){
            Connection conn = null;
            Statement state = null;
            try {
                //驱动注册
                //创建连接
                conn = jdbcUtil.getConnection();
                String sql = "insert into departments values(default,'"+department_name+"',"+location_id+")";
               state = conn.createStatement();
                int flag = state.executeUpdate(sql);
                System.out.println(flag);
            } catch (Exception e) {
                e.printStackTrace();
            }finally {
                jdbcUtil.closeStatement(state);
                jdbcUtil.closeConnection(conn);
    
            }
        }
    
        //更新数据
        public void updateDepartments (String department_name,int location_id,int department_id){
            Connection conn = null;
            Statement state = null;
            try{
                conn = jdbcUtil.getConnection();
                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 {
                jdbcUtil.closeStatement(state);
                jdbcUtil.closeConnection(conn);
            }
        }
    
        public static void main(String[] args) {
            jdbcTest test = new jdbcTest();
            //test.insertDepartments("研发部",8);
           test.updateDepartments("研发部4",6,3);
            //test.insertDepartments("教学部",9);
    
        }
    }

properties中

driver=com.mysql.jdbc.Driver
jdbcUrl=jdbc:mysql://localhost:3306/bjsxt?useUnicode=true&characterEncoding=utf-8
username=root
userpassword=niehan

看不出来是什么原因 为什么提示未连接呢?

image.png

JAVA 全系列/第三阶段:数据库编程/JDBC技术(旧) 19823楼
Python 全系列/第二阶段:Python 深入与提高/GUI编程(隐藏) 19824楼
JAVA 全系列/第四阶段:网页编程和设计/jQuery 19825楼
JAVA 全系列/第十一阶段:智能家居项目(旧)/至尊智能家居第二天 19826楼
JAVA 全系列/第三阶段:数据库编程/MySQL数据库的使用 19828楼
Python 全系列/第二阶段:Python 深入与提高/文件处理 19830楼

课程分类

百战程序员微信公众号

百战程序员微信小程序

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