| | |
please help me solve this! How to search in java using MYSQL database?
![]() |
•
•
Join Date: Mar 2008
Posts: 5
Reputation:
Solved Threads: 0
<code>
package packagePgmaLibrarySystemRolandgasparDizon;
import javax.swing.SwingUtilities;
import java.awt.BorderLayout;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JFrame;
import java.awt.Dimension;
import javax.swing.JLabel;
import java.awt.Rectangle;
import javax.swing.*;
import java.awt.*;
import javax.swing.JSlider;
import java.awt.GridBagLayout;
import javax.swing.border.SoftBevelBorder;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import java.awt.GridBagConstraints;
import javax.swing.JButton;
import javax.swing.BorderFactory;
import javax.swing.border.EtchedBorder;
import java.sql.*;
import java.awt.event.*;
import javax.swing.ImageIcon;
import java.awt.SystemColor;
import java.awt.Color;
import javax.swing.SwingConstants;
import javax.swing.JTextField;
import javax.swing.JComboBox;
import java.util.Date;
public class SearchStudents extends JFrame {
Connection con = null;
ResultSet rs = null;
Statement stmt = null; // @jve:decl-index=0:
ResultSetMetaData metaData = null;
FunctionForm ff = new FunctionForm();
public String[] columnName;
public Object[][] cells;
public int code;
// @jve:decl-index=0:
private static final long serialVersionUID = 1L;
private JPanel jContentPane = null;
private JLabel jLabel = null;
private JPanel jPanel = null;
private JScrollPane jScrollPane = null;
public JTable jTable = null;
private JLabel jLabel1 = null;
public JLabel jLabel2 = null;
public JLabel jLabel3 = null;
/**
* This method initializes jPanel
*
* @return javax.swing.JPanel
*/
private JPanel getJPanel() {
if (jPanel == null) {
GridBagConstraints gridBagConstraints = new GridBagConstraints();
gridBagConstraints.fill = GridBagConstraints.BOTH;
gridBagConstraints.gridy = 0;
gridBagConstraints.weightx = 1.0;
gridBagConstraints.weighty = 1.0;
gridBagConstraints.gridx = 0;
jPanel = new JPanel();
jPanel.setLayout(new GridBagLayout());
jPanel.setBounds(new Rectangle(9, 227, 993, 422));
jPanel.setBorder(new SoftBevelBorder(SoftBevelBorder.LOWERED));
jPanel.add(getJScrollPane(), gridBagConstraints);
}
return jPanel;
}
/**
* This method initializes jScrollPane
*
* @return javax.swing.JScrollPane
*/
private JScrollPane getJScrollPane() {
if (jScrollPane == null) {
jScrollPane = new JScrollPane();
jScrollPane.setViewportView(getJTable());
}
return jScrollPane;
}
/**
* This method initializes jTable
*
* @return javax.swing.JTable
*/
private JTable getJTable() {
if (jTable == null) {
getData();
if (jLabel3.getText() != ""){
jTable = new JTable(cells,columnName);
}
else
jTable = new JTable();
jTable.setBackground(SystemColor.textHighlight);
jTable.setGridColor(Color.white);
jTable.setForeground(SystemColor.activeCaptionText);
}
return jTable;
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
SwingUtilities.invokeLater(new Runnable() {
public void run() {
SearchStudents thisClass = new SearchStudents();
thisClass.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
thisClass.setVisible(true);
}
});
}
/**
* This is the default constructor
*/
public SearchStudents() {
super();
try{
Class.forName("com.mysql.jdbc.Driver");
con = DriverManager.getConnection("jdbc:mysql://localhost:3306/librarysystem","root","abc123");
}catch(Exception e){
System.out.print(e.getMessage() );
}
initialize();
}
/**
* This method initializes this
*
* @return void
*/
private void initialize() {
this.setSize(1024,700);
this.setLocationRelativeTo(null);
this.setContentPane(getJContentPane());
this.setTitle("Search Students");
this.addWindowListener(new java.awt.event.WindowAdapter() {
public void windowOpened(java.awt.event.WindowEvent e) {
getData();
}
});
}
/**
* This method initializes jContentPane
*
* @return javax.swing.JPanel
*/
private JPanel getJContentPane() {
if (jContentPane == null) {
jLabel3 = new JLabel();
jLabel3.setBounds(new Rectangle(5, 104, 54, 16));
jLabel3.setText("");
jLabel2 = new JLabel();
jLabel2.setBounds(new Rectangle(6, 78, 57, 16));
jLabel2.setText("");
jLabel2.setForeground(Color.white);
jLabel1 = new JLabel();
jLabel1.setBounds(new Rectangle(112, 37, 786, 116));
jLabel1.setIcon(new ImageIcon("C:/Documents and Settings/USER/My Documents/My Pictures/photoshop/bus_header.jpg"));
jLabel1.setHorizontalAlignment(SwingConstants.CENTER);
jLabel1.setHorizontalTextPosition(SwingConstants.CENTER);
jLabel1.setText("");
jLabel = new JLabel();
jLabel.setBounds(new Rectangle(354, 9, 199, 26));
jLabel.setText("Search Students");
jLabel.setForeground(SystemColor.activeCaptionText);
jLabel.setFont(new Font("Verdana",Font.BOLD + Font.ITALIC,17));
jContentPane = new JPanel();
jContentPane.setLayout(null);
jContentPane.setBackground(SystemColor.controlDkShadow);
jContentPane.add(jLabel, null);
jContentPane.add(getJPanel(), null);
jContentPane.add(jLabel1, null);
jContentPane.add(jLabel2, null);
jContentPane.add(jLabel3, null);
}
return jContentPane;
}
public void getData() {
try{
searchForm sf = new searchForm();
stmt = con.createStatement();
rs = stmt.executeQuery("Select * from register where "+jLabel2.getText()+" = '"+jLabel3.getText()+"' ");
metaData = rs.getMetaData();
int columnCount = metaData.getColumnCount();
columnName = new String[columnCount-1];
int c = 1;
for(int i=0;i<(columnCount-1);i++){
columnName[i] = metaData.getColumnName(c);
c++;
}
int index = 0;
rs.last();
int number = rs.getRow();
rs.beforeFirst();
cells = new Object[number][metaData.getColumnCount()];
while(rs.next()){
cells[index][0] = rs.getString(1);
cells[index][1] = rs.getString(2);
cells[index][2] = rs.getString(3);
cells[index][3] = rs.getString(4);
cells[index][4] = rs.getString(5);
cells[index][5] = rs.getString(6);
cells[index][6] = rs.getString(7);
cells[index][7] = rs.getString(8);
index++;
}
}catch(SQLException sqle){
System.out.print(sqle.getMessage() );
}
}
} // @jve:decl-index=0:visual-constraint="10,10"
please see attachment=>>>
</code>
package packagePgmaLibrarySystemRolandgasparDizon;
import javax.swing.SwingUtilities;
import java.awt.BorderLayout;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JFrame;
import java.awt.Dimension;
import javax.swing.JLabel;
import java.awt.Rectangle;
import javax.swing.*;
import java.awt.*;
import javax.swing.JSlider;
import java.awt.GridBagLayout;
import javax.swing.border.SoftBevelBorder;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import java.awt.GridBagConstraints;
import javax.swing.JButton;
import javax.swing.BorderFactory;
import javax.swing.border.EtchedBorder;
import java.sql.*;
import java.awt.event.*;
import javax.swing.ImageIcon;
import java.awt.SystemColor;
import java.awt.Color;
import javax.swing.SwingConstants;
import javax.swing.JTextField;
import javax.swing.JComboBox;
import java.util.Date;
public class SearchStudents extends JFrame {
Connection con = null;
ResultSet rs = null;
Statement stmt = null; // @jve:decl-index=0:
ResultSetMetaData metaData = null;
FunctionForm ff = new FunctionForm();
public String[] columnName;
public Object[][] cells;
public int code;
// @jve:decl-index=0:
private static final long serialVersionUID = 1L;
private JPanel jContentPane = null;
private JLabel jLabel = null;
private JPanel jPanel = null;
private JScrollPane jScrollPane = null;
public JTable jTable = null;
private JLabel jLabel1 = null;
public JLabel jLabel2 = null;
public JLabel jLabel3 = null;
/**
* This method initializes jPanel
*
* @return javax.swing.JPanel
*/
private JPanel getJPanel() {
if (jPanel == null) {
GridBagConstraints gridBagConstraints = new GridBagConstraints();
gridBagConstraints.fill = GridBagConstraints.BOTH;
gridBagConstraints.gridy = 0;
gridBagConstraints.weightx = 1.0;
gridBagConstraints.weighty = 1.0;
gridBagConstraints.gridx = 0;
jPanel = new JPanel();
jPanel.setLayout(new GridBagLayout());
jPanel.setBounds(new Rectangle(9, 227, 993, 422));
jPanel.setBorder(new SoftBevelBorder(SoftBevelBorder.LOWERED));
jPanel.add(getJScrollPane(), gridBagConstraints);
}
return jPanel;
}
/**
* This method initializes jScrollPane
*
* @return javax.swing.JScrollPane
*/
private JScrollPane getJScrollPane() {
if (jScrollPane == null) {
jScrollPane = new JScrollPane();
jScrollPane.setViewportView(getJTable());
}
return jScrollPane;
}
/**
* This method initializes jTable
*
* @return javax.swing.JTable
*/
private JTable getJTable() {
if (jTable == null) {
getData();
if (jLabel3.getText() != ""){
jTable = new JTable(cells,columnName);
}
else
jTable = new JTable();
jTable.setBackground(SystemColor.textHighlight);
jTable.setGridColor(Color.white);
jTable.setForeground(SystemColor.activeCaptionText);
}
return jTable;
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
SwingUtilities.invokeLater(new Runnable() {
public void run() {
SearchStudents thisClass = new SearchStudents();
thisClass.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
thisClass.setVisible(true);
}
});
}
/**
* This is the default constructor
*/
public SearchStudents() {
super();
try{
Class.forName("com.mysql.jdbc.Driver");
con = DriverManager.getConnection("jdbc:mysql://localhost:3306/librarysystem","root","abc123");
}catch(Exception e){
System.out.print(e.getMessage() );
}
initialize();
}
/**
* This method initializes this
*
* @return void
*/
private void initialize() {
this.setSize(1024,700);
this.setLocationRelativeTo(null);
this.setContentPane(getJContentPane());
this.setTitle("Search Students");
this.addWindowListener(new java.awt.event.WindowAdapter() {
public void windowOpened(java.awt.event.WindowEvent e) {
getData();
}
});
}
/**
* This method initializes jContentPane
*
* @return javax.swing.JPanel
*/
private JPanel getJContentPane() {
if (jContentPane == null) {
jLabel3 = new JLabel();
jLabel3.setBounds(new Rectangle(5, 104, 54, 16));
jLabel3.setText("");
jLabel2 = new JLabel();
jLabel2.setBounds(new Rectangle(6, 78, 57, 16));
jLabel2.setText("");
jLabel2.setForeground(Color.white);
jLabel1 = new JLabel();
jLabel1.setBounds(new Rectangle(112, 37, 786, 116));
jLabel1.setIcon(new ImageIcon("C:/Documents and Settings/USER/My Documents/My Pictures/photoshop/bus_header.jpg"));
jLabel1.setHorizontalAlignment(SwingConstants.CENTER);
jLabel1.setHorizontalTextPosition(SwingConstants.CENTER);
jLabel1.setText("");
jLabel = new JLabel();
jLabel.setBounds(new Rectangle(354, 9, 199, 26));
jLabel.setText("Search Students");
jLabel.setForeground(SystemColor.activeCaptionText);
jLabel.setFont(new Font("Verdana",Font.BOLD + Font.ITALIC,17));
jContentPane = new JPanel();
jContentPane.setLayout(null);
jContentPane.setBackground(SystemColor.controlDkShadow);
jContentPane.add(jLabel, null);
jContentPane.add(getJPanel(), null);
jContentPane.add(jLabel1, null);
jContentPane.add(jLabel2, null);
jContentPane.add(jLabel3, null);
}
return jContentPane;
}
public void getData() {
try{
searchForm sf = new searchForm();
stmt = con.createStatement();
rs = stmt.executeQuery("Select * from register where "+jLabel2.getText()+" = '"+jLabel3.getText()+"' ");
metaData = rs.getMetaData();
int columnCount = metaData.getColumnCount();
columnName = new String[columnCount-1];
int c = 1;
for(int i=0;i<(columnCount-1);i++){
columnName[i] = metaData.getColumnName(c);
c++;
}
int index = 0;
rs.last();
int number = rs.getRow();
rs.beforeFirst();
cells = new Object[number][metaData.getColumnCount()];
while(rs.next()){
cells[index][0] = rs.getString(1);
cells[index][1] = rs.getString(2);
cells[index][2] = rs.getString(3);
cells[index][3] = rs.getString(4);
cells[index][4] = rs.getString(5);
cells[index][5] = rs.getString(6);
cells[index][6] = rs.getString(7);
cells[index][7] = rs.getString(8);
index++;
}
}catch(SQLException sqle){
System.out.print(sqle.getMessage() );
}
}
} // @jve:decl-index=0:visual-constraint="10,10"
please see attachment=>>>
</code>
Well, what's wrong with this post. Let's count the ways:
1. You use a bad title (you can use Java to search a MySQL DB, but you can't use a MySQL DB to search Java).
2. You dump a bunch of unformatted into the post (because of a very poor attempt at using code tags).
3. You provide no description, what-so-ever of what your actual problem is.
4. You say "please see attachment" with some funky symbols after it, but didn't attach anything.
5. And I haven't even started to look at the code, either (and won't).
1. You use a bad title (you can use Java to search a MySQL DB, but you can't use a MySQL DB to search Java).
2. You dump a bunch of unformatted into the post (because of a very poor attempt at using code tags).
3. You provide no description, what-so-ever of what your actual problem is.
4. You say "please see attachment" with some funky symbols after it, but didn't attach anything.
5. And I haven't even started to look at the code, either (and won't).
Java Programmer and Sun Systems Administrator
----------------------------------------------
Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.
--Brian Kernighan
----------------------------------------------
Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.
--Brian Kernighan
![]() |
Similar Threads
Other Threads in the Java Forum
- Previous Thread: Problem using exceptions and looping
- Next Thread: help with diagnosis (if i'm right???
| Thread Tools | Search this Thread |
911 addball addressbook android api append applet application apps array arrays automation binary bluetooth businessintelligence button card chat class client code collision component crashcourse css csv database eclipse ee error fractal free game gis givemetehcodez graphics gui html ide image integer integration j2me japplet java javaarraylist javadoc javafx javaprojects jni jpanel julia jvm linux list loan machine map method methods migrate mobile netbeans newbie objects oriented output panel phone physics problem program programming project projects radio recursion replaydirector reporting scanner se server service set sms socket software sort sql string swing test textfield threads transfer tree trolltech ubuntu utility windows






