注册 登录
编程论坛 JAVA论坛

明明代码与视频上一样,但是运行时总是报错,百思不得其解,求大神帮忙看一下指出到底是哪里不对

KQY 发布于 2019-08-16 14:30, 1849 次点击
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();
    }
}

1 回复
#2
林月儿2019-08-16 21:45
报什么错
1