-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathJDBCDemo.java
More file actions
60 lines (56 loc) · 1.67 KB
/
Copy pathJDBCDemo.java
File metadata and controls
60 lines (56 loc) · 1.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
import java.io.IOException;
import java.io.FileNotFoundException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class JDBCDemo{
private static final String URL_JAVADB = "jdbc:derby:employee;create=true";
private static final String URL_SQLITE = "jdbc:sqlite:employee";
public static void main(String[] args){
if(args.length !=1){
System.err.println("usage 1: java JDBCDemo javadb");
System.err.println("usage 2: java JDBCDemo sqlite");
return;
}
String url = null;
switch(args[0]){
case "javadb":
url = URL_JAVADB;
break;
case "sqlite":
url = URL_SQLITE;
break;
default:
System.err.println("invalid command-line argument");
return;
}
Connection con = null;
try{
if("sqlite".equals(args[0])){
Class.forName("org.sqlite.JDBC"); // load the Driver class file explicitly, regarding non-JAVADB
}
con = DriverManager.getConnection(url);
// ... perform useful work
// simulates a JDBC method throwing SQLException
throw new SQLException("Unable to access database table", new IOException("File I/O problem")); // specify an IOException as cause.
}catch(ClassNotFoundException cnfe){
cnfe.printStackTrace();
}catch(SQLException sqle){
while(sqle!=null){
System.err.println("SQL error : " + sqle.getMessage());
System.err.println("SQL state : " + sqle.getSQLState());
System.err.println("Error code : " + sqle.getErrorCode());
System.err.println("Cause : " + sqle.getCause());
sqle = sqle.getNextException();
}
}finally{
if(con != null){
try{
con.close();
}catch(SQLException sqle){
sqle.printStackTrace();
}
}
}
}
}