# java.sql的範例
**Question:**
```java=
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class Test {
private static final String dbURL = "127.0.0.1:1433/camiol";
private static final String userName = "camiol";
private static final String password = "1qaz@WSX";
public static void main(String[] args) throws SQLException {
Connection conn = DriverManager.getConnection(dbURL,userName,password);
String query = "select id from Employee";
try (Statement stmt = conn.createStatement()){
ResultSet rs = stmt.executeQuery(query);
stmt.executeQuery("select id from Customer");
while(rs.next()) {
System.out.println("Employee ID : "+rs.getInt("id"));
}
} catch (Exception e) {
System.out.println("Error");
}
}
}
```
### What is the result of compiling and executing this code fragment?
A. The program prints employee IDs.
B. The program prints customer IDs.
**C. The program prints Error.**
D. compilation fails on line 13.
- [x] **Answer:c**
程式第19行,又用了一次Statement物件的executeQuery方法,會使得先前的ResultSet自動被關閉,因此程式第20行會拋出SQLException。
---
**Question:**
```java=
public class Test {
private static final String userName = "camiol";
private static final String passWord = "1qaz@WSX";
private static final String dbURL = "127.0.0.1/camiol";
public static void main(String[] args) {
try {
Properties prop = new Properties();
prop.put("user", userName);
prop.put("password",passWord);
Connection conn = DriverManager.getConnection(dbURL,prop);
if(conn!=null) {
System.out.println("Connection Established!");
}
} catch (SQLException e) {
System.out.println(e);
}
}
}
```
### What is the result?
A. A ClassNotFoundException is thrown at runtime.
B. The program prints nothing.
**C. The program prints Connection Established.**
D. A SQLException is thrown at runtime.
- [x] **Answer: C**
###### tags: `ocpjp`