使用Eclipse中的java连接到我的oracle 12c数据库
有人能告诉我我的代码有什么问题吗?我正在尝试连接到我的oracle 12c数据库,一旦确认可以开始操作数据 这是我的代码:使用Eclipse中的java连接到我的oracle 12c数据库,java,eclipse,oracle,Java,Eclipse,Oracle,有人能告诉我我的代码有什么问题吗?我正在尝试连接到我的oracle 12c数据库,一旦确认可以开始操作数据 这是我的代码: package Testing2; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Connection; import java.sql.DriverManager; public class Table
package Testing2;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Connection;
import java.sql.DriverManager;
public class Table6 {
public static void main(String[] args)throws SQLException {
// TODO Auto-generated method stub
try{
Class.forName("oracle.jdbc.Driver.OracleDriver");
}
catch(java.lang.ClassNotFoundException e)
{
System.err.println("ClassNotFoundException:");
System.err.println(e.getMessage());
}
Connection con = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:Orcl","hr","Victor");
PreparedStatement statement = con.prepareStatement("select * from COUNTRIES WHERE COUNTRY_ID = 'AR'");
ResultSet result = statement.executeQuery();
while(result.next()){
System.out.println("Current Date from Oracle : " + result.getString("current_day"));
}
System.out.println("done");
System.out.println("done!");
}
}
这是我的堆栈跟踪:
Exception in thread "main" java.sql.SQLException: Invalid column name
at oracle.jdbc.driver.OracleStatement.getColumnIndex(OracleStatement.java:3965)
at oracle.jdbc.driver.InsensitiveScrollableResultSet.findColumn(InsensitiveScrollableResultSet.java:299)
at oracle.jdbc.driver.GeneratedResultSet.getString(GeneratedResultSet.java:1460)
at Testing2.Table6.main(Table6.java:32)
暗示就在你的问题中。 线程“main”java.sql.SQLException中的异常:无效列名
选择*from COUNTRY_ID='AR'-尝试从oracle客户端运行它,看看您得到了什么提示在您的问题中。 线程“main”java.sql.SQLException中的异常:无效列名
选择*from COUNTRY_ID='AR'-尝试从oracle客户端运行它,查看您得到的结果,同时确保正确编写查询语句。我还必须确保jdbc.jar位于java构建路径库中。 这是我的密码:
package Testing2;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Connection;
import java.sql.DriverManager;
public class Table6 {
public static void main(String[] args)throws SQLException {
// TODO Auto-generated method stub
try{
Class.forName("oracle.jdbc.OracleDriver");
}
catch(java.lang.ClassNotFoundException e)
{
System.err.println("ClassNotFoundException:");
System.err.println(e.getMessage());
}
Connection con = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:Orcl","hr","Victor");
PreparedStatement statement = con.prepareStatement("select COUNTRY_ID FROM COUNTRIES");
ResultSet result = statement.executeQuery();
while(result.next()){
System.out.println("All Country Id's: " + result.getString(1));
}
System.out.println("done");
System.out.println("done!");
}
}
以下是我的输出:
All Country Id's: AR
All Country Id's: AU
All Country Id's: BE
All Country Id's: BR
All Country Id's: CA
All Country Id's: CH
All Country Id's: CN
All Country Id's: DE
All Country Id's: DK
All Country Id's: EG
All Country Id's: FR
All Country Id's: IL
All Country Id's: IN
All Country Id's: IT
All Country Id's: JP
All Country Id's: KW
All Country Id's: ML
All Country Id's: MX
All Country Id's: NG
All Country Id's: NL
All Country Id's: SG
All Country Id's: UK
All Country Id's: US
All Country Id's: ZM
All Country Id's: ZW
done
done!
以及确保正确编写查询语句。我还必须确保jdbc.jar位于java构建路径库中。 这是我的密码:
package Testing2;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Connection;
import java.sql.DriverManager;
public class Table6 {
public static void main(String[] args)throws SQLException {
// TODO Auto-generated method stub
try{
Class.forName("oracle.jdbc.OracleDriver");
}
catch(java.lang.ClassNotFoundException e)
{
System.err.println("ClassNotFoundException:");
System.err.println(e.getMessage());
}
Connection con = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:Orcl","hr","Victor");
PreparedStatement statement = con.prepareStatement("select COUNTRY_ID FROM COUNTRIES");
ResultSet result = statement.executeQuery();
while(result.next()){
System.out.println("All Country Id's: " + result.getString(1));
}
System.out.println("done");
System.out.println("done!");
}
}
以下是我的输出:
All Country Id's: AR
All Country Id's: AU
All Country Id's: BE
All Country Id's: BR
All Country Id's: CA
All Country Id's: CH
All Country Id's: CN
All Country Id's: DE
All Country Id's: DK
All Country Id's: EG
All Country Id's: FR
All Country Id's: IL
All Country Id's: IN
All Country Id's: IT
All Country Id's: JP
All Country Id's: KW
All Country Id's: ML
All Country Id's: MX
All Country Id's: NG
All Country Id's: NL
All Country Id's: SG
All Country Id's: UK
All Country Id's: US
All Country Id's: ZM
All Country Id's: ZW
done
done!
非常感谢JSR。问题解决了。我试着同时做很多事情。非常感谢JSR。问题解决了。我试图同时做很多事情。