Design and Develop SQL DDL statements which demonstrate the use of SQL objects such as Table, View , Index using Client-Data sever(two tier) CREATE TABLE:
package sample;
import java.sql.*;
public class createT {
public static void main(String[] args) {
Connection conn = null;
Statement stmt = null;
String DB_URL = "jdbc:mysql://10.5.7.51/te45_db";
String USER="te45";
String PASS="student";
try{
//STEP 2: Register JDBC driver
Class.forName("com.mysql.jdbc.Driver");
//STEP 3: Open a connection
System.out.println("Connecting to a selected database...");
conn = DriverManager.getConnection(DB_URL, USER, PASS);
System.out.println("Connected database successfully...");
//STEP 4: Execute a query
System.out.println("Creating table in given database...");
stmt = conn.createStatement();
String sql = "CREATE TABLE STUDENTS2 " +
"(StudentID INTEGER not NULL, " +
" StudentName VARCHAR(255), " +
" Major INTEGER, " +
" GPA INTEGER, " +
" TutrID INTEGER, " +
" PRIMARY KEY (StudentID ))";
stmt.executeUpdate(sql);
System.out.println("Created table in given database...");
}catch(SQLException se){
//Handle errors for JDBC
se.printStackTrace();
}catch(Exception e){
//Handle errors for Class.forName
e.printStackTrace();
}finally{
//finally block used to close resources
try{
if(stmt!=null)
conn.close();
}catch(SQLException se){
}// do nothing
try{
if(conn!=null)
conn.close();
}catch(SQLException se){
se.printStackTrace();
}//end finally try
}//end try
System.out.println("Goodbye!");
}
}
/*OUTPUT:-
Connecting to a selected database...
Connected database successfully...
Creating table in given database...
Created table in given database...
Goodbye!
*/
CREATING AND DISPLAYING VIEW:-
package sample1;
import java.sql.*;
public class createV {
public static void main(String[] args) {
Connection con = null;
String url = "jdbc:mysql://10.5.7.51:3306/te45_db";
String driverName = "com.mysql.jdbc.Driver";
String userName = "te45";
String password = "student";
try {
Class.forName(driverName).newInstance();
con = DriverManager.getConnection(url, userName, password);
try {
Statement st = con.createStatement();
String code = " Create VIEW VW_CIS As select * from STUDENTS";
st.executeUpdate(code);
System.out.println(" process successfully created!");
} catch (SQLException s) {
System.out.println("Table already exists!");
}
con.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
OUTPUT:-
process successfully created!
import java.sql.*;
public class createT {
public static void main(String[] args) {
Connection conn = null;
Statement stmt = null;
String DB_URL = "jdbc:mysql://10.5.7.51/te45_db";
String USER="te45";
String PASS="student";
try{
//STEP 2: Register JDBC driver
Class.forName("com.mysql.jdbc.Driver");
//STEP 3: Open a connection
System.out.println("Connecting to a selected database...");
conn = DriverManager.getConnection(DB_URL, USER, PASS);
System.out.println("Connected database successfully...");
//STEP 4: Execute a query
System.out.println("Creating table in given database...");
stmt = conn.createStatement();
String sql = "CREATE TABLE STUDENTS2 " +
"(StudentID INTEGER not NULL, " +
" StudentName VARCHAR(255), " +
" Major INTEGER, " +
" GPA INTEGER, " +
" TutrID INTEGER, " +
" PRIMARY KEY (StudentID ))";
stmt.executeUpdate(sql);
System.out.println("Created table in given database...");
}catch(SQLException se){
//Handle errors for JDBC
se.printStackTrace();
}catch(Exception e){
//Handle errors for Class.forName
e.printStackTrace();
}finally{
//finally block used to close resources
try{
if(stmt!=null)
conn.close();
}catch(SQLException se){
}// do nothing
try{
if(conn!=null)
conn.close();
}catch(SQLException se){
se.printStackTrace();
}//end finally try
}//end try
System.out.println("Goodbye!");
}
}
/*OUTPUT:-
Connecting to a selected database...
Connected database successfully...
Creating table in given database...
Created table in given database...
Goodbye!
*/
CREATING AND DISPLAYING VIEW:-
package sample1;
import java.sql.*;
public class createV {
public static void main(String[] args) {
Connection con = null;
String url = "jdbc:mysql://10.5.7.51:3306/te45_db";
String driverName = "com.mysql.jdbc.Driver";
String userName = "te45";
String password = "student";
try {
Class.forName(driverName).newInstance();
con = DriverManager.getConnection(url, userName, password);
try {
Statement st = con.createStatement();
String code = " Create VIEW VW_CIS As select * from STUDENTS";
st.executeUpdate(code);
System.out.println(" process successfully created!");
} catch (SQLException s) {
System.out.println("Table already exists!");
}
con.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
OUTPUT:-
process successfully created!
Comments
Post a Comment