postgresql-jdbc/Sanity/basic-sanity/Test.java
Marian Koncek 18b88dc477 Import tests
2025-03-10 16:10:48 +01:00

98 lines
3.8 KiB
Java

// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
// Test.java of /CoreOS/postgresql-jdbc/Sanity/basic-sanity
// Description: Runs basic sanity - select, insert, update, delete
// Author: Lukas Zachar <lzachar@redhat.com>
//
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
// Copyright (c) 2013 Red Hat, Inc. All rights reserved.
//
// This copyrighted material is made available to anyone wishing
// to use, modify, copy, or redistribute it subject to the terms
// and conditions of the GNU General Public License version 2.
//
// This program is distributed in the hope that it will be
// useful, but WITHOUT ANY WARRANTY; without even the implied
// warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
// PURPOSE. See the GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public
// License along with this program; if not, write to the Free
// Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
// Boston, MA 02110-1301, USA.
//
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
public class Test {
public static void main(String[] args) throws Exception {
String jdbs_connection = "jdbc:postgresql://127.0.0.1/mydbtest";
String user = "dbuser";
ResultSet rs = null;
System.out.println("Test:: Begin");
try {
Connection connection = DriverManager.getConnection(jdbs_connection, user, null);
Statement stmt = connection.createStatement();
if (stmt.execute("SELECT VERSION()")) {
rs = stmt.getResultSet();
while (rs.next()) {
System.out.println("SELECT VERSION: " + rs.getString(1));
}
}
System.out.println("Test:: Create table");
stmt.execute("CREATE TABLE Writers(Id SERIAL, Name VARCHAR(25))");
stmt.execute("INSERT INTO Writers(Name) VALUES('Jack London')");
stmt.execute("INSERT INTO Writers(Name) VALUES('Honore de Balzac')");
stmt.execute("INSERT INTO Writers(Name) VALUES('Lion Feuchtwanger')");
System.out.println("Test:: Read newly created table");
if (stmt.execute("SELECT * FROM Writers")) {
rs = stmt.getResultSet();
while (rs.next()) {
int myId = rs.getInt("id");
String myName = rs.getString("Name");
System.out.println(myId + ": " + myName);
}
}
System.out.println("Test:: Update row (2-> Maupasant)");
if (stmt.executeUpdate("UPDATE Writers SET Name = 'Guy de Maupasant' WHERE Id = 2") != 1 ) {
System.out.println("Test:: update count was not 1");
}
System.out.println("Test:: Delete row (1-> London)");
if (stmt.executeUpdate("DELETE FROM Writers WHERE Id = 1") != 1) {
System.out.println("Test:: delete count was not 1");
}
System.out.println("Test:: Read modified table");
if (stmt.execute("SELECT * FROM Writers")) {
rs = stmt.getResultSet();
while (rs.next()) {
int myId = rs.getInt("id");
String myName = rs.getString("Name");
System.out.println(myId + ": " + myName);
}
}
System.out.println("Test:: Drop table");
stmt.execute("DROP TABLE Writers");
} catch (Exception e) {
e.printStackTrace();
System.exit(1);
}
System.out.println("Test:: End");
}
}