Boa Tarde javaneses…
Eh o seguinte estou fazendo um pequeno sistema Desktop e tenho algumas duvidas quanto a conexao ao banco de Dados:
Fiz uma Classe de Conexao ao Banco:
import java.sql.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Conexao extends JFrame
{
public Connection conexao = null;
public Conexao()
{
//conexao com o Banco de Dados Oracle --inicio--
try {
String url = "jdbc:oracle:thin:@192.168.1.1:1521:teste";
String username = "teste";
String password = "teste";
Class.forName ("oracle.jdbc.driver.OracleDriver");
conexao = DriverManager.getConnection ( url, username, password );
//System.out.print("ok\n");
}
catch ( ClassNotFoundException cnfex ) {
}
catch ( SQLException sqlex ) {
}
catch ( Exception ex ) {
ex.printStackTrace();
}
//conexao com o Banco de Dados Oracle --fim--
}
public static void main ( String args[] )
{
Conexao app = new Conexao();
app.addWindowListener
(
new WindowAdapter()
{
public void windowClosing ( WindowEvent e )
{
System.exit ( 0 );
}
}
);
}
}
Agora eu nao sei como usar essa classe dentro de outras classes, para que eu nao precise fazer uma nova conexao em cada novo programa.
Como se faz isso? Alguem pode dar uma ajudinha ai?
A outra Classe eh essa:
Código:
import java.sql.*;
import java.net.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class C extends JFrame
{
public ResultSet rs = null;
public Statement st = null;
public String qy = null;
// private JLabel output;
// private String url, username, password;
private Connection conexao = null;
private JTextField jt_Codigo;
private JTextField jt_Descricao;
private JTextField jt_Estoque1;
private JTextField jt_Estoque2;
private JFormattedTextField jt_Preco;
private JLabel jl_Codigo;
private JLabel jl_Descricao;
private JLabel jl_Estoque1;
private JLabel jl_Estoque2;
private JLabel jl_Preco;
private String xCodigo;
private String xDescricao;
private JButton okButton;
private JButton fimButton;
//JPanel panePrincipal = new JPanel();
JPanel paneBotoes = new JPanel();
public C( Connection conexao)
{
Container c = getContentPane();
setTitle ( "C o n s u l t a E s t o q u e" );
c.setLayout( null );
//c.add ( panePrincipal );
//panePrincipal.setBounds ( 0,0,305,20 );
//panePrincipal.setBackground ( new Color ( 0,255,153 ) );
c.add ( paneBotoes );
paneBotoes.setBackground ( new Color ( 150,155,120 ) );
paneBotoes.setBounds ( 0,166,600,35 );
//conexao com o Banco de Dados Oracle --inicio--
/* try {
String url = "jdbc:oracle:thin:@192.168.1.1:1521:teste";
String username = "teste";
String password = "teste";
Class.forName ("oracle.jdbc.driver.OracleDriver");
conexao = DriverManager.getConnection ( url, username, password );
}
catch ( ClassNotFoundException cnfex ) {
}
catch ( SQLException sqlex ) {
}
catch ( Exception ex ) {
ex.printStackTrace();
}
//conexao com o Banco de Dados Oracle --fim--
*/
//output.setBackground ( new Color ( 0,255,153 ) );
fimButton = new JButton();
okButton = new JButton();
setDefaultCloseOperation ( WindowConstants.EXIT_ON_CLOSE );
c.setBackground ( new Color ( 220, 220, 180 ) );
//labels --inicio--
jl_Codigo = new JLabel ( "Codigo" );
c.add ( jl_Codigo );
jl_Codigo.setBounds ( 10, 20, 140, 15 );
jl_Descricao = new JLabel ( "Descricao" );
c.add ( jl_Descricao );
jl_Descricao.setBounds ( 10, 50, 140, 15 );
jl_Estoque1 = new JLabel ( "Estoque 1" );
c.add ( jl_Estoque1 );
jl_Estoque1.setBounds ( 10, 80, 140, 15 );
jl_Estoque2 = new JLabel ( "Estoque 2" );
c.add ( jl_Estoque2 );
jl_Estoque2.setBounds ( 10, 110, 140, 15 );
jl_Preco = new JLabel ( "Preco Venda" );
c.add ( jl_Preco );
jl_Preco.setBounds ( 10, 140, 140, 15 );
//labels --fim--
//campos editaveis JTextField --inicio--
jt_Codigo = new JTextField();
jt_Codigo.addActionListener ( new ActionListener()
{ public void actionPerformed ( ActionEvent e )
{
//jt_CodigoActionPerformed( e );
PreparedStatement st = null;
try {
xCodigo = jt_Codigo.getText().trim();
st = connect.prepareStatement (" SELECT * FROM SB1010 WHERE B1_COD = '" + xCodigo + "' ");
ResultSet rs = st.executeQuery();
rs.next();
//jt_Descricao = new JTextField();
//xDescricao = rs.getString("B1_DESC");
//jt_Descricao.toString() = xDescricao;
//jt_Preco = rs.getString("B1_PRV1");
} catch ( Exception x ){}
finally {}
}
} );
c.add ( jt_Codigo );
jt_Codigo.setBounds ( 150, 10, 160, 19 );
jt_Codigo.requestFocus(true);
//c.add ( jt_Descricao );
//jt_Descricao.setBounds ( 90, 120, 100, 20 );
/* try
{ javax.swing.text.MaskFormatter mdataField = new javax.swing.text.MaskFormatter("##/##/####");
dataField = new javax.swing.JFormattedTextField(mdataField);
} catch(Exception e){}
dataField.addActionListener ( new ActionListener()
{ public void actionPerformed ( ActionEvent e ) {
filialField.requestFocus();
}
} );
c.add ( dataField );
dataField.setBounds ( 90, 90, 100, 21 );
*/
//botoes de confirma e finaliza --inicio--
okButton.setText ( "Proximo" );
okButton.setMnemonic ( 'P' );
okButton.addActionListener ( new ActionListener() {
public void actionPerformed ( ActionEvent e ) {
//okButtonActionPerformed ( e );
} } );
paneBotoes.add(okButton); //c.add ( okButton, paneBotoes );
fimButton.setText ( " Fim " );
fimButton.setMnemonic ( 'F' );
fimButton.addActionListener ( new ActionListener() {
public void actionPerformed ( ActionEvent e ) {
fimButtonActionPerformed ( e );
} } );
paneBotoes.add(fimButton); //c.add ( fimButton, paneBotoes );
//botoes de confirma e finaliza --fim--
pack();
setSize( 600, 223 ); //seta o tamanho do frame
setLocationRelativeTo(null); //posiciona o frame no centro da tela
}
private void fimButtonActionPerformed ( ActionEvent e ) {
System.exit( 0 );
}
public static void main ( String[] args ) {
javax.swing.SwingUtilities.invokeLater ( new Runnable() {
public void run() {
new C().setVisible( true );
}
} );
}
}
Alem dessa classe pretendo usar a mesma conexao em outras…
Qualquer ajuda eh bem vinda…
e se nao eh pedir demais essa forma que estou usando para programar esta correta? Ou tem alguma outra forma mais nos padroes Java…
faz pouco tempo que estou em contato com Java.