For my tool I need to locate all of my database list on once JFrame and I succeeded to handle them individually.
And I want to reduce the code and used for loop replacing duplicate code for all databases required.
the code is below. Please check and advise why I am getting the mentioned error.
import javax.swing.*;
import java.sql.*;
import java.awt.event.*;
import java.awt.*;
import javax.swing.border.*;
import java.io.*;
public class OurTool extends JFrame implements MouseListener
{
public static void main(String args[])
{
OurTool f=new OurTool();
f.setSize(2000,1000);
f.setResizable(false);
f.setVisible(true);
f.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent w)
{
System.exit(0);
}
});
}
JButton[] dbBut= new JButton[2];
String dbList[] = {"DB1","DB2"};
int dbIndex,butTop;
public OurTool()
{
Container Con = getContentPane();
Con.setLayout(null);
for (dbIndex=1,butTop=10;dbIndex<=2;dbIndex++,butTop+= 105)
{
dbBut[dbIndex]= new JButton(dbList[dbIndex]);
Con.add(dbBut[dbIndex]);
dbBut[dbIndex].setSize(200,100);
dbBut[dbIndex].setBounds(5,butTop,200,100);
dbBut[dbIndex].addMouseListener(this);
}
}
public void mousePressed(MouseEvent ME)
{
}
public void mouseClicked(MouseEvent ME)
{
JButton B1=(JButton) ME.getSource();
for (dbIndex=1;dbIndex<=2;dbIndex++)
{
if (B1==(JButton) dbBut[dbIndex])
{
try
{
connectToMyDB(dbList[dbIndex]);
}catch(SQLException e){}
catch(ClassNotFoundException e){}
}
}
}
public void mouseReleased(MouseEvent ME)
{
}
public void mouseExited(MouseEvent ME)
{
}
public void mouseEntered(MouseEvent ME)
{
}
public void connectToMyDB(String mydb) throws ClassNotFoundException,SQLException
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection cn=DriverManager.getConnection("jdbcdbc:mydb","user1","pwd1");
System.out.println("connected to database..");
Statement st=cn.createStatement();
ResultSet rs=st.executeQuery("select * from tab1;");
while(rs.next())
{
System.out.println(rs.getString("Time")+" "+rs.getString("Message"));
}
rs.close();
st.close();
cn.close();
}
}
--- Update ---
I changed JButton declaration and am out of ArrayIndexOutOfBoundException now..and got "NullPointerException" now.
new code is:
import javax.swing.*;
import java.sql.*;
import java.awt.event.*;
import java.awt.*;
import javax.swing.border.*;
import java.io.*;
public class OurTool extends JFrame implements MouseListener
{
public static void main(String args[])
{
OurTool f=new OurTool();
f.setSize(2000,1000);
f.setResizable(false);
f.setVisible(true);
f.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent w)
{
System.exit(0);
}
});
}
JButton dbBut[];
String dbList[] = {"DB1","DB2"};
int dbIndex,butTop;
public OurTool()
{
Container Con = getContentPane();
Con.setLayout(null);
for (dbIndex=1,butTop=10;dbIndex<=2;dbIndex++,butTop+= 105)
{
dbBut[dbIndex]= new JButton(dbList[dbIndex]);
Con.add(dbBut[dbIndex]);
dbBut[dbIndex].setSize(200,100);
dbBut[dbIndex].setBounds(5,butTop,200,100);
dbBut[dbIndex].addMouseListener(this);
}
}
public void mousePressed(MouseEvent ME)
{
}
public void mouseClicked(MouseEvent ME)
{
JButton B1=(JButton) ME.getSource();
for (dbIndex=1;dbIndex<=2;dbIndex++)
{
if (B1==(JButton) dbBut[dbIndex])
{
try
{
connectToMyDB(dbList[dbIndex]);
}catch(SQLException e){}
catch(ClassNotFoundException e){}
}
}
}
public void mouseReleased(MouseEvent ME)
{
}
public void mouseExited(MouseEvent ME)
{
}
public void mouseEntered(MouseEvent ME)
{
}
public void connectToMyDB(String recDB) throws ClassNotFoundException,SQLException
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection cn=DriverManager.getConnection("jdbcdbc:mydb","user1","pwd1");
System.out.println("connected to database..");
Statement st=cn.createStatement();
ResultSet rs=st.executeQuery("select * from tab1;");
while(rs.next())
{
System.out.println(rs.getString("Time")+" "+rs.getString("Message"));
}
rs.close();
st.close();
cn.close();
}
}