Create a table named Student with the following details Inse

Create a table named Student with the following details: Insert Sample Data Write a program that accepts student id as input and displays student details. If the student doesn\'t exist, display message.Files to be submitted Student.sql middot DisplayStudent.java

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

}
}

 Create a table named Student with the following details: Insert Sample Data Write a program that accepts student id as input and displays student details. If t
 Create a table named Student with the following details: Insert Sample Data Write a program that accepts student id as input and displays student details. If t

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site