0% found this document useful (0 votes)
2 views

pract18_s

The document contains Java programs that demonstrate database operations using JDBC, including creating a Student table, inserting records, and querying data. It provides code examples for connecting to a MySQL database, executing SQL statements, and handling user input through a graphical interface. Additionally, it includes exercises for creating an Employee table and displaying students with marks greater than 70.

Uploaded by

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

pract18_s

The document contains Java programs that demonstrate database operations using JDBC, including creating a Student table, inserting records, and querying data. It provides code examples for connecting to a MySQL database, executing SQL statements, and handling user input through a graphical interface. Additionally, it includes exercises for creating an Employee table and displaying students with marks greater than 70.

Uploaded by

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

Maitreyee Lohar TYCO 2254

 Program Code:
1.Write a program to create a Student table in database and then insert a record in 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:");
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();
}
Maitreyee Lohar TYCO 2254

Output:

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:
Maitreyee Lohar TYCO 2254

>Exercise:
1. Develop 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:
Maitreyee Lohar TYCO 2254

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

import java.sql.*; public class pract18_4 { 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("Roll no:"+rs.getString(1)+ " Name:"+rs.getString(2));
}
}
}
Output:

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy