-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathDatabase.java
More file actions
67 lines (64 loc) · 2.09 KB
/
Copy pathDatabase.java
File metadata and controls
67 lines (64 loc) · 2.09 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
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package chattingapp;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
*
* @author riddhi
*/
public class Database {
private Connection connection;
public Database()
{
try {
String url = "jdbc:mysql://localhost/chatting?user=root&password=Liverolin7";
connection = DriverManager.getConnection(url);
} catch (SQLException ex) {
Logger.getLogger(Database.class.getName()).log(Level.SEVERE, null, ex);
}
}
public ArrayList<Client> getClients()
{
try {
ArrayList<Client> clients = new ArrayList<>();
Statement stmt = connection.createStatement();
ResultSet rs = stmt.executeQuery("select * from login");
while(rs.next())
{
String username = rs.getString(1);
String password = rs.getString(2);
Client client = new Client(username,password);
clients.add(client);
}
return clients;
} catch (SQLException ex) {
Logger.getLogger(Database.class.getName()).log(Level.SEVERE, null, ex);
}
return null;
}
public Client registerClient(String username,String password)
{
Client client = null;
try {
PreparedStatement pstmt = connection.prepareStatement("insert into login values(?,?)");
pstmt.setString(1, username);
pstmt.setString(2,password);
pstmt.executeUpdate();
client = new Client(username,password);
} catch (SQLException ex) {
Logger.getLogger(Database.class.getName()).log(Level.SEVERE, null, ex);
}
return client;
}
}