blob: d703135da126bdcfebd1b068712df192d7ac7d19 (
plain)
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
|
/**
*
*/
/**
* @author sven
*
*/
import java.io.*;
import java.util.*;
import java.sql.*;
public class DB2P6 {
static private Connection con;
static private Statement stmt;
static private String query;
static private PreparedStatement pStmt;
//static private CallableStatement cStmt;
static private ResultSet rs;
static private BufferedReader bf;
/**
* @param args
*/
public static void main(String[] args)
throws SQLException, IOException {
// TODO Auto-generated method stub
bf = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Username: ");
String username = bf.readLine();
System.out.println("Password: ");
String pw = bf.readLine();
// 1. Treiber-Klasse f�r JdbcOdbcDriver laden und registrieren
String driverClass =
"sun.jdbc.odbc.JdbcOdbcDriver";
try {
Class.forName(driverClass);
}
catch (ClassNotFoundException exc) {
System.out.println("c1: "+exc.getMessage());
System.exit(1);
}
// Wieviele Elemente sind derzeit bei der Klasse DriverManager
// registriert?
Enumeration e = DriverManager.getDrivers();
while (e.hasMoreElements())
System.out.println("Beim DriverManager registriert: " +
e.nextElement() + "\n");
// 2. Connection aufbauen
try {
String url = "jdbc:odbc:db2p6";
con = DriverManager.getConnection(url,username,pw);
System.out.println("Jetzt ist die Connection da");
// Transaktionsmanagement: AutoCommit off
con.setAutoCommit(false);
System.out.println("Isolation Level: ");
System.out.println("1: Read Committed");
System.out.println("2: Serializable");
String il = bf.readLine();
// Isolationlevel setzen
if (il.equals("1"))
con.setTransactionIsolation(Connection.TRANSACTION_READ_COMMITTED);
else if (il.equals("2"))
con.setTransactionIsolation(Connection.TRANSACTION_SERIALIZABLE);
else
con.setTransactionIsolation(Connection.TRANSACTION_SERIALIZABLE);
//System.out.println(con.getTransactionIsolation());
con.commit();
}
catch (SQLException exc) {
System.out.println("c2: "+exc.getMessage());
}
//String vAcode;
String vAname;
try {
query = "SELECT aname FROM AUTOR";
stmt = con.createStatement();
rs = stmt.executeQuery(query);
while (rs.next()) {
//vAcode=rs.getString(1);
vAname=rs.getString(1);
System.out.println(vAname);
}
rs.close();
}
catch (SQLException exc) {
System.out.println("c2: "+exc.getMessage());
}
try {
pStmt = con.prepareStatement("insert into autor values (?,?)");
System.out.println("Autor code: ");
String vNewAcode = bf.readLine();
System.out.println("Autor Name: ");
String vNewAname = bf.readLine();
pStmt.setString(1,vNewAcode);
pStmt.setString(2,vNewAname);
pStmt.executeUpdate();
con.commit();
}
catch (SQLException exc) {
con.rollback();
System.out.println("c2: "+exc.getMessage());
}
System.out.println("Autorenliste nochmals anzeigen? [J/N]");
String auswahl = bf.readLine();
if (auswahl.toUpperCase().equals("J"))
{
try {
query = "SELECT aname FROM AUTOR";
stmt = con.createStatement();
rs = stmt.executeQuery(query);
while (rs.next()) {
//vAcode=rs.getString(1);
vAname=rs.getString(1);
System.out.println(vAname);
}
rs.close();
}
catch (SQLException exc) {
System.out.println("c2: "+exc.getMessage());
}
}
}
}
|