vec godinama prograniram u VB-u i nedavno sam uzeo uciti pomalo Javu.
E sada generalno imam problem sa nacinom razmisljanja u Java-i.
Malo po malo privikavam se i hvatam pomalo konce...
Programiram u Netbeans-ima
I imam 2 klase:
1) Klasa koja sama za sebe savrseno radi u konzoli kada se pozove sa iz cmd-a
evo koda
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package jdbcrowsettest;
// Fig. 25.29: JdbcRowSetTest.java
// Displaying the contents of the authors table using JdbcRowSet.
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import javax.sql.rowset.JdbcRowSet;
import com.sun.rowset.JdbcRowSetImpl; // Sun's JdbcRowSet implementation
public class JdbcRowSetTest
{
// JDBC driver name and database URL
static final String DRIVER = "com.mysql.jdbc.Driver";
static final String DATABASE_URL = "jdbc:mysql://localhost/books";
static final String USERNAME = "user1";
static final String PASSWORD = "";
// constructor connects to database, queries database, processes
// results and displays results in window
public JdbcRowSetTest()
{
// connect to database books and query database
try
{
Class.forName( DRIVER );
// specify properties of JdbcRowSet
JdbcRowSet rowSet = new JdbcRowSetImpl();
rowSet.setUrl( DATABASE_URL ); // set database URL
rowSet.setUsername( USERNAME ); // set username
rowSet.setPassword( PASSWORD ); // set password
rowSet.setCommand( "SELECT * FROM authors" ); // set query
rowSet.execute(); // execute query
// process query results
ResultSetMetaData metaData = rowSet.getMetaData();
int numberOfColumns = metaData.getColumnCount();
System.out.println( "Authors Table of Books Database:\n" );
// display rowset header
for ( int i = 1; i <= numberOfColumns; i++ )
System.out.printf( "%-8s\t", metaData.getColumnName( i ) );
System.out.println();
// display each row
while( rowSet.next())
{
for ( int i = 1; i <= numberOfColumns; i++ )
System.out.printf( "%-8s\t",rowSet.getObject( i ) );
System.out.println();
} // end while
// close the underlying ResultSet, Statement and Connection
rowSet.close();
} // end try
catch ( SQLException sqlException )
{
sqlException.printStackTrace();
System.exit( 1 );
} // end catch
catch ( ClassNotFoundException classNotFound )
{
classNotFound.printStackTrace();
System.exit( 1 );
} // end catch
} // end DisplayAuthors constructor
}
// launch the application
//public static void main( String args[] )
//{
//JdbcRowSetTest application = new JdbcRowSetTest();
//} // end main
//} // end class JdbcRowSetTest
2) Druga klasa je nastala iz VB logike koju cesto koristim graficki objekt --> modul --> procedura
Zelja mi je da klikom na JButton pokrenem klasu 1 i to ne ide
Evo koda
public class frmMain extends javax.swing.JFrame {
/** Creates new form frmMain */
public frmMain() {
initComponents();
}
/** This method is called from within the constructor to
* initialize the form.
* WARNING: Do NOT modify this code. The content of this method is
* always regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
jButton1 = new javax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jButton1.setText("jButton1");
jButton1.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent evt) {
jButton1MouseClicked(evt);
}
});
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(130, 130, 130)
.addComponent(jButton1)
.addContainerGap(197, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(69, 69, 69)
.addComponent(jButton1)
.addContainerGap(208, Short.MAX_VALUE))
);
pack();
}// </editor-fold>
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}
private void jButton1MouseClicked(java.awt.event.MouseEvent evt) {
// TODO add your handling code here:
JdbcRowSetTest = new JdbcRowSetTest();
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new frmMain().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JButton jButton1;
// End of variables declaration
}
Moze li se to napraviti u Java-i na nacin na koji sam si to smislio ili mozda ne