-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.java
More file actions
85 lines (57 loc) · 2.5 KB
/
Main.java
File metadata and controls
85 lines (57 loc) · 2.5 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
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
package com.company;
import java.sql.*; // import the classes in the java.sql package
class DatabaseSample{
public static void main (String args[]){ // to execute
System.out.println("Program Initiated");
// connecting to database
Connection con = null;
Statement stmt = null;
try {
System.out.println("Loading the driver");
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); // loading the MS Access
//driver
System.out.println("Connecting");
con = DriverManager.getConnection("jdbc:odbc:odbc_ex");
// odbc_ex is the data source name (DSN)
System.out.println("Connected");
// the transaction to be carried out - SQL statement
String queryString = "insert into Student (id, first_name, last_name) values ('007','James','Bond')";
//System.out.println(queryString);
stmt = con.createStatement(); // creating the statement object to work database
int i = stmt.executeUpdate(queryString); // returns an integer - number of
// records added
if (i != 0){
System.out.println("Data update succeded");
}
else {
System.out.println("Data update error");
}
// retrieve the data in the Names table
queryString = "select * from Names";
ResultSet rs = stmt.executeQuery(queryString); // query result is in rs object
while (rs.next()){
String id = rs.getString("id"); //
String fname = rs.getString("first_name");
String lname = rs.getString("last_name");
System.out.println("ID = "+id+ " First Name = "+fname+" Last Name = "+lname);
}
System.out.println("Thank you for using the database");
} catch (SQLException e) {
System.out.println("Cannot find the database");
e.printStackTrace();
} catch (ClassNotFoundException e) {
System.out.println("Cannot find the database");
} finally {
try{
if(stmt != null) {
stmt.close();
stmt = null;
}
if(con != null) {
con.close();
con = null;
}
}catch (SQLException e) { }
}
}
}