-
Notifications
You must be signed in to change notification settings - Fork 227
Expand file tree
/
Copy pathDbUtil.java
More file actions
83 lines (61 loc) · 1.95 KB
/
DbUtil.java
File metadata and controls
83 lines (61 loc) · 1.95 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
package com.example;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class DbUtil {
static Connection con=null;
static String driver = "com.mysql.cj.jdbc.Driver";
static String url = "jdbc:mysql://lwplabs.cdo5uynfnrno.us-east-1.rds.amazonaws.com:3306/homeloan";
static String username = "admin";
static String password = "admin123";
static {
try {
/*
Properties prop = new Properties();
prop.load(DbUtil.class.getResourceAsStream("application.properties"));
String driver = prop.getProperty("database.driver");
String url = prop.getProperty("database.url");
String username = prop.getProperty("database.username");
String password = prop.getProperty("database.password");
*/
Class.forName(driver);
con = DriverManager.getConnection(url, username, password);
System.out.println("database connected...");
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch(SQLException e) {
e.printStackTrace();
}
}
public static int register(String name, String password, String address, int age, String profession) {
int rows = 0;
try {
PreparedStatement pst = con.prepareStatement("INSERT INTO user VALUES(?,?,?,?,?)");
pst.setString(1,name);
pst.setString(2, password);
pst.setString(3, address);
pst.setInt(4, age);
pst.setString(5, profession);
rows = pst.executeUpdate();
}catch(Exception e) {
e.printStackTrace();
}
return rows;
}
public static int login(String name, String password) {
int rows = 0;
try {
PreparedStatement pst = con.prepareStatement("SELECT * FROM user WHERE name = ? AND password = ?");
pst.setString(1,name);
pst.setString(2, password);
ResultSet rs = pst.executeQuery();
if(rs.next())
rows = 1;
}catch(Exception e) {
e.printStackTrace();
}
return rows;
}
}