Question

In: Computer Science

android studio -Starting with a basic activity, create a new Java class (use File->New->Java class) called...

android studio

-Starting with a basic activity, create a new Java class (use File->New->Java class) called DataBaseManager as in Lecture 5 and create a database table in SQLite, called StudentInfo. The fields for the StudentInfo table include StudentID, FirstName, LastName, YearOfBirth and Gender. Include functions for adding a row to the table and for retrieving all rows, similar to that shown in lecture 5. No user interface is required for this question, t

-Continuing from , follow the example in the lecture notes 5, create a menu (or action bar items) for “Add A Record” and “View Records”. When the menu item “Add A Record” is selected, the program shows a form for entering student information. The data will be added into the database table StudentInfo. When the menu item “View Records” is selected, the program shows all records of all students.

Solutions

Expert Solution

The following program will all the data to the database and also shows all the student records

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.sql.DriverManager;
import java.sql.SQLException;

public class DatabaseManager {

//connection
private Connection connect() {
  
String url = "url of database";
Connection con = null;
try {
conn = DriverManager.getConnection(url);
} catch (SQLException e) {
System.out.println(e.getMessage());
}
return con;
}

public void selectAll(){
String sql = "SELECT StudentID, FirstName, LastName, YearOfBirth , Gender FROM StudentInfo";
  
try (Connection con = this.connect();
Statement stmt = con.createStatement();
ResultSet rset = stmt.executeQuery(sql)){
  
while (rset.next()) {
System.out.println(rset.getInt("StudentID") + "\t" +
rset.getString("FirstName") + "\t" +
rset.getString("LastName")+"\t"+
rset.getString("YearOfBirth")+"\t"+
rset.getString("Gender"));
}
} catch (SQLException e) {
System.out.println(e.getMessage());
}

public void insert(int StudentID,String FirstName,String LastName,String YearOfBirth,String Gender ) {
String sql = "INSERT INTO StudentInfo(StudentID,FirstName,LastName,YearOfBirth,Gender) VALUES(?,?,?,?,?)";

try (Connection con = this.connect();
PreparedStatement pstmt = con.prepareStatement(sql)) {
pstmt.setInt(1, StudentID);
pstmt.setString(2, FirstName);
pstmt.setString(3, LastName);
pstmt.setString(4, YearOfBirth);
pstmt.setString(5, Gender);
pstmt.executeUpdate();
} catch (SQLException e) {
System.out.println(e.getMessage());
}
}
}
public static void main(String[] args) {
DatabaseManager db = new DatabaseManager();
  
//function call to view data from database
db.selectAll();

//funtion call to insert data to database
db.insert("StudentID","FirstName","LastName","YearOfBirth","Gender")
}

}


Related Solutions

android studio Begin a new app and create a Java class for products with data: productName,...
android studio Begin a new app and create a Java class for products with data: productName, productCode and price and use a file of objects to store product data and read back and display on screen. Do this by creating a simple form using EditTexts, buttons or ActionBar items and display output using an Alert.
Android Studio. Java. Please create an application that -> An activity that allows user to enter...
Android Studio. Java. Please create an application that -> An activity that allows user to enter name, gender, date of birth, state of residence (selected from a pre-defined list: CA, AZ, NV, OR), email address and favorite website. This activity has a button "Show Data" that displays detail entered
Create a Java class file for a Car class. In the File menu select New File......
Create a Java class file for a Car class. In the File menu select New File... Under Categories: make sure that Java is selected. Under File Types: make sure that Java Class is selected. Click Next. For Class Name: type Car. For Package: select csci2011.lab7. Click Finish. A text editor window should pop up with the following source code (except with your actual name): csci1011.lab7; /** * * @author Your Name */ public class Car { } Implement the Car...
Create a Scorekeeper app in android studio by using java language - Layouts | Widgets Create...
Create a Scorekeeper app in android studio by using java language - Layouts | Widgets Create the layout for your score keeping app. The app should have: Two team names (TextViews) Two scores (TextViews) Buttons to increase/ decrease the scores An amount to change the score by (RadioButtons) You must have at least two score options The scores can be changed by anything you want American football: 1, 2, 3, 6 Basketball: 1, 2, 3 Freestyle wrestling: 1, 2, 3,...
Android Studio Code: Provide a working android studio code i.e java and xml code for the...
Android Studio Code: Provide a working android studio code i.e java and xml code for the activity below: Develop an application that is capable to turn pages back and forth. Detailed Instructions: For the main activity, create a layout that represents a fictitious title and author. The activity should include a clickable button on the bottom right that allows you to go forward to the next activity. The next activity will simply be an image above simple text that can...
Create a Java project called Lab3B and a class named Lab3B. Create a second new class...
Create a Java project called Lab3B and a class named Lab3B. Create a second new class named Book. In the Book class: Add the following private instance variables: title (String) author (String) rating (int) Add a constructor that receives 3 parameters (one for each instance variable) and sets each instance variable equal to the corresponding variable. Add a second constructor that receives only 2 String parameters, inTitle and inAuthor. This constructor should only assign input parameter values to title and...
Create a Java project called Lab3A and a class named Lab3A. Create a second new class...
Create a Java project called Lab3A and a class named Lab3A. Create a second new class named Employee. In the Employee class: Add the following private instance variables: name (String) job (String) salary (double) Add a constructor that receives 3 parameters (one for each instance variable) and sets each instance variable equal to the corresponding variable. (Refer to the Tutorial3 program constructor if needed to remember how to do this.) Add a public String method named getName (no parameter) that...
Create a Java project called 5 and a class named 5 Create a second new class...
Create a Java project called 5 and a class named 5 Create a second new class named CoinFlipper Add 2 int instance variables named headsCount and tailsCount Add a constructor with no parameters that sets both instance variables to 0; Add a public int method named flipCoin (no parameters). It should generate a random number between 0 & 1 and return that number. (Important note: put the Random randomNumbers = new Random(); statement before all the methods, just under the...
Create a new Java project called lab1 and a class named Lab1 Create a second class...
Create a new Java project called lab1 and a class named Lab1 Create a second class called VolumeCalculator. Add a static field named PI which = 1415 Add the following static methods: double static method named sphere that receives 1 double parameter (radius) and returns the volume of a sphere. double static method named cylinder that receives 2 double parameters (radius & height) and returns the volume of a cylinder. double static method named cube that receives 1 double parameter...
JAVA A simple Class in a file called Account.java is given below. Create two Derived Classes...
JAVA A simple Class in a file called Account.java is given below. Create two Derived Classes Savings and Checking within their respective .java files. (modify display() as needed ) 1. Add New private String name (customer name) for both, add a New int taxID for Savings only. 2. Add equals() METHOD TO CHECK any 2 accounts Demonstrate with AccountDemo.java in which you do the following: 3. Create 1 Savings Account and 3 Checking Accounts, where 2 checkings are the same....
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT