hi all,我初学java 在学core java2 II这本书第四章jdbc 系统winxp oracle10g数据库
已把oracle10g安装目录下的classes12.jar文件复制到Java\jre1.6.0_01\lib\ext目录 并设置了classpath环境变量以下是一个jdbc程序:ExecSQL.java
[code]
import java.io.*;
import java.util.*;
import java.sql.*;
/**
Executes all SQL statements in a file.
Call this program as
java -classpath driverPath:. ExecSQL commandFile
*/
class ExecSQL
{
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();
}
} /**
Gets a connection from the properties specified
in the file database.properties
@return the database connection
*/
public static Connection getConnection()
throws SQLException, IOException
{
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);
} /**
Prints a result set.
@param stat the statement whose result set should be
printed
*/
public static void showResultSet(Statement stat)
throws SQLException
{
ResultSet result = stat.getResultSet();
ResultSetMetaData metaData = result.getMetaData();
int columnCount = metaData.getColumnCount(); for (int i = 1; i <= columnCount; i++)
{
if (i > 1) System.out.print(", ");
System.out.print(metaData.getColumnLabel(i));
}
System.out.println(); while (result.next())
{
for (int i = 1; i <= columnCount; i++)
{
if (i > 1) System.out.print(", ");
System.out.print(result.getString(i));
}
System.out.println();
}
result.close();
}
}[/code]这是database.properties
jdbc.drivers=oracle.jdbc.driver.OracleDriver
jdbc.url=jdbc:oracle:thin:@127.0.0.1:1521:orcl
jdbc.username=111111
jdbc.password=111111运行程序,在控制台显示:Enter command or EXIT to exit:然后无论我输入什么,总是出现以下错误信息:java.sql.SQLException: ORA-00900: invalid SQL statement at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:124)
at oracle.jdbc.driver.T4CTTIoer.processError(T4CTTIoer.java:304)
at oracle.jdbc.driver.T4CTTIoer.processError(T4CTTIoer.java:271)
at oracle.jdbc.driver.T4C8Oall.receive(T4C8Oall.java:622)
at oracle.jdbc.driver.T4CStatement.doOall8(T4CStatement.java:111)
at oracle.jdbc.driver.T4CStatement.execute_for_rows(T4CStatement.java:473)
at oracle.jdbc.driver.OracleStatement.doExecuteWithTimeout(OracleStatement.java:1027)
at oracle.jdbc.driver.OracleStatement.execute(OracleStatement.java:1515)Enter command or EXIT to exit: at ExecSQL.main(ExecSQL.java:42)________________________多谢大家另外与此问题无关,征学java的朋友,尤其是初学者一起学习,my msn: [email protected]
已把oracle10g安装目录下的classes12.jar文件复制到Java\jre1.6.0_01\lib\ext目录 并设置了classpath环境变量以下是一个jdbc程序:ExecSQL.java
[code]
import java.io.*;
import java.util.*;
import java.sql.*;
/**
Executes all SQL statements in a file.
Call this program as
java -classpath driverPath:. ExecSQL commandFile
*/
class ExecSQL
{
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();
}
} /**
Gets a connection from the properties specified
in the file database.properties
@return the database connection
*/
public static Connection getConnection()
throws SQLException, IOException
{
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);
} /**
Prints a result set.
@param stat the statement whose result set should be
printed
*/
public static void showResultSet(Statement stat)
throws SQLException
{
ResultSet result = stat.getResultSet();
ResultSetMetaData metaData = result.getMetaData();
int columnCount = metaData.getColumnCount(); for (int i = 1; i <= columnCount; i++)
{
if (i > 1) System.out.print(", ");
System.out.print(metaData.getColumnLabel(i));
}
System.out.println(); while (result.next())
{
for (int i = 1; i <= columnCount; i++)
{
if (i > 1) System.out.print(", ");
System.out.print(result.getString(i));
}
System.out.println();
}
result.close();
}
}[/code]这是database.properties
jdbc.drivers=oracle.jdbc.driver.OracleDriver
jdbc.url=jdbc:oracle:thin:@127.0.0.1:1521:orcl
jdbc.username=111111
jdbc.password=111111运行程序,在控制台显示:Enter command or EXIT to exit:然后无论我输入什么,总是出现以下错误信息:java.sql.SQLException: ORA-00900: invalid SQL statement at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:124)
at oracle.jdbc.driver.T4CTTIoer.processError(T4CTTIoer.java:304)
at oracle.jdbc.driver.T4CTTIoer.processError(T4CTTIoer.java:271)
at oracle.jdbc.driver.T4C8Oall.receive(T4C8Oall.java:622)
at oracle.jdbc.driver.T4CStatement.doOall8(T4CStatement.java:111)
at oracle.jdbc.driver.T4CStatement.execute_for_rows(T4CStatement.java:473)
at oracle.jdbc.driver.OracleStatement.doExecuteWithTimeout(OracleStatement.java:1027)
at oracle.jdbc.driver.OracleStatement.execute(OracleStatement.java:1515)Enter command or EXIT to exit: at ExecSQL.main(ExecSQL.java:42)________________________多谢大家另外与此问题无关,征学java的朋友,尤其是初学者一起学习,my msn: [email protected]
import java.io.*;
import java.util.*;
import java.sql.*;
/**
Executes all SQL statements in a file.
Call this program as
java -classpath driverPath:. ExecSQL commandFile
*/
class ExecSQL
{
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();
}
} /**
Gets a connection from the properties specified
in the file database.properties
@return the database connection
*/
public static Connection getConnection()
throws SQLException, IOException
{
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);
} /**
Prints a result set.
@param stat the statement whose result set should be
printed
*/
public static void showResultSet(Statement stat)
throws SQLException
{
ResultSet result = stat.getResultSet();
ResultSetMetaData metaData = result.getMetaData();
int columnCount = metaData.getColumnCount(); for (int i = 1; i <= columnCount; i++)
{
if (i > 1) System.out.print(", ");
System.out.print(metaData.getColumnLabel(i));
}
System.out.println(); while (result.next())
{
for (int i = 1; i <= columnCount; i++)
{
if (i > 1) System.out.print(", ");
System.out.print(result.getString(i));
}
System.out.println();
}
result.close();
}
}
invalid SQL statement
这个的意思是你的SQL语句是无效的。你先在控制台输出你输入的SQL语句看合法不洛
再测试一下你是否连上了数据库
现在运行的是该章的第二个程序,运行程序无错误并显示:Enter command or EXIT to exit:
之后我输入 java ExecSQL Books.sql 回车后就出现一楼的错误信息 其实现在好像我输入什么都显示这个错误这个invalid SQL statement提示是因为我"Enter command"错误产生的? 还是别的原因? 希望达人解答 thanks in advance