Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.sql.*;
- public class DBGuy {
- private Connection c = null;
- public DBGuy() {
- try {
- Class.forName("org.postgresql.Driver");
- c = DriverManager
- .getConnection("jdbc:postgresql://localhost:5432/testdb",
- "user", "pass");
- } catch(Exception e) {
- e.printStackTrace();
- System.exit(0);
- }
- System.out.println("Opened database sucessfully.");
- }
- public void createCompanyTable() {
- Statement s = null;
- try {
- s = c.createStatement();
- String sql = "CREATE TABLE COMPANY (" +
- "ID INT PRIMARY KEY NOT NULL," +
- "NAME TEXT NOT NULL," +
- "AGE INT NOT NULL," +
- "ADDRESS CHAR(50)," +
- "SALARY REAL)";
- s.executeUpdate(sql);
- s.close();
- } catch(Exception e) {
- e.printStackTrace();
- System.exit(0);
- }
- System.out.println("Company table created successfully.");
- }
- public void deleteCompanyTable() {
- Statement s = null;
- try {
- s = c.createStatement();
- String sql = "DROP TABLE IF EXISTS COMPANY";
- s.execute(sql);
- } catch(Exception e) {
- e.printStackTrace();
- System.exit(0);
- }
- }
- public void closeConnection() {
- try {
- c.close();
- } catch (Exception e) {
- e.printStackTrace();
- System.exit(0);
- }
- System.out.println("Connection closed.");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement