Tuesday, April 7, 2020

PROGRAMMING IN JAVA WEEK 10

QUESTION 1:

import java.*;
import java.sql.*;

QUESTION 2:

// Open a connection and check connection status
conn = DriverManager.getConnection(DB_URL);
if(conn.isClosed())
  System.out.print("false");
else
  System.out.print("true");

QUESTION 3:

import java.sql.*;
import java.util.Scanner;
import java.lang.*;

public class Question103 {
    public static void main(String args[]) {
        try {
              Connection conn = null;
              Statement stmt = null;
              String DB_URL = "jdbc:sqlite:/tempfs/db";
              System.setProperty("org.sqlite.tmpdir", "/tempfs");
            conn = DriverManager.getConnection(DB_URL);
              conn.close();
              System.out.print(conn.isClosed());
        }
       catch(Exception e){ System.out.println(e);}
    }
}


QUESTION 4:

// The statement containing SQL command to create table "players"
String sql = "CREATE TABLE PLAYERS "+
  "(UID INT, "+
  "First_Name VARCHAR(45), "+
  "Last_Name VARCHAR(45), "+
  "Age INT, "+
  " PRIMARY KEY(UID))";

// Execute the statement containing SQL command below this comment
stmt.executeUpdate(sql);


QUESTION 5:

// Write the SQL command to rename a table
String str = "alter table PLAYERS rename to SPORTS";

// Execute the SQL command
stmt.executeUpdate(str);


2 comments: