JDBC - JAVA Developer Hands-On Solutions
The Course Id is 63427.
1. RunningScripts.java
package com.fresco.jdbc.code.util;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.Reader;
import java.sql.Connection;
import org.apache.ibatis.jdbc.ScriptRunner;
public class RunningScripts {
public void runDbScript() throws Exception {
ScriptRunner sr = new ScriptRunner(DbUtil.getConnection());
//Creating a reader object
Reader reader = new BufferedReader(new FileReader("./db.sql"));
//Running the script
sr.runScript(reader);
}
}
2. DBOperations.java
package com.fresco.jdbc.code;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import com.fresco.jdbc.code.util.DbUtil;
public class DbOperations {
Connection con;
public static final String INSERT_SUBJECT = "INSERT INTO subject (name) VALUES (?)";
public static final String GET_ID_SUBJECT = "SELECT id, name FROM subject WHERE id = ?";
public static final String GET_ALL_SUBJECT = "SELECT id, name FROM subject";
public static final String INSERT_STUDENT = "INSERT INTO student (name, score, id) VALUES (?, ?, id) SELECT id FROM subject WHERE name = \"?\"";
public static final String GET_ID_STUDENT = "SELECT id, student_name, score, subject_id FROM student WHERE id = ?";
public static final String GET_ALL_STUDENT = "SELECT * FROM student";
public DbOperations() {
con = DbUtil.getConnection();
}
public boolean insertSubject(String name) throws SQLException {
PreparedStatement preparedStatement = this.con.prepareStatement(INSERT_SUBJECT);
preparedStatement.setString(1, name);
return preparedStatement.execute();
}
public ArrayList getSubjectById(int id) throws SQLException {
PreparedStatement preparedStatement = this.con.prepareStatement(GET_ID_SUBJECT);
preparedStatement.setInt(1, id);
ResultSet resultSet = preparedStatement.executeQuery();
ArrayList arrayList = new ArrayList();
while(resultSet.next()){
arrayList.add(resultSet.getInt("id"));
arrayList.add(resultSet.getString("name"));
}
return arrayList;
}
public ResultSet getAllSubjects() throws SQLException {
PreparedStatement preparedStatement = this.con.prepareStatement(GET_ALL_SUBJECT);
ResultSet resultSet = preparedStatement.executeQuery();
return resultSet;
}
public boolean insertStudent(String student_name, float score, String name) throws SQLException {
PreparedStatement statement = this.con.prepareStatement("INSERT INTO student (student_name, score, subject_id) VALUES (?, ?, (SELECT id FROM subject WHERE name = ?))");
statement.setString(1, student_name);
statement.setFloat(2, score);
statement.setString(3,name);
return statement.execute();
}
public ArrayList getStudentyId(int id) throws SQLException {
PreparedStatement statement = this.con.prepareStatement(GET_ID_STUDENT);
statement.setInt(1, id);
ArrayList arrayList = new ArrayList();
ResultSet resultSet = statement.executeQuery();
while(resultSet.next()){
arrayList.add(resultSet.getInt("id"));
arrayList.add(resultSet.getString("student_name"));
arrayList.add(resultSet.getFloat("score"));
arrayList.add(resultSet.getInt("subject_id"));
}
return arrayList;
}
public ResultSet getAllStudents() throws SQLException {
PreparedStatement preparedStatement = this.con.prepareStatement(GET_ALL_STUDENT);
ResultSet resultSet = preparedStatement.executeQuery();
return resultSet;
}
}
cd/project/challenge &&cd JDBC; mvn clean test;cat target/surefire-reports/*.TXT
ReplyDeletePlease provide solution for Course Id 64904
ReplyDeleteError" the goal u specified requires an POM file in directory"
ReplyDeleteErroo project requires an POM file
ReplyDeleteI need code for mini Project JSON and spring boot blogging app
ReplyDeleteMini Project - Java Programming Masterclass_FP(62237)
ReplyDeletecan you please provide ans for this
This code throwing error. Can anyone help me to fix this. Here is the error
ReplyDeletejava.lang.NullPointerException
at com.fresco.jdbc.code.DbOperations.insertSubject(DbOperations.java:50)
at com.fresco.jdbcTests.JdbcTest.test2(JdbcTest.java:24)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:59)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:56)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.runners.ParentRunner$3.evaluate(ParentRunner.java:306)
at org.junit.runners.BlockJUnit4ClassRunner$1.evaluate(BlockJUnit4ClassRunner.java:100)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:366)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:103)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:63)
at org.junit.runners.ParentRunner$4.run(ParentRunner.java:331)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:79)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:329)
at org.junit.runners.ParentRunner.access$100(ParentRunner.java:66)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:293)
at org.junit.runners.ParentRunner$3.evaluate(ParentRunner.java:306)
at org.junit.runners.ParentRunner.run(ParentRunner.java:413)
at org.apache.maven.surefire.junit4.JUnit4Provider.execute(JUnit4Provider.java:252)
at org.apache.maven.surefire.junit4.JUnit4Provider.executeTestSet(JUnit4Provider.java:141)
at org.apache.maven.surefire.junit4.JUnit4Provider.invoke(JUnit4Provider.java:112)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.apache.maven.surefire.util.ReflectionUtils.invokeMethodWithArray(ReflectionUtils.java:189)
at org.apache.maven.surefire.booter.ProviderFactory$ProviderProxy.invoke(ProviderFactory.java:165)
at org.apache.maven.surefire.booter.ProviderFactory.invokeProvider(ProviderFactory.java:85)
at org.apache.maven.surefire.booter.ForkedBooter.runSuitesInProcess(ForkedBooter.java:115)
at org.apache.maven.surefire.booter.ForkedBooter.main(ForkedBooter.java:75)
test3(com.fresco.jdbcTests.JdbcTest) Time elapsed: 0.014 sec <<< ERROR!
java.lang.NullPointerException
at com.fresco.jdbcTests.JdbcTest.test3(JdbcTest.java:43)
please update this solution we are getting another question having the Category ,product, order table
ReplyDeleteits not working for me it says the array is a raw type
ReplyDeletegiving Null pointer exception
ReplyDeletePlease update the course 64904 hands-on solutions
ReplyDeletePlease 62237 and 63431 answer pls
ReplyDelete