Create a table named Student with the following details Inse
Solution
import java.sql.*;
public class DisplayStudent {
// JDBC driver name and database URL
static final String JDBC_DRIVER = \"com.mysql.jdbc.Driver\";
static final String DB_URL = \"jdbc:mysql://localhost/students\";
// Database credentials
static final String USER = \"name\";
static final String PASS = \"password\";
public static void main(String[] args) {
Connection conn = null;
Statement stmt = null;
try{
//STEP 2: Register JDBC driver
Class.forName(\"com.mysql.jdbc.Driver\");
// Open a connections
System.out.println(\"Connecting to a selected database...\");
conn = DriverManager.getConnection(DB_URL, USER, PASS);
System.out.println(\"Connected database successfully...\");
//Creating table in given database
stmt = conn.createStatement();
String sql = \"CREATE TABLE Student \" +
\"(StudentID INTEGER not NULL, \" +
\" Name VARCHAR(25) not null, \" +
\" Major Text(4) not null, \" +
\" Level VARCHAR(10), \" +
\" GPA Decimal (2,2))\";
stmt.executeUpdate(sql);
System.out.println(\"Created Student table in database...\");
}catch(SQLException se){
//Handle errors for JDBC
se.printStackTrace();
}catch(Exception e){
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
}//try ending
}
}

