Create a table named Student with the following details: Insert Sample Data Writ
ID: 3779211 • Letter: C
Question
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
• DisplayStudent.java
Explanation / Answer
Please let me know if you need more information:-
=========================================
1.Creating TABLE:-
--------------------------------
CREATE TABLE Student
(
StudentID int NOT NULL PRIMARY KEY,
Name varchar(25) NOT NULL,
Major TEXT NOT NULL,
Level varchar(10),
GPA DECIMAL(2,2),
CONSTRAINT chk_Level CHECK(Level IN ('FreshMan', 'Sophomore', 'Junior', 'Senior'))
)
---------------------------------------------
Java Code to Get the Student Details:-
---------------------------------------------------
Lets Assume You have ACCESS db:
-----------------------
import java.sql.*;
import java.io.*;
import java.lang.*;
class connectiontoaccess
{
public static void main(String[] chinnasrinu)throws ClassNotFoundException,SQLException
{
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection conn=null;
conn=DriverManager.getConnection("jdbc:oracle:thin:@135.188.3.81:1521:bppkc","bppadm","RELEASE09");
System.out.println("If this is printed you are connected to DB");
}
}
----------------------------
mYSQL:-
--------------------------
import java.sql.*;
import java.io.*;
import java.lang.*;
class connectiontomySQL
{
public static void main(String[] chinnasrinu)throws ClassNotFoundException,SQLException
{
Class.forName("com.mysql.jdbc.Driver");
Connection conn=null;
conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/mysql","root","chinnasrinu");
conn=DriverManager.getConnection("jdbc:mysql://localhost/chinnasrinu","chinna","chinnasrinu");
conn=DriverManager.getConnection("jdbc:mysql://localhost/test","root","chinnasrinu");
conn=DriverManager.getConnection("jdbc:mysql://localhost/information_schema","root","chinnasrinu");
System.out.println("welcome to MySQL");
}
}
-----------------------------------------------------
Now to retrieve data from Student Db:-
-------------------------------------------------
import java.sql.*;
import java.io.*;
import java.lang.*;
class retrive
{
public static void main(String[] chinnasrinu)throws ClassNotFoundException,SQLException
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection conn=null;
conn=DriverManager.getConnection("jdbc:odbc:Database1","chinnasrinu","chinnasrinu");
Statement st=conn.createStatement();
ResultSet rs=st.executeQuery("SELECT * FROM student");
while(rs.next())
{
float nm=rs.getFloat(1);
String m=rs.getString(2);
System.out.println(nm);
System.out.println(m);
}
rs.close();
conn.close();
}
}
=========================
DisplayStudent.java
-------------------------------------------
import java.sql.*;
import java.io.*;
import java.lang.*;
class retrive
{
public static void main(String[] chinnasrinu)throws ClassNotFoundException,SQLException
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection conn=null;
conn=DriverManager.getConnection("jdbc:odbc:Database1","chinnasrinu","chinnasrinu");
Statement st=conn.createStatement();
String StudentName = "";//Optional
String sqlQry = "SELECT * FROM Student where studentID='"+StudentName+"'";
ResultSet rs=st.executeQuery(sqlQry);
while(rs.next())
{
int id = rs.getInt("StudentID");
String Name = rs.getString("Name");
java.sql.Blob ablob = rs.getBlob("Major");
String Major = new String(ablob.getBytes(1L, (int) ablob.length()));
String Level = rs.getString("Level");
double GPA = rs.getBigDecimal("GPA").doubleValue();
}
rs.close();
conn.close();
}
}
-------------------------------------------------
Thanks
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.