#1st Database Use. Help ?
41 messages · Page 1 of 1 (latest)
⌛ This post has been reserved for your question.
Hey @strong thorn! Please use
/closeor theClose Postbutton above when you're finished. Please remember to follow the help guidelines. This post will be automatically closed after 300 minutes of inactivity.
TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.
These sound like questions that are better suited for your instructor.
I'm studying with youtube
Suit my database on a local server in order to give acess to my program
I only created the classes and rn i'm studying springboot
import java.util.ArrayList;
public class Student {
private String name; // 1o nome do aluno
private ArrayList<Test> tests = new ArrayList<Test>();
public Student(String name){
this.name=name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public void addTest(int grade){
Test newTest = new Test(grade);
tests.add(newTest);
}
public int getLowestGrade(){
int lowest = 0;
for (Test test : tests) {
if (lowest > test.getGrade()) {
lowest = test.getGrade();
}
}
return lowest;
}
public int getHighestGrade(){
int highest = tests.indexOf(0);
for (Test test : tests) {
if (highest < test.getGrade()) {
highest = test.getGrade();
}
}
return highest;
}
public double testsMean(){
double sum = 0;
for (Test test : tests){
sum += test.getGrade();
}
double mean = sum/(tests.size());
return mean;
}
}
Student class above this message
import java.util.ArrayList;
public class System {
ArrayList<Student> students;
public System(){
students = null;
}
public void addStudent(String name){
Student newStudent = new Student(name);
students.add(newStudent);
}
}
System class
class Test {
private int grade;
public Test(int grade) {
this.grade = grade;
}
public int getGrade() {
return grade;
}
}```
Test class
My system have to show the following options to the user:
Register student
List students
See biggest note.
E. Exit
If I choose the 2st option, the output will be something like this :
Carl
Jean
Peter
B. Back
E. Exit
If I choose 2 ( Jean ), I will have this :
Student : Jean
Tests Mean : 0
Biggest note : 0
Lower note : 0
Done tests : 0
Register test for this student
B. Back
E. Exit
If I choose 1 In order to register test, I have to ask the user the test note and register it .
I need to implement the main and after that start syncing with my db
so you have a Spring Boot application and you want to save these objects to the DB?
Can you show your pom.xml (or build.gradle)?
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.0.4</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.api</groupId>
<artifactId>systemVidenci</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>systemVidenci</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>19</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
ok do you have the database connection properties in your application.properties file already?
So, Spring Boot doesn't know you are using MySQL
If you want to connect to a MySQL DB from Spring, you first need to add the MySQL JDBC driver (MySQL Connector/J) to the pom.xml
So I have to generate a new pom xml from Spring initializer ?
I only found postgre sql
no, you can add it to your existing pom.xml
<dependency>
<groupId>com.mysql</groupId>
<artifactId>mysql-connector-j</artifactId>
</dependency>
<dependency>
<groupId>com.mysql</groupId>
<artifactId>mysql-connector-j</artifactId>
</dependency>
After doing that, can you show your application.properties?
My app properties is empty
I didn't created the db already
server.port = 8090
spring.datasource.url=jdbc:mysql://localhost/api_spring?useTimezone=true&serverTimezone=UTC
spring.datasource.username=root
spring.datasource.password=senha
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.jpa.hibernate.ddl-auto=update
I got this app properties
yes
this is what's needed for MySQL
Now, you need to tell spring what classes you want to persist
in your case, you want to persist instances of the student class
So you need to mark your student class
For doing that, add @Entity to it which tells Spring that Student objects might be persiszed in the DB
each element of a table needs to have a (unique) primary key
What do you want to choose for that? A new field?