为什么会报错??

为什么会报错??

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
package com.imooc.jdbc.utils;
 
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
 
/**
 * @author lenovo
 
 *JDBC的工具类
 *
 */
 
public class JDBCUtils {
    private static final String driverClass;
    private static final String url;
    private static final String user;
    private static final String password;
     
     
    static {
        driverClass="com.mysql.cj.jdbc.Driver";
        url="jdbc:mysql://localhost:3306/imooc"
                "?useSSL=false&serverTimezone=Hongkong&useUnicode=true&characterEncoding=utf-8";
        user="root";
        password="117919";
    }
     
        /**
         * 注册驱动的方法
         * @throws ClassNotFoundException 
         */
    public static void LoadDriver() throws ClassNotFoundException {
        Class.forName(driverClass);
    
    /**
     * 获得连接的方法
     * @throws SQLException 
     * @throws ClassNotFoundException 
     
     */
    public static Connection getConnection() throws SQLException, ClassNotFoundException {
        LoadDriver();
        Connection conn= DriverManager.getConnection(url, user, password);
        return conn;
    }
    /**
     * 释放资源的方法
     
     */
    public static void release(Statement stmt,Connection conn) {
        if(stmt!=null) {
            try {
                stmt.close();
            catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            stmt=null;
        }
        if(conn!=null) {
            try {
                conn.close();
            catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            conn=null;
        }
    }
    public static void release(Statement stmt,Connection conn,ResultSet rs) {
        if(stmt!=null) {
            try {
                stmt.close();
            catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            stmt=null;
        }
        if(conn!=null) {
            try {
                conn.close();
            catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            conn=null;
        }
        if(rs!=null) {
            try {
                rs.close();
            catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            rs=null;
        }
    }
}



package com.imooc.jdbc.utils;


import java.sql.Connection;

import java.sql.ResultSet;

import java.sql.SQLException;

import java.sql.Statement;


import org.junit.Test;


public class JDBCTest {

//保存记录

@Test

public void test() {

Connection conn=null;

Statement stmt=null;

ResultSet rs=null;

try {

//注册驱动

//获得连接

conn=JDBCUtils.getConnection();

// 创建SQL语句对象

String sql = "insert goods values(null,'耳机',800,'蓝牙耳机')";

//创建申明

stmt=conn.createStatement();

//执行SQL语句对象

int i=stmt.executeUpdate(sql);

if(i>0) {

System.out.println("插入成功!");

}else {

System.out.println("插入失败!");

}

//创建结果集

            rs = stmt.executeQuery(sql);

            //遍历结果集

            while (rs.next()) {

                int id = rs.getInt("id");

                String name = rs.getString("name");

                String price = rs.getString("price");

                String desp = rs.getString("desp");

                System.out.println(id + "   " + name + "   "+price  + "   "+ desp);

            }

} catch (ClassNotFoundException e) {

// TODO Auto-generated catch block

e.printStackTrace();

} catch (SQLException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}finally {

JDBCUtils.release(stmt, conn, rs);

}

}

}


在我查询的时候会报错  别的代码可以运行,加查询结果集那段 会报错  也查不出结果。。。为什么?

正在回答 回答被采纳积分+1

登陆购买课程后可参与讨论,去登陆

1回答
chrismorgen 2019-01-17 16:35:52

请问同学的报错提示是什么呢?通常情况下,我们需要阅读报错提示来修改自己的代码,建议你将报错提示粘贴到我要回答中,方便老师具有针对性的为你解答,祝学习愉快~

  • 提问者 有点麋鹿 #1
    插入成功! java.sql.SQLException: Can not issue data manipulation statements with executeQuery(). at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:129) at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:97) at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:89) at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:63) at com.mysql.cj.jdbc.StatementImpl.checkForDml(StatementImpl.java:385) at com.mysql.cj.jdbc.StatementImpl.executeQuery(StatementImpl.java:1153) at com.imooc.jdbc.utils.JDBCTest.test(JDBCTest.java:35) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:498) at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50) at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12) at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47) at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
    2019-01-17 16:38:13
  • 提问者 有点麋鹿 #2
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47) at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17) at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325) at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78) at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57) at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290) at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71) at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288) at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58) at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268) at org.junit.runners.ParentRunner.run(ParentRunner.java:363) at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:89) at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:41) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:541) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:763) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:463) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:209)
    2019-01-17 16:38:35
  • 提问者 有点麋鹿 #3
    在老师的电脑上代码是没问题的吗?
    2019-01-17 16:39:32
问题已解决,确定采纳
还有疑问,暂不采纳

恭喜解决一个难题,获得1积分~

来为老师/同学的回答评分吧

0 星
Java数据库开发与实战应用2018版
  • 参与学习           人
  • 提交作业       277    份
  • 解答问题       4297    个

Java数据库开发的必备技能,从流行的MySQL数据库开始,到Java原生的数据库管理接口JDBC的使用,再到常用的数据持久化框架MyBatis,让你向Java工程师的目标又迈进了一步!

了解课程
请稍等 ...
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号

在线咨询

领取优惠

免费试听

领取大纲

扫描二维码,添加
你的专属老师
插入代码