KEMBAR78
Database Setquestions | PDF | Computer Data | Software Engineering
0% found this document useful (0 votes)
9 views4 pages

Database Setquestions

The document contains multiple Java programs that interact with MySQL databases across different years from 2016 to 2023. Each program demonstrates various SQL operations such as selecting, inserting, updating, and querying data from different tables. Additionally, it includes a JSP form and a servlet for handling library book data submissions in 2023.

Uploaded by

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

Database Setquestions

The document contains multiple Java programs that interact with MySQL databases across different years from 2016 to 2023. Each program demonstrates various SQL operations such as selecting, inserting, updating, and querying data from different tables. Additionally, it includes a JSP form and a servlet for handling library book data submissions in 2023.

Uploaded by

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

//2016

import java.sql.*;
public class display {
public static void main(String[] args)
{
try
{
//step1: Load the database driver
Class.forName("com.mysql.jdbc.Driver");
//step2: connect to the database
Connection
con=DriverManager.getConnection("jdbc:mysql://localhost:3306/LoginData","root","123
4");
//step3: create sql query and execute it
String sql="select* from logindetails";
Statement st=con.createStatement();
ResultSet rs=st.executeQuery(sql);
while(rs.next())
{
System.out.println(rs.getString("Username"));
System.out.println(rs.getString("Password"));

}
//step4: close
con.close();
}
catch(Exception ex){
System.out.println(ex);
}
}
}

//2017
import java.sql.*;
public class customer {
public static void main(String args[])
{
try {

Class.forName("com.mysql.jdbc.Driver");
Connection
v=DriverManager.getConnection("jdbc:mysql://localhost:3306/testdb","root","1234");
String sql="Select* from employee";
Statement st=v.createStatement();
ResultSet re=st.executeQuery(sql);
while(re.next())
{
System.out.print(re.getString("id")+",");
System.out.print(re.getString("name")+",");
System.out.print(re.getString("post")+",");
System.out.print(re.getString("salary")+"\n");

}
}
catch(Exception e)
{
System.out.println(e);
}
}

//2018
import java.sql.*;
public class Bookdetails {
public static void main (String args[])
{ try {
Class.forName("com.mysql.jdbc.Driver");
Connection
cn=DriverManager.getConnection("jdbc:mysql://localhost:3306/Library","root","1234")
;
String sql="Insert into Book values(101,'Tog of war','aarati',2000),
(102,'Tog of Cool','prerna',3000),(103,'Tog of Cool','pragati',2500),(104,'Tog of
Cool','yuvraj',500),(105,'Tog of Cool','jyoti',2550)";

Statement stm=cn.createStatement();
stm.executeUpdate(sql);

}
catch(Exception t)
{
System.out.println(t);
}
}

//2019
import java.sql.*;
import java.util.*;

public class bookdiaplay {


public static void main(String args[])
{
try {
Class.forName("com.mysql.jdbc.Driver");
Connection
con=DriverManager.getConnection("jdbc:mysql://localhost:3306/Marvel","root","1234")
;

String sql="Select name from Movie where genre="action";


Statement dm=con.createStatement();
ResultSet rd=dm.executeQuery(sql);
while(rd.next())
{
System.out.println(rd.getString("name"));
}

}
catch(Exception e){
System.out.println(e);
}
}

//2022

import java.sql.*;

public class employeedetail {


public static void main(String args[])

{
try
{
Class.forName("com.mysql.jdbc.Driver");
Connection
con=DriverManager.getConnection("jdbc:mysql://localhost:3306/college_db","root","12
34");
Statement st=con.createStatement();
String sql="Update employees set salary=20000 where employee_id =6";
st.executeUpdate(sql);

}
catch(Exception e)
{
System.out.println(e);
}
}
}

//2023

libraryform.jsp
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<center>
<form action="Library" method="post"><br><br>
Author:<input type="text" name="author"><br><br>
Price:<input type="text" name="price"><br><br>
<input type="submit" value="submit">

</form>
</center>
</body>
</html>
//2023_Servletfile_Library.java
import java.io.*;
import java.sql.*;
import jakarta.servlet.*;
import jakarta.servlet.http.*;

@WebServlet("/Library")
public class Library extends HttpServlet {
private static final long serialVersionUID = 1L;

public Library() {

protected void doPost(HttpServletRequest request, HttpServletResponse


response) throws ServletException, IOException {

String author=request.getParameter("author");
int price=Integer.parseInt(request.getParameter("price"));
try
{
Class.forName("com.mysql.jdbc.Driver");
Connection
n=DriverManager.getConnection("jdbc:mysql://localhost:3306/Library","root","1234");
PreparedStatement ps=n.prepareStatement("select* from Book where
author=? and price<? ");
ps.setString(1, author);
ps.setInt(2, price);
ResultSet re=ps.executeQuery();
PrintWriter out=response.getWriter();
while(re.next())
{

out.println(re.getInt("id"));
out.println(re.getString("title"));
out.println(re.getString("author"));
out.println(re.getInt("price"));

}
catch(ClassNotFoundException e)
{

}
catch(SQLException e)
{

}
}

You might also like