FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
telegramPullRequestBot/src/main/java/DataBase.java at main · Dimus99/telegramPullRequestBot · GitHub
Dimus99
/
telegramPullRequestBot
Public
Notifications
You must be signed in to change notification settings
Fork
2
Star
0
Code
Issues
0
Pull requests
1
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
telegramPullRequestBot
/
src
/
main
/
java
/
DataBase.java
Copy path
More file actions
More file actions
Latest commit
History
History
History
74 lines (63 loc) · 2.63 KB
Breadcrumbs
telegramPullRequestBot
/
src
/
main
/
java
/
DataBase.java
Copy path
File metadata and controls
74 lines (63 loc) · 2.63 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
import
java
.
sql
.*;
import
java
.
util
.
Objects
;
public
class
DataBase
{
static
Connection
conn
;
String
databaseName
;
//Конструктор с подключением к бд
//В переменной path должно находится расположние файла с бд (имя файла и самой таблицы должны сопадать)
public
DataBase
(
String
path
) {
databaseName
=
path
.
split
(
"
\\
W+"
)[
path
.
split
(
"
\\
W+"
).
length
-
2
];
try
{
Class
.
forName
(
"org.sqlite.JDBC"
);
path
=
"jdbc:sqlite:"
.
concat
(
path
);
conn
=
DriverManager
.
getConnection
(
path
);
System
.
out
.
println
(
"successfully connected to '"
+
databaseName
+
"'"
);
}
catch
(
Exception
e
) {
System
.
out
.
println
(
e
.
getMessage
());
}
}
// Добавляем запись в бд
// Этот же метод используется для изменения уже существующих данных
public
void
addData
(
String
id
,
String
token
)
throws
SQLException
{
// Создаем ввод команды SQLite
String
query
=
"INSERT INTO "
+
databaseName
+
" (%s, %s) "
.
formatted
(
"id"
,
"token"
) +
"VALUES ('%s', '%s');"
.
formatted
(
id
,
token
);
// Пробуем создать новую запись
try
{
Statement
statement
=
conn
.
createStatement
();
statement
.
executeUpdate
(
query
);
}
catch
(
Exception
e
)
{
System
.
out
.
println
(
e
.
getMessage
());
}
// Если запись с таки id уже существует, изменяем ее
finally
{
query
=
"UPDATE "
+
databaseName
+
" "
+
"SET token =
\"
%s
\"
"
.
formatted
(
token
) +
"WHERE id =
\"
%s
\"
;"
.
formatted
(
id
);
Statement
statement
=
conn
.
createStatement
();
statement
.
executeUpdate
(
query
);
}
System
.
out
.
println
(
"data added"
);
}
public
String
getTokenById
(
String
id
)
throws
SQLException
{
ResultSet
resSet
;
String
res
=
null
;
String
query
=
"SELECT token FROM "
+
databaseName
+
" WHERE id =
\"
"
+
id
+
"
\"
;"
;
try
{
Statement
statement
=
conn
.
createStatement
();
resSet
=
statement
.
executeQuery
(
query
);
}
catch
(
Exception
e
) {
System
.
out
.
println
(
e
.
getMessage
());
return
""
;
}
while
(
Objects
.
requireNonNull
(
resSet
).
next
()) {
res
=
resSet
.
getString
(
"token"
);
}
return
res
;
}
}
Back
|
FazBrowse Home
|
New Git URL