Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.lang.System.*;
- import java.nio.file.* ;
- import java.sql.*;
- import java.io.*;
- import java.util.*;
- public class TestDB {
- public static void main(String[] args) throws IOException {
- try {
- runTest();
- } catch (SQLException ex) {
- for (Throwable t : ex)
- t.printStackTrace();
- }
- }
- public static void runTest() throws SQLException, IOException {
- try (Connection conn = getConnection(); Statement stat = conn.createStatement()) {
- stat.executeUpdate("CREATE TABLE G (id BIGINT,name CHAR(20))");
- stat.executeUpdate("ALTER TABLE G ALTER COLUMN id SET DEFAULT NEXTVAL('seq')");
- for( int i=0;i<5;i++)
- stat.executeUpdate("INSERT INTO public.g (id,name) VALUES(DEFAULT,'Hello, world2')");
- try (ResultSet result=stat.executeQuery("SELECT NEXTVAL('seq')"))
- {
- while(result.next())
- System.out.println(result.getString(1));
- }
- ////stat.executeUpdate("DROP TABLE G");
- // stat.executeUpdate("DROP TABLE Greetings");
- }
- }
- public static Connection getConnection()throws SQLException,IOException
- {
- Properties props=new Properties();
- try (InputStream in =
- Files.newInputStream(Paths.get("Database.properties")))
- {
- props.load(in);
- }
- String drivers = props.getProperty("jdbc.drivers");
- if(drivers!=null)
- {
- System.setProperty("jdbc.drivers",drivers);
- }
- String url=props.getProperty("jdbc.url");
- String username = props.getProperty("jdbc.username");
- String password = props.getProperty("jdbc.password");
- return DriverManager.getConnection(url,username,password);
- }
- }
Add Comment
Please, Sign In to add comment