0% found this document useful (0 votes)
10 views6 pages

Ajp 18

Uploaded by

fdj5045
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views6 pages

Ajp 18

Uploaded by

fdj5045
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

X.

Program Code:
1. Write a Program to create a Student Table in database and insert a record in a
student table.
Program:
import java.sql.*;

public class Student


{
public static void main(String args[])
{
try {
// Load the JDBC-ODBC driver
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");

// Establish a connection to the database


Connection con =
DriverManager.getConnection("Jdbc:Odbc:dsn_stud", "", "");

// Create a statement for executing SQL commands


Statement st = con.createStatement();

// Create a table called TB_student


st.execute("CREATE TABLE TB_student(rollno number, Name
varchar(20), percentage number)");
System.out.println("Table is created");

// Insert records into TB_student


st.execute("INSERT INTO TB_student VALUES(1, 'Yash', 80)");
st.execute("INSERT INTO TB_student VALUES(2, 'Payal', 85)");
st.execute("INSERT INTO TB_student VALUES(3, 'Manasi', 79)");
st.execute("INSERT INTO TB_student VALUES(4, 'Gaurav', 90)");
System.out.println("Records inserted");

// Execute a SELECT query to fetch data from TB_student


st.execute("SELECT * FROM TB_student");
ResultSet rs = st.getResultSet();

// Process and print the retrieved data


if (rs != null)
{
while (rs.next())
{
System.out.println("Roll no " + rs.getString(1));
System.out.println("Name " + rs.getString(2));
System.out.println("Percentage " + rs.getString(3));
}
}

// Close the statement and the database connection


st.close();
con.close();
} catch (Exception e) {
System.out.println("Exception: " + e);
}
}
}

Output:
XIII. Exercise:
1. Develop a program to create employee table in database having two columns
“emp_id” and “emp_name”.
Program:
import java.sql.*;

public class Student


{
public static void main(String args[])
{
try {
// Load the JDBC-ODBC driver
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");

// Establish a connection to the database


Connection con =
DriverManager.getConnection("Jdbc:Odbc:dsn_stud", "", "");

// Create a statement for executing SQL commands


Statement st = con.createStatement();
// Create a table called TB_Employee with columns emp_id and
emp_name
st.execute("CREATE TABLE TB_Employee(emp_id number,
emp_name varchar(20))");
System.out.println("Table is created");

// Close the statement and the database connection


st.close();
con.close();
} catch (Exception e) {
System.out.println("Exception: " + e);
}
}
}
Output:

2. Develop a program to display the name and roll_no of students from “student table”
having percentage > 70.
Program:
import java.sql.*;

public class Student


{
public static void main(String args[])
{
try {
// Load the JDBC-ODBC driver
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");

// Establish a connection to the database


Connection con =
DriverManager.getConnection("Jdbc:Odbc:dsn_stud", "", "");

// Create a statement for executing SQL commands


Statement st = con.createStatement();

// Execute a SELECT query to fetch records from TB_student with


percentage > 70
st.execute("SELECT * FROM TB_student WHERE percentage > 70");
ResultSet rs = st.getResultSet();

// Process and print the retrieved data


if (rs != null)
{
while (rs.next())
{
System.out.println("Roll no " + rs.getString(1));
System.out.println("Name " + rs.getString(2));
System.out.println("Percentage " + rs.getString(3));
}
}

// Close the statement and the database connection


st.close();
con.close();
} catch (Exception e) {
System.out.println("Exception: " + e);
}
}
}
Output:

You might also like