100% found this document useful (1 vote)
792 views3 pages

2nd Mini Project

The document contains code for performing database operations in Java using JDBC including methods for inserting, retrieving, and querying data from database tables. It defines methods for inserting and retrieving subjects, students, and their associated data from database tables. It also includes code for running SQL scripts to initialize the database tables.

Uploaded by

vosew12190
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
100% found this document useful (1 vote)
792 views3 pages

2nd Mini Project

The document contains code for performing database operations in Java using JDBC including methods for inserting, retrieving, and querying data from database tables. It defines methods for inserting and retrieving subjects, students, and their associated data from database tables. It also includes code for running SQL scripts to initialize the database tables.

Uploaded by

vosew12190
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/ 3

1

methods:===========================================================================
==========

package com.fresco.jdbc.code;

import java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import com.fresco.jdbc.code.util.DbUtil;

public class DbOperations {


Connection con;
public DbOperations() {
con = DbUtil.getConnection();
}
public boolean insertSubject(String name) throws SQLException {
PreparedStatement ps = con.prepareStatement("insert into subject(name)
values(?)");
ps.setString(1, name);
int i = ps.executeUpdate();
if(i>0)
return true;
return false;
}
public ArrayList getSubjectById(int id) throws SQLException {
PreparedStatement ps=con.prepareStatement("select * from subject where
id=?");
ps.setInt(1, id);
ResultSet rs=ps.executeQuery();
ArrayList list = new ArrayList();
while(rs.next())
{
list.add(rs.getInt(1));
list.add(rs.getString(2));
}
if(!list.isEmpty())
return list;

return null;
}
public ResultSet getAllSubjects() throws SQLException {
PreparedStatement ps=con.prepareStatement("select * from subject");
ResultSet rs=ps.executeQuery();
return rs;
}
public boolean insertStudent(String student_name, float score, String name)
throws SQLException {

PreparedStatement ps = con.prepareStatement("insert into


student(student_name,score,subject_id) values(?,?,?)");
PreparedStatement ps1=con.prepareStatement("select * from subject where
name=?");
ps1.setString(1, name);
ResultSet rs=ps1.executeQuery();

int id = 0;
if(rs.next())
{
id= rs.getInt(1);
}
else
return false;
ps.setString(1, student_name);
ps.setFloat(2, score);
ps.setInt(3,id);
int i = ps.executeUpdate();
if(i>0)
return true;

return false;

}
public ArrayList getStudentyId(int id) throws SQLException {
PreparedStatement ps=con.prepareStatement("select * from student where
id=?");
ps.setInt(1, id);
ResultSet rs=ps.executeQuery();
ArrayList list = new ArrayList();
while(rs.next())
{
list.add(rs.getInt(1));
list.add(rs.getString(2));
list.add(rs.getFloat(3));
list.add(rs.getInt(4));
}
if(!list.isEmpty())
return list;

return null;
}
public ResultSet getAllStudents() throws SQLException {

PreparedStatement ps=con.prepareStatement("select * from student");


ResultSet rs=ps.executeQuery();
return rs;
}

}
running script:====================================================================
import com.fresco.jdbc.code.util.DbUtil;
public class RunningScripts {
public void runDbScript() throws Exception {
Connection con = DbUtil.getConnection();
ScriptRunner sr = new ScriptRunner(con);
Reader reader = new BufferedReader(new FileReader("db.sql"));
sr.runScript(reader);

}
}
2:

SQL Query:

SELECT country.name,car_model.name,sum(car_model.price*sales.quantity) as
total_sales from sales as sales
inner join country as country on country.id=sales.country_id
inner join car_model as car_model on car_model.id=sales.model_id
where sales.sales_date like "%/%/2020"
group by country.name, car_model.name
order by total_sales asc;

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