0% found this document useful (0 votes)
11 views4 pages

AJP Practical 18

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)
11 views4 pages

AJP Practical 18

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/ 4

Practical 18 :Write a program to insert and retrieve data from database using JDBC– 2200100176 Vedika Mohite

Program code 1: Write a program to create a student table in database and insert a record in a student table.

import java.sql.*;
import javax.swing.JOptionPane;
import java.awt.*;
import java.awt.event.*;
public class JDBCProgram5 implements ActionListener{
Frame f1;
Label l1,l2,l3;
TextField t1,t2,t3;
Button b1;
static Statement stmt;
JDBCProgram5() {
f1=new Frame();
l1=new Label("Roll NO:");
l2=new Label("Name:");
l3=new Label("Marks:"); Output :-
t1=new TextField(20);
t2=new TextField(20);
t3=new TextField(20);
b1=new Button("Insert");
f1.add(l1);
f1.add(t1);
f1.add(l2);
f1.add(t2);
f1.add(l3);
f1.add(t3);
f1.add(b1);
b1.addActionListener(this);
f1.setLayout(new FlowLayout());
f1.setSize(500,500);
f1.setVisible(true);
}
public static void main(String[] args) throws SQLException {
Connection
con=DriverManager.getConnection("jdbc:mysql://localhost:3306/mydata","root"
,"root");
stmt=con.createStatement();
new JDBCProgram5();
}
@Override
public void actionPerformed(ActionEvent arg0) {
String query="insert into
stud(rollno,name,marks)values('"+t1.getText()+"','"+t2.getText()+"','"+t3.getText(
)+"');";
try {
int cn=stmt.executeUpdate(query);
if(cn>0) {
JOptionPane.showMessageDialog(f1, "Row inserted");
}else {
JOptionPane.showMessageDialog(f1, "cannot insert a row");
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}

Vedika Mohite
Practical 18 :Write a program to insert and retrieve data from database using JDBC– 2200100176 Vedika Mohite

Program code 2: Write the output of following code.

import java.sql.*;
public class JDBCProgram8 {

public static void main(String[] args) {


Connection con;
try {
con =
DriverManager.getConnection("jdbc:mysql://localhost:3306/mydata", "root",
"root");
System.out.println("Connection to the database created");
Statement smt=con.createStatement();
ResultSet rs=smt.executeQuery("Select * from stud");
String text="";
System.out.println("Roll Number\tName");
while(rs.next()) {
text=text+rs.getString(1)+"\t\t"+rs.getString(2)+"\n";
}
System.out.println(text);
} catch (SQLException e) {
System.out.println("sql error");
}

Output :-

Vedika Mohite
Practical 18 :Write a program to insert and retrieve data from database using JDBC– 2200100176 Vedika Mohite

Exercise Q.1: Write a program to create employee table in database having two columns “emp_id” and “emp_name”.

import java.sql.*;
public class JDBCProgram6 {

public static void main(String[] args) throws SQLException {


Connection
con=DriverManager.getConnection("jdbc:mysql://localhost:3306/mydata","root"
,"root");
Statement stmt=con.createStatement();
String query="create table employee(emp_id int primary
key,emp_name varchar(45));";
int cn=stmt.executeUpdate(query);
if(cn==0)
{
System.out.println("Table is created");
} else {

System.out.println("Error in creating table");

Output :-

Vedika Mohite
Practical 18 :Write a program to insert and retrieve data from database using JDBC– 2200100176 Vedika Mohite

Exercise Q.2: Develop a program to display the name and roll_no of students from “student table”having
percentage>70.


import java.sql.*;
public class JDBCProgram7 {

public static void main(String[] args) throws SQLException {


Connection
con=DriverManager.getConnection("jdbc:mysql://localhost:3306/mydata","root"
,"root");
Statement stmt=con.createStatement();
ResultSet rs=stmt.executeQuery("select * from stud where
marks>70");
while(rs.next())
{
System.out.println("Name="+rs.getString(2)+" Roll
no="+rs.getString(1));
}

Output :-

Vedika Mohite

You might also like