Repository navigation
Expand file tree
/
Copy pathQuestion105.java
More file actions
45 lines (37 loc) · 1.85 KB
/
Copy pathQuestion105.java
File metadata and controls
45 lines (37 loc) · 1.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
import java.sql.*;
import java.lang.*;
public class RenameTable {
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");
// Open a connection
conn = DriverManager.getConnection(DB_URL);
stmt = conn.createStatement();
// The statement containing SQL command to create table "players"
String CREATE_TABLE_SQL="CREATE TABLE players (UID INT, First_Name VARCHAR(45), Last_Name VARCHAR(45), Age INT);";
// Execute the statement containing SQL command
stmt.executeUpdate(CREATE_TABLE_SQL);
// Write the SQL command to rename a table
String alter="ALTER TABLE players RENAME TO sports;";
// Execute the SQL command
stmt.executeUpdate(alter);
ResultSet rs = stmt.executeQuery("SELECT * FROM sports;");
ResultSetMetaData rsmd = rs.getMetaData();
System.out.println("No. of columns : " + rsmd.getColumnCount());
System.out.println("Column 1 Name: " + rsmd.getColumnName(1));
System.out.println("Column 1 Type : " + rsmd.getColumnTypeName(1));
System.out.println("Column 2 Name: " + rsmd.getColumnName(2));
System.out.println("Column 2 Type : " + rsmd.getColumnTypeName(2));
System.out.println("Column 3 Name: " + rsmd.getColumnName(3));
System.out.println("Column 3 Type : " + rsmd.getColumnTypeName(3));
System.out.println("Column 4 Name: " + rsmd.getColumnName(4));
System.out.println("Column 5 Type : " + rsmd.getColumnTypeName(4));
stmt.close();
conn.close();
}
catch(Exception e){ System.out.println(e);}
}
}