How to create a Java Project with JDBC Spring Framework

Joined
Nov 5, 2023
Messages
5
Reaction score
1
I want to create a Java Application where values of an object are read from a database, but using JDBC with Spring Framework.
 
Joined
Nov 6, 2023
Messages
11
Reaction score
0
public class MainApp {
public static void main(String[] args) {
Scanner scanner= new Scanner(System.in);
String nume=scanner.next();
int varsta=scanner.nextInt();
String data=scanner.next();

ApplicationContext context=new ClassPathXmlApplicationContext("fisier.xml");
OperatiiBD operatii= (OperatiiBD) context.getBean("operatiiJDBC");
operatii.insert(nume,varsta,data);
List<Winx> lista =operatii.getListaWinx();
for (var i:lista) {
System.out.println(i);

}
 
Joined
Nov 6, 2023
Messages
11
Reaction score
0
import org.springframework.jdbc.core.RowMapper;

import java.sql.ResultSet;
import java.sql.SQLException;

public class Winxmapper implements RowMapper<Winx> {

public Winx mapRow (ResultSet resultSet, int Rownum) throws SQLException{
Winx winx=new Winx();
winx.setId(resultSet.getInt("id"));
winx.setNume(resultSet.getString("Name"));
winx.setVarsta(resultSet.getInt("Age"));
winx.setNastere(resultSet.getString("birthday"));
return winx;
}
 
Joined
Nov 6, 2023
Messages
11
Reaction score
0
import java.util.List;
import javax.sql.DataSource;
import org.springframework.jdbc.core.JdbcTemplate;
class OperatiiBD {
private DataSource dataSource;
private JdbcTemplate jdbcTemplateObject;
public void setDataSource(DataSource dataSource) {
this.dataSource = dataSource;
this.jdbcTemplateObject = new JdbcTemplate(dataSource);
}
public void insert(String nume, int varsta, String nastere) {
String SQL = "insert into fairies (Name, Age, birthday) values ( ?, ?, ?)";
jdbcTemplateObject.update( SQL, nume, varsta, nastere);
}
public void update(int id, int varsta){
String SQL = "update fairies set Age = ? where id = ?";
jdbcTemplateObject.update(SQL, id, varsta);
}
public void delete(int id){
String SQL = "delete from fairies where id = ?";
jdbcTemplateObject.update(SQL, id);
}

public Winx getWinx(int id) {
String SQL = "select * from fairies where id = ?";
Winx winx = jdbcTemplateObject.queryForObject(SQL,new Object[]{id},
new Winxmapper());
return winx;
}
public List<Winx> getListaWinx() {
String SQL = "select * from fairies";
List <Winx> winx = jdbcTemplateObject.query(SQL,new Winxmapper());
return winx;
}
}
 
Joined
Nov 6, 2023
Messages
11
Reaction score
0
public static void adaugareProdus(Connection connection){
Scanner scanner=new Scanner(System.in);
String nume=scanner.next();
int vechime=scanner.nextInt();
int id_magazin=scanner.nextInt();
String sqlVerificare="select * from magazin where id= ?";
try(PreparedStatement ps=connection.prepareStatement(sqlVerificare))
{
ps.setInt(1, id_magazin);
ResultSet rs=ps.executeQuery();
if(!rs.next())
{
throw new SQLException("Nu exista asa id in tabela magazin");
}else{
String sql="insert into produs(nume, vechime, id_magazin) values(?, ?, ?)";
try(PreparedStatement ps1=connection.prepareStatement(sql)) {
ps1.setString(1, nume);
ps1.setInt(2, vechime);
ps1.setInt(3, id_magazin);
ps1.executeUpdate();
System.out.println("Adaugare produs");
}
}
} catch (SQLException e) {
e.printStackTrace();
} }
 
Joined
Nov 6, 2023
Messages
11
Reaction score
0
public static void main(String[] args) {
try {
String url="jdbc:mysql://localhost:3306/sliv_test_2";
int opt;
Scanner scanner=new Scanner(System.in);
Connection connection = DriverManager.getConnection(url, "root", "");
while (true){
System.out.println("1. Adaugare magazin");
System.out.println("2. Adaugare produs");
opt=scanner.nextInt();
switch (opt)
{
case 1:
adaugareMagazin(connection);
break;
case 2:
adaugareProdus(connection);
break;
}
}
} catch (SQLException e) {
e.printStackTrace();
} }
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,056
Messages
2,570,443
Members
47,090
Latest member
LillieRipl

Latest Threads

Top