
import java.sql.*;

public class ThreadPS {

	public static void main(String args[]) throws Exception {
		Class.forName("org.postgresql.Driver");
		Connection conn = DriverManager.getConnection("jdbc:postgresql://localhost:5432/jurka","jurka","");

		Statement stmt = conn.createStatement();
		stmt.execute("CREATE TEMP TABLE threadps (a int, b text, c text)");
		stmt.close();

		PreparedStatement ps = conn.prepareStatement("INSERT INTO threadps VALUES (?,?,?)");
		ps.setInt(1,0);
		ps.setString(2, "0");
		ps.setString(3, "");

		Executor exec = new Executor(ps);
		new Thread(exec, "Exec").start();

		Setter set = new Setter(ps);
		new Thread(set, "Set").start();
	}

	static class Executor implements Runnable {
		private PreparedStatement ps;
		Executor(PreparedStatement ps) {
			this.ps = ps;
		}

		public void run() {
			while (true) {
				try {
					ps.executeUpdate();
				} catch (SQLException sqle) {
					sqle.printStackTrace();
					System.exit(1);
				}
			}
		}
	}

	static class Setter implements Runnable {
		private PreparedStatement ps;
		Setter(PreparedStatement ps) {
			this.ps = ps;
		}

		public void run() {
			int count = 0;
			while (true) {
				try {
					if (count++ % 2 == 0) {
						ps.setInt(1,10);
						ps.setString(2, "aa");
						ps.setString(3, "bb");
					} else {
						ps.setInt(1, 1);
						ps.setString(2, "a");
						ps.setString(3, "b");
					}
				} catch (SQLException sqle) {
					sqle.printStackTrace();
					System.exit(1);
				}
			}
		}
	}

}
