SQLBean.java
package mybeans;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;
import java.util.ArrayList;
import java.util.Arrays;
import static java.util.Arrays.asList;
import java.sql.*;
public class SQLBean implements java.io.Serializable {
private String name;
public String year; //Frosh, Soph, Jr, Senior,Year 1,Year 2,Year 3
public int testgrade; //entrance exam grade
static final String JDBC_DRIVER = "org.mariadb.jdbc.Driver";
static final String DB_URL = "jdbc:mariadb://localhost:3306/cs4010";
static final String USER = "cs4010";
static final String PASS = "cs4010";
public SQLBean() {
}
public String getName(){
return name;
}
public String getYear(){
return year;
}
public int getTestgrade(){
return testgrade;
}
public void setName(String Name){
this.name = Name;
}
public void setYear(String Year){
this.year = Year;
}
public void setTestgrade(int Testgrade){
this.testgrade = Testgrade;
}
public String getAll(){
return name+" "+year+ " " +new Integer(testgrade).toString();
}
public void setAll(String thename){
List<String> theBeanData=TheStudent(thename);
setName(theBeanData.get(0));
if (theBeanData.size()>1) {
setYear(theBeanData.get(1));
setTestgrade(Integer.parseInt(theBeanData.get(2)));
}
}
public List<String> TheStudent(String who){
Connection conn = null;
Statement stmt = null;
List<String> theReturn=new ArrayList<String>();
try {
Class.forName(JDBC_DRIVER);
conn = DriverManager.getConnection(DB_URL,USER,PASS);
stmt = conn.createStatement();
String this_query="SELECT name, year, test FROM student where name=\"";
this_query=this_query+who+"\";";
ResultSet rs = stmt.executeQuery(this_query);
while (rs.next()) {
theReturn.add(rs.getString("name"));
theReturn.add(rs.getString("year"));
theReturn.add(Integer.toString(rs.getInt("test")));
}
rs.close();
stmt.close();
conn.close();
} catch (Exception e) {
theReturn.add("Failed");
}
return theReturn;
}
}