Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
J
jvstd1
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Patryk Czarnik
jvstd1
Commits
f72a71a6
Commit
f72a71a6
authored
Oct 30, 2024
by
Patryk Czarnik
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Pierwsze przykłady JDBC
parent
01de5718
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
68 additions
and
0 deletions
+68
-0
pom.xml
pom.xml
+11
-0
A_Polaczenie.java
src/main/java/p21_jdbc/bazowe/A_Polaczenie.java
+27
-0
B_PierwszySelect.java
src/main/java/p21_jdbc/bazowe/B_PierwszySelect.java
+30
-0
No files found.
pom.xml
View file @
f72a71a6
...
...
@@ -14,4 +14,14 @@
<project.build.sourceEncoding>
UTF-8
</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>
org.postgresql
</groupId>
<artifactId>
postgresql
</artifactId>
<version>
42.7.4
</version>
<scope>
runtime
</scope>
</dependency>
</dependencies>
</project>
\ No newline at end of file
src/main/java/p21_jdbc/bazowe/A_Polaczenie.java
0 → 100644
View file @
f72a71a6
package
p21_jdbc
.
bazowe
;
import
java.sql.Connection
;
import
java.sql.DriverManager
;
import
java.sql.SQLException
;
public
class
A_Polaczenie
{
public
static
void
main
(
String
[]
args
)
{
String
url
=
"jdbc:postgresql://vps-2bc225bd.vps.ovh.net/hr"
;
try
{
// W starszych wersjach Javy trzeba było wymusić załadowanie klasy sterownika do JVM
// Od ~ Java 7 już tego robić nie trzeba
// Class.forName("org.postgresql.Driver");
Connection
c
=
DriverManager
.
getConnection
(
url
,
"alx"
,
"abc123vps"
);
System
.
out
.
println
(
"Nawiązano połączenie: "
+
c
);
c
.
close
();
}
catch
(
SQLException
e
)
{
throw
new
RuntimeException
(
e
);
}
catch
(
Exception
e
)
{
throw
new
RuntimeException
(
e
);
}
}
}
src/main/java/p21_jdbc/bazowe/B_PierwszySelect.java
0 → 100644
View file @
f72a71a6
package
p21_jdbc
.
bazowe
;
import
java.sql.*
;
public
class
B_PierwszySelect
{
public
static
void
main
(
String
[]
args
)
{
String
url
=
"jdbc:postgresql://vps-2bc225bd.vps.ovh.net/hr"
;
try
(
Connection
c
=
DriverManager
.
getConnection
(
url
,
"alx"
,
"abc123vps"
))
{
System
.
out
.
println
(
"Połączenie: "
+
c
);
try
(
PreparedStatement
stmt
=
c
.
prepareStatement
(
"SELECT * FROM employees"
))
{
try
(
ResultSet
rs
=
stmt
.
executeQuery
())
{
System
.
out
.
println
(
"ResultSet: "
+
rs
);
while
(
rs
.
next
())
{
System
.
out
.
printf
(
"Pracownik nr %d: %s %s (%s) zarabia %s%n"
,
rs
.
getInt
(
1
),
// albo rs.getInt("employee_id")
rs
.
getString
(
"first_name"
),
rs
.
getString
(
"last_name"
),
rs
.
getString
(
"job_id"
),
rs
.
getBigDecimal
(
"salary"
)
);
}
}
}
}
catch
(
SQLException
e
)
{
throw
new
RuntimeException
(
e
);
}
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment