public class BazaProcesor {
protected Connection _connection = null;
protected String _driver = null;
protected String _dbUrl = null;
public BazaProcesor(String driver, String dbUrl)
{
this._driver = driver;
this._dbUrl = dbUrl;
}
public boolean openConnection(String user, String pass)
{
try
{
Class.forName(_driver);
_connection = DriverManager.getConnection(_dbUrl, user, pass);
//_connection.setAutoCommit(false);
}
catch(Exception e)
{
System.out.println(e.getMessage());
_connection = null;
return false;
}
return true;
}
public void closeConnection()
{
if(null != _connection)
{
try
{
_connection.close();
}
catch(SQLException e)
{
System.out.println(e.getMessage());
}
finally
{
_connection = null;
}
}
}
//Metoda pobierajaca liste testow z bazy
public Test[] getTesty()
{
String sql = "SELECT testy.id_rek AS id, testy.nazwa AS nazwa, st_trudnosci.st_trudnosci AS trudnosc, testy.czas AS czas, testy.autor AS autor "
+ "FROM testy, st_trudnosc WHERE testy.st_trudnosci = st_trudnosci.id_rek;";
Vector v_testy = new Vector(0, 1);
Test[] testy = null;
try
{
wierz 77
Statement statement =_connection.createStatement(java.sql.ResultSet.TYPE_SCROLL_INSENSITIVE, java.sql.ResultSet.CONCUR_READ_ONLY);
ResultSet rs = statement.executeQuery(sql);
rs.beforeFirst();
while(rs.next())
{
Test test = new Test(rs.getInt("id"), rs.getString("nazwa"), rs.getString("trudnosc"), rs.getString("czas"), rs.getString("autor"));
v_testy.addElement(test);
}
testy = new Test[v_testy.size()];
v_testy.copyInto(testy);
return testy;
}
catch(SQLException e)
{
System.out.println(e.getMessage());
return null;
}
}
public Pytanie[] getPytania(int id_test)
{
String sql = "SELECT id_rek, pytanie, autor FROM pytania WHERE test = ?;";
Vector v_pytania = new Vector(0, 1);
Pytanie[] pytania = null;
try
{
PreparedStatement pstm = _connection.prepareStatement(sql, java.sql.ResultSet.TYPE_SCROLL_INSENSITIVE, java.sql.ResultSet.CONCUR_READ_ONLY);
pstm.setInt(1, id_test);
ResultSet rs = pstm.executeQuery(sql);
rs.beforeFirst();
while(rs.next())
{
Pytanie p = new Pytanie(rs.getInt(1), rs.getString(2), rs.getString(3));
v_pytania.addElement(p);
}
pytania = new Pytanie[v_pytania.size()];
v_pytania.copyInto(pytania);
return pytania;
}
catch(SQLException e)
{
System.out.println(e.getMessage());
return null;
}
}
public Wynik[] getWyniki()
{
String sql = "SELECT wyniki.id_rek, testy.nazwa, wyniki.user, wyniki.data, wyniki.wynik FROM wyniki, testy WHERE wyniki.test = testy.id_rek;";
Vector v_wyniki = new Vector(0,1);
Wynik[] wyniki = null;
try
{
Statement stm = _connection.createStatement(java.sql.ResultSet.TYPE_SCROLL_INSENSITIVE, java.sql.ResultSet.CONCUR_READ_ONLY);
ResultSet rs = stm.executeQuery(sql);
rs.beforeFirst();
while(rs.next())
{
Wynik w = new Wynik(rs.getInt(1), rs.getString(2), rs.getString(3), rs.getString(4), rs.getShort(5));
v_wyniki.addElement(w);
}
wyniki = new Wynik[v_wyniki.size()];
v_wyniki.copyInto(wyniki);
return wyniki;
}
catch(SQLException e)
{
System.out.println(e.getMessage());
return null;
}
}
}
public class GlowneOkno extends JFrame
{
BazaProcesor bp;
// private Component c;
private JPanel jpPytanie, jpTest;
private JLabel jlPytanie;
private JButton jbNext, jbPrev, jbNewTest;
private JComboBox cbTesty;
public GlowneOkno()
{
wiersz 31
dbInit();
stworzOkno();
}
private void stworzOkno()
{
// c = new Container();
jpPytanie = new JPanel(new RiverLayout());
jpTest = new JPanel(new RiverLayout());
jlPytanie = new JLabel();
jbNext = new JButton("Next Question");
jbPrev = new JButton("Previuos Question");
jbNewTest = new JButton("New Test");
cbTesty = new JComboBox();
getContentPane();
setLayout(new BorderLayout());
setDefaultCloseOperation(EXIT_ON_CLOSE);
setTitle("Test");
setSize(800, 600);
setResizable(false);
// jpPytanie.setPreferredSize(new Dimension(800,600));
// jpPytanie.setBackground(Color.blue);
jpTest.add("center", jbNewTest);
jpTest.add("br left", cbTesty);
jpPytanie.add("center", jlPytanie);
jpPytanie.add("br center", jbPrev);
jpPytanie.add("center", jbNext);
add(jpPytanie, BorderLayout.CENTER);
add(jpTest, BorderLayout.WEST);
//pack();
jbNext.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
jlPytanie.setText("Next");
}
});
jbNewTest.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
}
});
jbPrev.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
jlPytanie.setText("Previuos");
}
});
}
public void dbInit()
{
bp = new BazaProcesor("com.mysql.jdbc.Driver", "jdbc:mysql://localhost:3306/test1");
bp.openConnection("root", "somalia");
wiersz 104
Test[] testy = bp.getTesty();
for(int i=0; i<testy.length; i++)
{
cbTesty.addItem(testy[i]);
}
}
public static void main(String[] args)
{
java.awt.EventQueue.invokeLater(new Runnable() {
public void run(){
wierz 126
new GlowneOkno().setVisible(true);
}
});
}
}
Sorry, że tak spamuje ale ostatnio jestem kompletnie nie kumaty, proszę adminów o usunięcie tego syfu z goscia.
Jeszcze raz zwracam się z prośbą o pomoc w rozwiązaniu tego błędu.