【发布时间】:2017-04-28 21:57:12
【问题描述】:
我正在学习 JDBC 并尝试使用 IDE Eclipse 在 Java 上执行 sql 查询。正在加载 jdbc 驱动程序并建立连接,但是查询没有运行。
import java.sql.*;
public class JDBCDemo {
public static void main(String[] args) {
String url="jdbc:oracle:thin:@//localhost:1521/xe";
String un="system";
String pwd="system";
Connection con=null;
Statement stmt=null;
ResultSet res=null;
try{
DriverManager.registerDriver(new oracle.jdbc.OracleDriver());
System.out.println("Driver Loaded successfully");
}
catch(Exception e)
{
System.out.println("Driver not loaded");
}
try{
DriverManager.getConnection(url,un,pwd);
System.out.println("Connection established");
}
catch(Exception f)
{
System.out.println("Connection not established");
}
try{
String s="Select * from student";
stmt=con.createStatement();
res=stmt.executeQuery(s);
System.out.println("Query executed succesfully");
}
catch(Exception e)
{
System.out.println("Query not executed");
}
输出是: 驱动加载成功
连接建立
查询未执行
【问题讨论】:
-
为了人类的爱,请打印堆栈跟踪或记录您的异常:
e.printStackTrace(System.err);