Hello!
i need to filter the searched data in the database sql and display it on the table. But i keep on getting tis error:
java.lang.StringIndexOutOfBoundsException: String index out of range: 1
at java.lang.String.charAt(Unknown Source)
at app.SearchByKey.SearchStudentNumber.SearchStudentRecordReader(SearchStudentNumber.java:38)
at app.ui.ListOfStudents.updateSearchTable(ListOfStudents.java:159)
at app.ui.ListOfStudents$3.keyReleased(ListOfStudents.java:105)
at java.awt.Component.processKeyEvent(Unknown Source)
at javax.swing.JComponent.processKeyEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.KeyboardFocusManager.redispatchEvent(Unknown Source)
at java.awt.DefaultKeyboardFocusManager.dispatchKeyEvent(Unknown Source)
at java.awt.DefaultKeyboardFocusManager.preDispatchKeyEvent(Unknown Source)
at java.awt.DefaultKeyboardFocusManager.typeAheadAssertions(Unknown Source)
at java.awt.DefaultKeyboardFocusManager.dispatchEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$200(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
I think its because i printed that instance of myStudent and it prints null,
but i can't find what should i change to make it right.
this is my code.
package app.ui;
import java.awt.Color;
import java.awt.EventQueue;
import java.awt.FlowLayout;
import java.util.List;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JPanel;
import javax.swing.table.DefaultTableModel;
import javax.swing.border.CompoundBorder;
import app.SearchByKey.SearchStudentNumber;
import app.model.Student;
import app.search.student.list.SearchStudent;
import app.util.DataFileReader;
import javax.swing.JTextField;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
public class ListOfStudents {
private JFrame frame;
protected JTable tblListOfStudents;
private JTextField txtStudNo;
String letter = "";
String searchNa ="";
protected List<Student> search = null;
/**
* Launch the application.
*/
public void ListOfStudents() {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
ListOfStudents window = new ListOfStudents();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public ListOfStudents() {
initialize();
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
frame = new JFrame();
frame.setBounds(100, 100, 800, 452);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);
JLabel lblListStudents = new JLabel("List of Students");
lblListStudents.setBounds(46, 38, 136, 14);
frame.getContentPane().add(lblListStudents);
JScrollPane scrpListOfStudents = new JScrollPane();
scrpListOfStudents.setBounds(46, 89, 433, 255);
frame.getContentPane().add(scrpListOfStudents);
tblListOfStudents = new JTable();
tblListOfStudents.setModel(new DefaultTableModel(
new Object[][] {
},
new String[] {
"Student Number", "Name", "Course", "Year"
}
) {
boolean[] columnEditables = new boolean[] {
false, false, false, false
};
public boolean isCellEditable(int row, int column) {
return columnEditables[column];
}
});
scrpListOfStudents.setViewportView(tblListOfStudents);
JLabel lblSearchS = new JLabel("Search:");
lblSearchS.setBounds(491, 38, 63, 14);
frame.getContentPane().add(lblSearchS);
txtStudNo = new JTextField();
txtStudNo.addKeyListener(new KeyAdapter() {
@Override
public void keyReleased(KeyEvent arg0) {
//System.out.println("unaMeron: "+ myStudent.getStudentName());
updateSearchTable();
}
});
txtStudNo.setBounds(564, 35, 117, 20);
frame.getContentPane().add(txtStudNo);
txtStudNo.setColumns(10);
updateStudentListTable();
}
public void updateStudentListTable(){
DefaultTableModel dtm = (DefaultTableModel) tblListOfStudents.getModel();
dtm.getDataVector().removeAllElements();
tblListOfStudents.updateUI();
DataFileReader read = new DataFileReader();
List<Student> studentList = read.StudentRecordReader();
for (Student myStudent : studentList) {
String[] rowData = new String[4];
rowData[0] =myStudent.getStudentNumber();
rowData[1] =myStudent.getStudentName();
rowData[2] =myStudent.getCourse();
rowData[3] = myStudent.getSYear();
dtm.addRow(rowData);
}
tblListOfStudents.setBackground(Color.green);
tblListOfStudents.updateUI();
}
/* public Student setSearch(){
Student myStudent = new Student();
letter = txtStudNo.getText();
myStudent.setSearchNo(letter);
return myStudent;
}*/
public void updateSearchTable(){
DefaultTableModel dtm = (DefaultTableModel) tblListOfStudents.getModel();
dtm.getDataVector().removeAllElements();
tblListOfStudents.updateUI();
Student myStudent = new Student();
letter = txtStudNo.getText();
myStudent.setSearchNo(letter);
SearchStudentNumber mySearchStudent = new SearchStudentNumber();
List<Student> studentList =mySearchStudent.SearchStudentRecordReader(myStudent);
for (Student meStudent : studentList) {
String[] rowData = new String[4];
rowData[0] =meStudent.getStudentNumber();
rowData[1] =meStudent.getStudentName();
rowData[2] =meStudent.getCourse();
rowData[3] = meStudent.getSYear();
dtm.addRow(rowData);
}
tblListOfStudents.setBackground(Color.green);
tblListOfStudents.updateUI();
}
}
this is the second class
this should set the List that is contained by the table:
package app.SearchByKey;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JOptionPane;
import app.model.Student;
public class SearchStudentNumber {
public List<Student> SearchStudentRecordReader(Student y) {
String url = "jdbc:mysql://localhost:3306/";
String dbName = "Student";
String driver = "com.mysql.jdbc.Driver";
String userName = "root";
String password = "1234";
List<Student> studentList = new ArrayList<Student>();
try {
Class.forName(driver).newInstance();
Connection conn = DriverManager.getConnection(url+dbName,userName,password);
Statement st = conn.createStatement();
ResultSet re = st.executeQuery("SELECT studentNo FROM bio");
String mayaman = " ";
String bringMe= " ";
while (re.next()){
Student myStudent = new Student();
myStudent.setStudentNumber(re.getString("studentNo"));
mayaman = myStudent.getStudentNumber();
bringMe+=mayaman;
System.out.println("sana: "+ myStudent.getStudentName());
}
for(int count= 0 ; count <=y.getSearchNo().length(); count++){
String fake=Character.toString(y.getSearchNo().charAt(count));
for(int count_sec = 1; count_sec<=bringMe.length() ; count_sec++){
String orig =Character.toString(bringMe.charAt(count_sec));
System.out.println("entered: "+Character.toString(y.getSearchNo().charAt(count)));
System.out.println("laman: "+Character.toString(bringMe.charAt(count_sec)));
if(Character.toString(y.getSearchNo().charAt(count)).equals(Character.toString(bringMe.charAt(count_sec)))){
System.out.println("Im IN!");
ResultSet res = st.executeQuery("SELECT * FROM bio");
while (res.next()) {
Student myStudent = new Student();
myStudent.setStudentNumber(res.getString("studentNo"));
myStudent.setStudentName(res.getString("studentName"));
System.out.println("Name: "+myStudent.getStudentName());
myStudent.setCourse(res.getString("course"));
myStudent.setSYear(res.getString("syear"));
studentList.add(myStudent);
}
}else{
JOptionPane.showMessageDialog(null,"Sorry, there is no data to display!");
}
conn.close();
break;
}
}
}catch (Exception e) {
e.printStackTrace();
JOptionPane.showMessageDialog(null,"Cannot Connect to Database! Contact Ate Con, Rosselle , and Lonilie NOW! as in RIGHT NOW! ahihih!!");
}
return studentList;
}
}
thanks, hope you can help me!