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

程序运行成功,但是数据库没有添加到数据

1:代码

package sxt.com;

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

public class JdbcTest {

    //向departments表添加一条数据
    public void insertDepartments(String department_name,int location_id){
        Connection conn = null;
        Statement state = null;

        //驱动注册
        try {
            Class.forName("com.mysql.jdbc.Driver");
            //创建链接
             conn =DriverManager.getConnection("jdbc:mysql://localhost:3306/sxt?useUnicode=true&characterEncoding=utf-8&useSSL=false","root","2020");
            //执行sql
            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 {
            if (state != null){
                try {
                    state.close();
                } catch (SQLException throwables) {
                    throwables.printStackTrace();
                }finally {

                }
            }
        }
        if (conn != null){
            try {
                conn.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }

    }

    public static void main(String[] args) {
        JdbcTest test = new JdbcTest();
        test.insertDepartments("研发部",8);
    }
}
2:运行

image.png


3:数据库:

image.png

JAVA 全系列/第三阶段:数据库编程/JDBC技术(旧) 95楼
JAVA 全系列/第三阶段:数据库编程/SQL 语言 97楼
JAVA 全系列/第三阶段:数据库编程/SQL 语言 98楼
JAVA 全系列/第三阶段:数据库编程/SQL 语言 100楼
JAVA 全系列/第三阶段:数据库编程/SQL 语言 102楼

public class StatmentTest {
    /**
     * 添加用户
     */
    private  Connection connection = null;
    private Statement statement = null;
    public void insertUsers(String username,int userage){
        try{
            create();
            //定义需要执行的SQL语句
            String sql = "insert into users values(default,'"+username+"',"+userage+")";
            //执行SQL,返回boolean类型值,如果SQL有结果集返回,那么返回值为true,如果没有返回值则返回false
            boolean execute = statement.execute(sql);
            System.out.println(execute);
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            close();
        }
    }
    /**
     * 修改用户信息
     */
    public void updateUsers(int userid,String username,int userage){
        try{
            create();
            //定义SQL语句
            String sql = "update users set username = '"+username+"',userage="+userage+" where userid="+userid;
            //执行SQL语句
            int i = statement.executeUpdate(sql);
            System.out.println(i);
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            close();
        }
    }
    private void close(){
        JdbcUtils.closeResource(statement,connection);
    }
    private void create(){
        //获取Connection对象
        connection = JdbcUtils.getConnection();
        //获取Statement对象
        try {
            statement = connection.createStatement();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }
}
//可以这么写吗?运行成了,就是不知道有没有别的问题。


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

课程分类

百战程序员微信公众号

百战程序员微信小程序

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