-
Notifications
You must be signed in to change notification settings - Fork 49
/
Copy pathFirstJDBC.java
64 lines (55 loc) · 2.42 KB
/
FirstJDBC.java
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
import java.sql.*;
public class FirstJDBC {
public static void main(String[] args) {
// connecting to the database
String serverName = "localhost";
String myDatabase = "JavaJDBC";
final String url = "jdbc:mysql://" + serverName + "/" + myDatabase;
// initializing ResultSet
ResultSet rs = null;
// username and password
final String username = "root";
final String password = "MyNewPass";
try {
// using static method of DriverManager
Connection connection = DriverManager.getConnection(url, username, password);
System.out.println("Success: Database connection. " + connection);
// creating statement object
// TYPE_SCROLL_SENSITIVE - The cursor can scroll forward and backward, and the
// result set
// is sensitive to changes made by others to the database that occur after the
// result set
// was created
// CONCUR_READ_ONLY - Creates a read-only resultset. This is the default.
Statement stmt = connection.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,
ResultSet.CONCUR_READ_ONLY);
// executing SELECT query and returning ResultSet
rs = stmt.executeQuery("SELECT * FROM MyGuests");
// displaying results column wise
System.out.println("id\t" + "firstname\t\t" + "lastname\t\t" + "email\t\t" + "reg_date\t");
while (rs.next()) {
int id = rs.getInt("id");
String firstname = rs.getString("firstname");
String lastname = rs.getString("lastname");
String email = rs.getString("email");
String reg = rs.getString("reg_date");
// updating row if id is 14
if (id == 14) {
rs.updateString("firstname", "ravi");
rs.updateRow();
}
System.out.println(id + "\t" + firstname + "\t\t" + lastname + "\t\t" + email + "\t\t" + reg + "\t");
}
} catch (SQLException e) {
System.out.println("Error: Couldn't connect to database " + e);
} finally {
if (rs != null) {
try {
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
}