-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathuser.java
130 lines (110 loc) · 4.04 KB
/
user.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
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
package bus_booking;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Scanner;
public class user {
private Connection connect = null;
private Statement statement = null;
private PreparedStatement preparedStatement = null;
private ResultSet resultSet = null;
Connection c = null;
Statement stmt = null;
String userid;
String pass;
user(String userid, String pass)
{
this.userid=userid;
this.pass=pass;
}
public
void modifyDetails(String new_pass)
{
try
{
// This will load the MySQL driver, each DB has its own driver
Class.forName("org.postgresql.Driver");
// Setup the connection with the DB
connect = DriverManager.getConnection("jdbc:postgresql://localhost:5432/bus_ticket_booking",
"postgres", "postgres");
// Statements allow to issue SQL queries to the database
statement = connect.createStatement();
// PreparedStatements can use variables and are more efficient
preparedStatement = connect
.prepareStatement("update user_table set password = ? where username=?");
// Parameters start with 1
String pass1=pass;
preparedStatement.setString(1, pass1);
preparedStatement.setString(2, userid);
preparedStatement.executeUpdate();
preparedStatement = connect
.prepareStatement("SELECT * from user_table");
resultSet = preparedStatement.executeQuery();
System.out.print("User password changed");
}
catch (Exception e)
{
e.printStackTrace();
System.err.println(e.getClass().getName()+": "+e.getMessage());
System.exit(0);
}
}
void addDetails()
{
try
{
Class.forName("org.postgresql.Driver");
// Setup the connection with the DB
connect = DriverManager.getConnection("jdbc:postgresql://localhost:5432/bus_ticket_booking",
"postgres", "postgres");
// Statements allow to issue SQL queries to the database
statement = connect.createStatement();
// PreparedStatements can use variables and are more efficient
preparedStatement = connect
.prepareStatement("insert into user_table values (?, ?)");
// Parameters start with 1
String id=userid;
preparedStatement.setString(1, id);
String pass1=pass;
preparedStatement.setString(2, pass1);
preparedStatement.executeUpdate();
preparedStatement = connect
.prepareStatement("SELECT * from user_table");
resultSet = preparedStatement.executeQuery();
System.out.print("New user added");
}
catch (Exception e)
{
e.printStackTrace();
System.err.println(e.getClass().getName()+": "+e.getMessage());
System.exit(0);
}
}
private void writeMetaData(ResultSet resultSet) throws SQLException {
// Now get some metadata from the database
// Result set get the result of the SQL query
System.out.println("The columns in the table are: ");
System.out.println("Table: " + resultSet.getMetaData().getTableName(1));
for (int i = 1; i<= resultSet.getMetaData().getColumnCount(); i++){
System.out.println("Column " +i + " "+ resultSet.getMetaData().getColumnName(i));
}
}
// You need to close the resultSet
private void close() {
try {
if (resultSet != null) {
resultSet.close();
}
if (statement != null) {
statement.close();
}
if (connect != null) {
connect.close();
}
} catch (Exception e) {
}
}
}