会员可以在此提问,百战程序员老师有问必答
对大家有帮助的问答会被标记为“推荐”
看完课程过来浏览一下别人提的问题,会帮你学得更全面
截止目前,同学们一共提了 132382个问题
JAVA 全系列/第四阶段:网页编程和设计/JavaScript语言 7681楼
Python 全系列/第二阶段:Python 深入与提高/GUI编程(隐藏) 7682楼
JAVA 全系列/第十阶段:百战旅游网项目/百战旅游网 7685楼

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技术 7686楼
Python 全系列/第一阶段:Python入门/Python入门(动画版) 7688楼
JAVA 全系列/第六阶段:项目管理与SSM框架/Mybatis 7690楼
JAVA 全系列/第五阶段:JavaWeb开发/Servlet技术详解 7691楼
JAVA 全系列/第六阶段:项目管理与SSM框架/Mybatis 7692楼
JAVA 全系列/第五阶段:JavaWeb开发/Servlet技术详解 7695楼

课程分类

百战程序员微信公众号

百战程序员微信小程序

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