feat(postgres_java): add classes
This commit is contained in:
parent
6ee110e8ef
commit
72c4b2bb5d
|
@ -0,0 +1,65 @@
|
|||
package de.swingbe.postgres_java;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.sql.DriverManager;
|
||||
import java.sql.SQLException;
|
||||
import java.sql.Statement;
|
||||
import java.util.logging.Level;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
public class JavaPostgreSqlBatchUpdates {
|
||||
|
||||
public static void main(String[] args) {
|
||||
|
||||
//connection URL for the postgres database
|
||||
//jdbc:postgresql://<host>:<port>/<database name>
|
||||
String url = "jdbc:postgresql://localhost:5432/testdb";
|
||||
|
||||
String user = "usr";
|
||||
String password = "#password";
|
||||
|
||||
try (Connection con = DriverManager.getConnection(url, user, password)) {
|
||||
|
||||
try (Statement st = con.createStatement()) {
|
||||
|
||||
//autocommit should always be turned off when doing batch updates
|
||||
con.setAutoCommit(false);
|
||||
|
||||
//create a new table called friends and insert five rows into it
|
||||
st.addBatch("DROP TABLE IF EXISTS friends");
|
||||
st.addBatch("CREATE TABLE friends(id serial, name VARCHAR(10))");
|
||||
st.addBatch("INSERT INTO friends(name) VALUES ('Jane')");
|
||||
st.addBatch("INSERT INTO friends(name) VALUES ('Tom')");
|
||||
st.addBatch("INSERT INTO friends(name) VALUES ('Rebecca')");
|
||||
st.addBatch("INSERT INTO friends(name) VALUES ('Jim')");
|
||||
st.addBatch("INSERT INTO friends(name) VALUES ('Robert')");
|
||||
|
||||
//method returns an array of committed changes
|
||||
int[] counts = st.executeBatch();
|
||||
|
||||
con.commit();
|
||||
|
||||
System.out.println("Committed " + counts.length + " updates");
|
||||
|
||||
} catch (SQLException ex) {
|
||||
|
||||
if (con != null) {
|
||||
try {
|
||||
con.rollback();
|
||||
} catch (SQLException ex1) {
|
||||
Logger lgr = Logger.getLogger(JavaPostgreSqlBatchUpdates.class.getName());
|
||||
lgr.log(Level.WARNING, ex1.getMessage(), ex1);
|
||||
}
|
||||
}
|
||||
|
||||
Logger lgr = Logger.getLogger(JavaPostgreSqlBatchUpdates.class.getName());
|
||||
lgr.log(Level.SEVERE, ex.getMessage(), ex);
|
||||
}
|
||||
|
||||
} catch (SQLException ex) {
|
||||
|
||||
Logger lgr = Logger.getLogger(JavaPostgreSqlBatchUpdates.class.getName());
|
||||
lgr.log(Level.SEVERE, ex.getMessage(), ex);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,50 @@
|
|||
package de.swingbe.postgres_java;
|
||||
|
||||
import java.sql.*;
|
||||
import java.util.Formatter;
|
||||
import java.util.logging.Level;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
public class JavaPostgreSqlColumnHeaders {
|
||||
|
||||
public static void main(String[] args) {
|
||||
|
||||
//connection URL for the postgres database
|
||||
//jdbc:postgresql://<host>:<port>/<database name>
|
||||
String url = "jdbc:postgresql://localhost:5432/testdb";
|
||||
|
||||
String user = "usr";
|
||||
String password = "#password";
|
||||
|
||||
//sql statement that joins authors with their books
|
||||
String query = "SELECT name, title From authors, books WHERE authors.id=books.author_id";
|
||||
|
||||
try (Connection con = DriverManager.getConnection(url, user, password); PreparedStatement pst = con.prepareStatement(query); ResultSet rs = pst.executeQuery()) {
|
||||
|
||||
//to get the column names you need this object
|
||||
ResultSetMetaData meta = rs.getMetaData();
|
||||
|
||||
String colname1 = meta.getColumnName(1);
|
||||
String colname2 = meta.getColumnName(2);
|
||||
|
||||
//print column names
|
||||
Formatter fmt1 = new Formatter();
|
||||
fmt1.format("%-21s%s", colname1, colname2);
|
||||
System.out.println(fmt1);
|
||||
|
||||
//print data
|
||||
while (rs.next()) {
|
||||
|
||||
Formatter fmt2 = new Formatter();
|
||||
fmt2.format("%-21s", rs.getString(1));
|
||||
System.out.print(fmt2);
|
||||
System.out.println(rs.getString(2));
|
||||
}
|
||||
|
||||
} catch (SQLException ex) {
|
||||
|
||||
Logger lgr = Logger.getLogger(JavaPostgreSqlColumnHeaders.class.getName());
|
||||
lgr.log(Level.SEVERE, ex.getMessage(), ex);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,44 @@
|
|||
package de.swingbe.postgres_java;
|
||||
|
||||
import org.postgresql.copy.CopyManager;
|
||||
import org.postgresql.core.BaseConnection;
|
||||
|
||||
import java.io.FileInputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStreamReader;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.sql.Connection;
|
||||
import java.sql.DriverManager;
|
||||
import java.sql.SQLException;
|
||||
import java.util.logging.Level;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
public class JavaPostgreSqlCopyFromTextFile {
|
||||
|
||||
public static void main(String[] args) {
|
||||
|
||||
//connection URL for the postgres database
|
||||
//jdbc:postgresql://<host>:<port>/<database name>
|
||||
String url = "jdbc:postgresql://localhost:5432/testdb";
|
||||
|
||||
String user = "usr";
|
||||
String password = "#password";
|
||||
|
||||
try (Connection con = DriverManager.getConnection(url, user, password)) {
|
||||
|
||||
CopyManager cm = new CopyManager((BaseConnection) con);
|
||||
|
||||
String fileName = "src/main/resources/friends.txt";
|
||||
|
||||
try (FileInputStream fis = new FileInputStream(fileName); InputStreamReader isr = new InputStreamReader(fis, StandardCharsets.UTF_8)) {
|
||||
|
||||
cm.copyIn("COPY friends FROM STDIN WITH DELIMITER ','", isr);
|
||||
}
|
||||
|
||||
} catch (SQLException | IOException ex) {
|
||||
Logger lgr = Logger.getLogger(JavaPostgreSqlCopyFromTextFile.class.getName());
|
||||
lgr.log(Level.SEVERE, ex.getMessage(), ex);
|
||||
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,47 @@
|
|||
package de.swingbe.postgres_java;
|
||||
|
||||
import org.postgresql.copy.CopyManager;
|
||||
import org.postgresql.core.BaseConnection;
|
||||
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.OutputStreamWriter;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.sql.Connection;
|
||||
import java.sql.DriverManager;
|
||||
import java.sql.SQLException;
|
||||
import java.util.logging.Level;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
public class JavaPostgreSqlCopyToTextFile {
|
||||
|
||||
public static void main(String[] args) {
|
||||
|
||||
//connection URL for the postgres database
|
||||
//jdbc:postgresql://<host>:<port>/<database name>
|
||||
String url = "jdbc:postgresql://localhost:5432/testdb";
|
||||
|
||||
String user = "usr";
|
||||
String password = "#password";
|
||||
|
||||
try {
|
||||
|
||||
Connection con = DriverManager.getConnection(url, user, password);
|
||||
CopyManager cm = new CopyManager((BaseConnection) con);
|
||||
|
||||
String fileName = "src/main/resources/friends.txt";
|
||||
|
||||
try (FileOutputStream fos = new FileOutputStream(fileName); OutputStreamWriter osw = new OutputStreamWriter(fos, StandardCharsets.UTF_8)) {
|
||||
|
||||
cm.copyOut("COPY friends TO STDOUT WITH DELIMITER AS ','", osw);
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
} catch (SQLException ex) {
|
||||
|
||||
Logger lgr = Logger.getLogger(JavaPostgreSqlCopyToTextFile.class.getName());
|
||||
lgr.log(Level.SEVERE, ex.getMessage(), ex);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,33 @@
|
|||
package de.swingbe.postgres_java;
|
||||
|
||||
import java.sql.*;
|
||||
import java.util.logging.Level;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
public class JavaPostgreSqlListTables {
|
||||
|
||||
public static void main(String[] args) {
|
||||
|
||||
//connection URL for the postgres database
|
||||
//jdbc:postgresql://<host>:<port>/<database name>
|
||||
String url = "jdbc:postgresql://localhost:5432/testdb";
|
||||
|
||||
String user = "usr";
|
||||
String password = "#password";
|
||||
|
||||
String query = "SELECT table_name FROM information_schema.tables " + "WHERE table_schema = 'public'";
|
||||
|
||||
try (Connection con = DriverManager.getConnection(url, user, password); PreparedStatement pst = con.prepareStatement(query); ResultSet rs = pst.executeQuery()) {
|
||||
|
||||
while (rs.next()) {
|
||||
|
||||
System.out.println(rs.getString(1));
|
||||
}
|
||||
|
||||
} catch (SQLException ex) {
|
||||
|
||||
Logger lgr = Logger.getLogger(JavaPostgreSqlListTables.class.getName());
|
||||
lgr.log(Level.SEVERE, ex.getMessage(), ex);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,47 @@
|
|||
package de.swingbe.postgres_java;
|
||||
|
||||
import java.sql.*;
|
||||
import java.util.logging.Level;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
public class JavaPostgreSqlMultipleStatements {
|
||||
|
||||
public static void main(String[] args) {
|
||||
|
||||
//connection URL for the postgres database
|
||||
//jdbc:postgresql://<host>:<port>/<database name>
|
||||
String url = "jdbc:postgresql://localhost:5432/testdb";
|
||||
|
||||
String user = "usr";
|
||||
String password = "#password";
|
||||
|
||||
String query = "SELECT id, name FROM authors WHERE Id=1;" + "SELECT id, name FROM authors WHERE Id=2;" + "SELECT id, name FROM authors WHERE Id=3";
|
||||
|
||||
try (Connection con = DriverManager.getConnection(url, user, password); PreparedStatement pst = con.prepareStatement(query)) {
|
||||
|
||||
boolean isResult = pst.execute();
|
||||
|
||||
if (isResult) {
|
||||
System.out.println("The method returns a boolean value indicating if the first result is a ResultSet object");
|
||||
}
|
||||
do {
|
||||
try (ResultSet rs = pst.getResultSet()) {
|
||||
|
||||
while (rs.next()) {
|
||||
|
||||
System.out.print(rs.getInt(1));
|
||||
System.out.print(": ");
|
||||
System.out.println(rs.getString(2));
|
||||
}
|
||||
|
||||
isResult = pst.getMoreResults();
|
||||
}
|
||||
} while (isResult);
|
||||
|
||||
} catch (SQLException ex) {
|
||||
|
||||
Logger lgr = Logger.getLogger(JavaPostgreSqlMultipleStatements.class.getName());
|
||||
lgr.log(Level.SEVERE, ex.getMessage(), ex);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -11,18 +11,20 @@ public class JavaPostgreSqlPrepared {
|
|||
|
||||
public static void main(String[] args) {
|
||||
|
||||
//connection URL for the postgres database
|
||||
//jdbc:postgresql://<host>:<port>/<database name>
|
||||
String url = "jdbc:postgresql://localhost:5432/testdb";
|
||||
|
||||
String user = "usr";
|
||||
String password = "#password";
|
||||
|
||||
//add new author to authors table
|
||||
//add new author to author's table
|
||||
int id = 6;
|
||||
String author = "Trygve Gulbranssen";
|
||||
String query = "INSERT INTO authors(id, name) VALUES(?, ?)";
|
||||
|
||||
//create prepared statement using placeholders instead of directly writing values
|
||||
try (Connection con = DriverManager.getConnection(url, user, password);
|
||||
PreparedStatement pst = con.prepareStatement(query)) {
|
||||
try (Connection con = DriverManager.getConnection(url, user, password); PreparedStatement pst = con.prepareStatement(query)) {
|
||||
|
||||
//bound integer to placeholder
|
||||
pst.setInt(1, id);
|
||||
|
|
|
@ -8,7 +8,10 @@ public class JavaPostgreSqlRetrieve {
|
|||
|
||||
public static void main(String[] args) {
|
||||
|
||||
//connection URL for the postgres database
|
||||
//jdbc:postgresql://<host>:<port>/<database name>
|
||||
String url = "jdbc:postgresql://localhost:5432/testdb";
|
||||
|
||||
String user = "usr";
|
||||
String password = "#password";
|
||||
|
||||
|
@ -16,9 +19,7 @@ public class JavaPostgreSqlRetrieve {
|
|||
String query = "SELECT * FROM authors";
|
||||
|
||||
//create prepared statement using placeholders instead of directly writing values
|
||||
try (Connection con = DriverManager.getConnection(url, user, password);
|
||||
PreparedStatement pst = con.prepareStatement(query);
|
||||
ResultSet rs = pst.executeQuery()) {
|
||||
try (Connection con = DriverManager.getConnection(url, user, password); PreparedStatement pst = con.prepareStatement(query); ResultSet rs = pst.executeQuery()) {
|
||||
|
||||
//advance cursor to the next record
|
||||
//return false if there are no more records in the result set
|
||||
|
|
|
@ -0,0 +1,55 @@
|
|||
package de.swingbe.postgres_java;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.sql.DriverManager;
|
||||
import java.sql.SQLException;
|
||||
import java.sql.Statement;
|
||||
import java.util.logging.Level;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
public class JavaPostgreSqlTransactionEx {
|
||||
|
||||
public static void main(String[] args) {
|
||||
|
||||
//connection URL for the postgres database
|
||||
//jdbc:postgresql://<host>:<port>/<database name>
|
||||
String url = "jdbc:postgresql://localhost:5432/testdb";
|
||||
|
||||
String user = "usr";
|
||||
String password = "#password";
|
||||
|
||||
try (Connection con = DriverManager.getConnection(url, user, password)) {
|
||||
|
||||
try (Statement st = con.createStatement()) {
|
||||
|
||||
//to work with transactions, you must set autocommit to false
|
||||
con.setAutoCommit(false);
|
||||
|
||||
st.executeUpdate("UPDATE authors SET name = 'Leo Tolstoy' " + "WHERE Id = 1");
|
||||
st.executeUpdate("UPDATE books SET title = 'War and Peace' " + "WHERE Id = 1");
|
||||
st.executeUpdate("UPDATE books SET titl = 'Anna Karenina' " + "WHERE Id = 2");
|
||||
|
||||
//ff autocommit is turned off, you must explicitly call the commit method
|
||||
con.commit();
|
||||
|
||||
} catch (SQLException ex) {
|
||||
|
||||
if (con != null) {
|
||||
try {
|
||||
con.rollback();
|
||||
} catch (SQLException ex1) {
|
||||
Logger lgr = Logger.getLogger(JavaPostgreSqlTransactionEx.class.getName());
|
||||
lgr.log(Level.WARNING, ex1.getMessage(), ex1);
|
||||
}
|
||||
}
|
||||
|
||||
Logger lgr = Logger.getLogger(JavaPostgreSqlTransactionEx.class.getName());
|
||||
lgr.log(Level.SEVERE, ex.getMessage(), ex);
|
||||
}
|
||||
} catch (SQLException ex) {
|
||||
|
||||
Logger lgr = Logger.getLogger(JavaPostgreSqlTransactionEx.class.getName());
|
||||
lgr.log(Level.SEVERE, ex.getMessage(), ex);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -8,18 +8,22 @@ public class JavaPostgreSqlVersion {
|
|||
|
||||
public static void main(String[] args) {
|
||||
|
||||
//connection URL
|
||||
String url = "jdbc:postgresql://192.168.178.25:5432/testdb";
|
||||
String user = "user";
|
||||
String key = "key";
|
||||
//connection URL for the postgres database
|
||||
//jdbc:postgresql://<host>:<port>/<database name>
|
||||
String url = "jdbc:postgresql://localhost:5432/testdb";
|
||||
|
||||
String user = "usr";
|
||||
String password = "#password";
|
||||
|
||||
try (
|
||||
//establish connection
|
||||
Connection con = DriverManager.getConnection(url, user, key);
|
||||
Connection con = DriverManager.getConnection(url, user, password);
|
||||
|
||||
//create object for sending SQL statements
|
||||
Statement st = con.createStatement();
|
||||
|
||||
//execute SQL statement
|
||||
//rs is a table of data returned by a SQL statement
|
||||
//rs object is a table of data returned by a SQL statement
|
||||
ResultSet rs = st.executeQuery("SELECT VERSION()")) {
|
||||
|
||||
while (rs.next()) {
|
||||
|
|
Loading…
Reference in New Issue