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...
Start NetBeans. Create a new project called Lab7. Create a Java main class file using the...
Start NetBeans. Create a new project called Lab7. Create a Java main class file using the class name YourlastnameLab7 with your actual last name. Create a Java class file for a Polygon class. Implement the Polygon class. Add a private instance variable representing the number of sides in the polygon. Add a constructor that takes a single argument and uses it to initialize the number of sides. If the value of the argument is less than three, display an error...
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...
Using Android Studio, create a one java android app that has 4 buttons:- Change Color Button...
Using Android Studio, create a one java android app that has 4 buttons:- Change Color Button - When the Change Color button is clicked, it changes the current activity background to a randomly selected color Speak Button - When the Speak button is clicked, it opens a new activity named SpeakActivity. On the SpeakActivity there are three controls: EditText, Button (Speak) and Button (Back). The Speak button uses the Text to Speech service to say the text entered in EditText....
Create and display an XML file in Android studio that has -One edittext box the does...
Create and display an XML file in Android studio that has -One edittext box the does NOT use material design -One edittext box that DOES use material design Provide a “hint” for each, and make sure the string that comprises the hint is referenced from the strings.xml file. Observe the differences in how the hints are displayed. Here is sample code for a material design editText: <android.support.design.widget.TextInputLayout         android:id="@+id/input_layout_price1Text"         android:layout_width="80dp"         android:layout_height="40dp"            >     <EditText             android:id="@+id/price1Text"             android:importantForAutofill="no"...
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...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT