java.sql.SQLException: Can not issue empty query.

1、错误描述

java.sql.SQLException: Can not issue empty query.
	at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:996)
	at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:935)
	at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:924)
	at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:870)
	at com.mysql.jdbc.StatementImpl.checkNullOrEmptyQuery(StatementImpl.java:492)
	at com.mysql.jdbc.StatementImpl.executeQuery(StatementImpl.java:1367)
	at com.you.sql.Student.queryStudent(Student.java:51)
	at com.you.sql.Student.main(Student.java:79)

2、错误原因

/**
 *
 * @Project:MySQL
 * @Title:Student.java
 * @Package:com.you.sql
 * @Description:
 * @Author:YouHaiDong
 * @Date:2015年6月10日 下午11:49:36
 * @Version:
 */
package com.you.sql;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

/**
 * <p>请用一句话概括功能</p>
 * @ClassName:Student
 * @Description:
 * @Author:YouHaiDong
 * @Date:2015年6月10日 下午11:49:36
 *
 */
public class Student
{
	/**
	 * 查询学生基本信息
	 * @Title:Student
	 * @Description:
	 * @Date:2015年6月11日 上午12:06:40
	 * @return :void
	 * @throws
	 */
	public static void queryStudent()
	{
		StringBuffer sql = new StringBuffer();
		String url = "jdbc:mysql://localhost:3333/student";
		String user = "root";
		String password = "root";
		Connection conn = null;
		Statement stat = null;
		ResultSet rs = null;
		try
		{
			conn = DriverManager.getConnection(url, user, password);
			stat = conn.createStatement();
			rs = stat.executeQuery(sql.toString());
			while(rs.next())
			{
				String stuId = rs.getString("stu_id");
				String stuName = rs.getString("stu_name");
				String stuSex = rs.getString("sex");
				String stuAge = rs.getString("stu_age");
				String stuPhone = rs.getString("stu_phone");
				System.out.println("学号:"+stuId+"----"+"姓名:"+stuName+"----"+"性别:"+stuSex+"---"+"年龄:"+stuAge+"----"+"电话:"+stuPhone);
			}
		}
		catch (SQLException e)
		{
			e.printStackTrace();
		}
	}

	/**
	 *
	 * @Title:Student
	 * @Description:
	 * @param args
	 * @Date:2015年6月11日 上午12:07:53
	 * @return :void
	 * @throws
	 */
	public static void main(String args[])
	{
		queryStudent();
	}
}

3、解决办法

由于在执行rs = stat.executeQuery(sql.toString());时,sql为空,导致报错

StringBuffer sql = new StringBuffer();

    sql.append("select * from t_stu_info ");

上一篇:打开eclipse "Initializing Java Tooling"错误


下一篇:Chage