我建了一个属性文件database.properties用来提供jdbc属性和想从publishers.sql 文件中读取SQL命令 并在程序中执行但是却出现错误:
代码如下:import java.io.*;
import java.sql.*;
import java.util.*;public class TestDB2 { public static void main(String[] args) {
try {
Scanner in;
if(args.length==0)
in=new Scanner(System.in);
else
in=new Scanner(new File(args[0]));
Connection conn=getConnection();
try {
Statement stat=conn.createStatement();
while(true) {
if(args.length==0) System.out.println("Enter command or EXIT to exit:");
if(!in.hasNextLine()) return;
String line=in.nextLine();
if(line.equalsIgnoreCase("EXIT")) return;
try {
boolean hasResultSet=stat.execute(line);
if(hasResultSet)
showResultSet(stat);
}
catch(SQLException e) {
while(e!=null) {
e.printStackTrace();
e=e.getNextException();
}
}
}
}
finally {
conn.close();
}
}
catch(SQLException e) {
while(e!=null) {
e.printStackTrace();
e=e.getNextException();
}
}
catch(IOException e) {
e.printStackTrace();
}
}
public static Connection getConnection() throws SQLException ,IOException {
Connection conn=null;
Properties props=new Properties();
FileInputStream in= new FileInputStream("database.properties");
props.load(in);
in.close();
String drivers=props.getProperty("jdbc.drivers");
if(drivers!=null) System.setProperty("jdbc.drivers",drivers);
String url=props.getProperty("jdbc.url");
String username=props.getProperty("jdbc.username");
String password=props.getProperty("jdbc.password"); return DriverManager.getConnection(url,username,password);
}
public static void showResultSet(Statement stat) throws SQLException {
ResultSet result=stat.getResultSet();
ResultSetMetaData metadata=result.getMetaData();
int column=metadata.getColumnCount();
for(int i=1;i<=column;i++) {
if(i>1) System.out.print(",");
System.out.print(metadata.getColumnLabel(i));
}
System.out.println();
while(result.next()) {
for(int i=1;i<=column;i++) {
if(i>1) System.out.print(",");
System.out.print(result.getString(1));
}
System.out.println();
}
result.close();
}
}
------------------------------------------------------------------------------------------------------------------------------------------------------
Error Messages:D:\wallpaper\COREJAVA>java TestDB2 Publishers.sql
java.sql.SQLException: General error message from server: "No tables used"
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:2001)
at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1168)
at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:1279)
at com.mysql.jdbc.MysqlIO.sqlQuery(MysqlIO.java:1225)
at com.mysql.jdbc.Connection.execSQL(Connection.java:2278)
at com.mysql.jdbc.Connection.execSQL(Connection.java:2225)
at com.mysql.jdbc.Statement.execute(Statement.java:906)
at TestDB2.main(TestDB2.java:32)
代码如下:import java.io.*;
import java.sql.*;
import java.util.*;public class TestDB2 { public static void main(String[] args) {
try {
Scanner in;
if(args.length==0)
in=new Scanner(System.in);
else
in=new Scanner(new File(args[0]));
Connection conn=getConnection();
try {
Statement stat=conn.createStatement();
while(true) {
if(args.length==0) System.out.println("Enter command or EXIT to exit:");
if(!in.hasNextLine()) return;
String line=in.nextLine();
if(line.equalsIgnoreCase("EXIT")) return;
try {
boolean hasResultSet=stat.execute(line);
if(hasResultSet)
showResultSet(stat);
}
catch(SQLException e) {
while(e!=null) {
e.printStackTrace();
e=e.getNextException();
}
}
}
}
finally {
conn.close();
}
}
catch(SQLException e) {
while(e!=null) {
e.printStackTrace();
e=e.getNextException();
}
}
catch(IOException e) {
e.printStackTrace();
}
}
public static Connection getConnection() throws SQLException ,IOException {
Connection conn=null;
Properties props=new Properties();
FileInputStream in= new FileInputStream("database.properties");
props.load(in);
in.close();
String drivers=props.getProperty("jdbc.drivers");
if(drivers!=null) System.setProperty("jdbc.drivers",drivers);
String url=props.getProperty("jdbc.url");
String username=props.getProperty("jdbc.username");
String password=props.getProperty("jdbc.password"); return DriverManager.getConnection(url,username,password);
}
public static void showResultSet(Statement stat) throws SQLException {
ResultSet result=stat.getResultSet();
ResultSetMetaData metadata=result.getMetaData();
int column=metadata.getColumnCount();
for(int i=1;i<=column;i++) {
if(i>1) System.out.print(",");
System.out.print(metadata.getColumnLabel(i));
}
System.out.println();
while(result.next()) {
for(int i=1;i<=column;i++) {
if(i>1) System.out.print(",");
System.out.print(result.getString(1));
}
System.out.println();
}
result.close();
}
}
------------------------------------------------------------------------------------------------------------------------------------------------------
Error Messages:D:\wallpaper\COREJAVA>java TestDB2 Publishers.sql
java.sql.SQLException: General error message from server: "No tables used"
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:2001)
at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1168)
at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:1279)
at com.mysql.jdbc.MysqlIO.sqlQuery(MysqlIO.java:1225)
at com.mysql.jdbc.Connection.execSQL(Connection.java:2278)
at com.mysql.jdbc.Connection.execSQL(Connection.java:2225)
at com.mysql.jdbc.Statement.execute(Statement.java:906)
at TestDB2.main(TestDB2.java:32)
解决方案 »
- java图片加载失败?
- 怎样获取光标位置。
- 请教for循环参数问题
- [求助]怎样用URLConnection 类,建立与http服务器的连接后,再检验http服务器返回的状态代码,如200,500,401用户名密码错误等?
- 知我者谓我心忧,不知者谓我何求!
- 哪里有java编程思想第3版(中文)下载呀,发给我一份谢谢了,马上给分
- 用jexcel API怎样往现有的workbook中插入sheet?
- jdbc向数据库里插数据为什么commit了数据库里还是没有数据
- 虚心请教,Java中的接口(interface)的使用?如果我要实现一个接口,是否要把各个接口中的所有方法都重写一遍呢?
- 通过网络传送整个文件夹的问题
- 大哥们 小妹我 想要一本 Thinking in Java Second Editon 作者 候捷 中国台湾的
- 求一个找出网页源代码中所有网址的正则表达式
你的line传的是什么.
如
CREATE TABLE publishers (message,....)
INSERT INTO publishers VALUES(......)
SELECT *
但是却出现如上错误提示 不知道什么意思
望继续关注