Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/368.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
java中参数化SELECT语句的问题_Java_Mysql - Fatal编程技术网

java中参数化SELECT语句的问题

java中参数化SELECT语句的问题,java,mysql,Java,Mysql,我遇到了一个参数化语句的问题,该语句返回错误“您的SQL语法中有一个错误在”?“。。。我看不出问题出在哪里。欢迎任何帮助。Thx提前 import java.sql.*; import java.util.LinkedList; import java.util.ListIterator; public class ReadDb { static final String JDBC_DRIVER = "com.mysql.jdbc.Driver"; static final S

我遇到了一个参数化语句的问题,该语句返回错误“您的SQL语法中有一个错误在”?“。。。我看不出问题出在哪里。欢迎任何帮助。Thx提前

import java.sql.*;
import java.util.LinkedList;
import java.util.ListIterator;

public class ReadDb {
    static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
    static final String DB_URL = "jdbc:mysql://localhost:3306/HarmonicEngine";
    static final String USER = "*********";
    static final String PASS = "*******";
    private static LinkedList<PitchStats> chord = new LinkedList<PitchStats>();
    private static int chnum;

    ReadDb(int chordnum) {
        chnum = chordnum;
    }

    public static void main(String[] args) {
        // *TEST*
        chnum = 1;
        LinkedList<PitchStats> trial = ReadExecute();
        ListIterator<PitchStats> listIterator = trial.listIterator();
        while (listIterator.hasNext()) {
            System.out.println(listIterator.next());
        }

    }

    public static LinkedList<PitchStats> ReadExecute() {
        Connection conn = null;
        PreparedStatement stmt = null;
        try {
            Class.forName("com.mysql.jdbc.Driver");
            System.out.println("Connecting to database...");
            conn = DriverManager.getConnection(DB_URL, USER, PASS);

            System.out.println("Creating statement....");

            String sql = "SELECT pitch_name, pitch_reg, move_num, block_num, prog_num, "
                    + "sustain FROM sample1 WHERE chord_num = ?";
            stmt = conn.prepareStatement(sql);
            stmt.setInt(1, chnum);
            ResultSet rs = stmt.executeQuery(sql);
            while (rs.next()) {
                String pitchname = rs.getString("pitch_name");
                int pitchreg = rs.getInt("pitch_reg");
                int movenum = rs.getInt("move_num");
                int blocknum = rs.getInt("block_num");
                int prognum = rs.getInt("prog_num");
                boolean sustained = rs.getBoolean("sustain");

                PitchStats thisPitch = new PitchStats(pitchname, pitchreg,
                        movenum, blocknum, prognum, chnum, sustained);

                chord.add(thisPitch);
            }
            rs.close();
            stmt.close();
            conn.close();
        } catch (SQLException se) {
            se.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (stmt != null)
                    stmt.close();
            } catch (SQLException se2) {
            }
            try {
                if (conn != null)
                    conn.close();
            } catch (SQLException se) {
                se.printStackTrace();
            }
            System.out.println("Goodbye!");
        }
        return chord;
    }

}
替换

ResultSet rs = stmt.executeQuery(sql);

第一个是继承的语句ExecuteQueryString,它按原样执行查询,没有替换的参数

另外,只要在finally块中关闭JDBC对象,就不需要在try块中关闭它们

ResultSet rs = stmt.executeQuery();