标题:明明代码与视频上一样,但是运行时总是报错,百思不得其解,求大神帮忙看一 ...
只看楼主
KQY
Rank: 1
等 级:新手上路
帖 子:5
专家分:0
注 册:2019-7-9
结帖率:66.67%
已结贴  问题点数:8 回复次数:1 
明明代码与视频上一样,但是运行时总是报错,百思不得其解,求大神帮忙看一下指出到底是哪里不对
public class test {

    static {
        try {
            Class.forName("com.mysql.jdbc.Driver");
        } catch (ClassNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    // 创建连接
    public Connection getCon() throws SQLException {
        String url = "jdbc:mysql://localhost:3306/test";
        String user = "root";
        String password = "52975xwy";
        Connection con = DriverManager.getConnection(url, user, password);
        return con;
    }

    // 增加记录
    public void insertclassifiction() throws SQLException {
        // 创建连接
        Connection con = this.getCon();
        // 创建会话
        Statement st = con.createStatement();
        // insert
        String insert_sql = "insert into classification(name,age,height,sex,relationship)  values(\"wang\",21,173,\"m\",\"self\")";
        st.execute(insert_sql);
        // 关闭st
        st.close();
        // con
        con.close();

    }

    // 根据number删除一条记录
    public void deleteStuByNumber() throws SQLException {
        // 创建连接
        Connection con = this.getCon();
        // 创建会话
        Statement st = con.createStatement();
        // delete
        String insert_sql = "delete from classification where number='5'";
        st.execute(insert_sql);
        // 关闭st
        st.close();
        // con
        con.close();

    }

    // 修改
    public void Updateclassification() throws Exception {
        // 创建连接
        Connection con = this.getCon();
        // 创建会话
        Statement st = con.createStatement();
        // delete
        String insert_sql = "update classification set name='Ms.GONG' where number=21";
        st.execute(insert_sql);
        // 关闭st
        st.close();
        // con
        con.close();

    }

    public static void main(String[] args) throws Exception {
        test t = new test();
        t.insertclassifiction();
        t.deleteStuByNumber();
        t.Updateclassification();
    }
}

搜索更多相关主题的帖子: public 创建 con String close 
2019-08-16 14:30
林月儿
Rank: 16Rank: 16Rank: 16Rank: 16
来 自:湖南
等 级:版主
威 望:138
帖 子:2276
专家分:10647
注 册:2015-3-19
得分:4 
报什么错

剑栈风樯各苦辛,别时冰雪到时春
2019-08-16 21:45



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-496430-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.013507 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved