05. december 2002 - 19:37Der er
63 kommentarer og 2 løsninger
DatabaseHandler??
Problem:
Jeg har en klasse som skal styre alle databasekald, men nu har jeg siddet med det længe nok og ønsker lidt hjælp.
Hvordan bruger jeg så DatabaseHandleren fra en anden klasse Login, som skal gennemløbe en tabel for brugernavne som passer til det brugeren har indtastet i et login/password-felt i GUI??
import java.sql.*;
public class DatabaseCon {
private Connection con;
public DatabaseCon() { try { Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); String url = "jdbc:odbc:kalender"; con = DriverManager.getConnection(url, "", ""); } catch ( Exception e ) { System.out.println( e ); } }
public void sqlResult(String sql) { try { Statement statement = con.createStatement(); ResultSet set = statement.executeQuery( " " + sql + " " ); } catch ( SQLException e ) { System.out.println( e );
}
}
public void sqlUpdate(String sql) { try { Statement statement = con.createStatement(); statement.executeUpdate(" " + sql + " "); } catch ( SQLException e ) { System.out.println( e ); }
}
public void close() { try { con.close(); } catch ( Exception e ) { System.out.println( "Der skete en fejl ved lukningen af forbindelsen" +e); } } }
Ja og den returnere en fejl... Har du evt prøvet at samle den sqlstring og lave en system.out.println på den for at se om den er korekt? Har mistanke til den!
hmm .. nu skal jeg ikke blande mig i dit db-design .. men som hovedregel er det bedst at holde sig til integers .. de fleste databaser hånderer integers hurtigere end strenge ... men det er vist et sidespor :-)
Giver mfalck helt ret... Er helt klart bedst at bruge int... Netop fordi databasen har nemmere ved at søge på disse og primærnøglen bliver altid brugt...
bom bom -det kan man også godt - men printStackTrace kan tage en PrintStream el PrintWriter som argument ... virker lige godt (i al fald i min version af java :-) )
public opretMedlem (String title) { super( title );
layout = new GridBagLayout(); panel = new JPanel(); panel.setLayout ( layout );
GridBagConstraints con;
JLabel BrugerID = new JLabel( "BrugerID" ); con = createGBC( 0, 0, 1, 1 ); con.anchor = GridBagConstraints.CENTER; con.insets = new Insets(15, 15, 2, 5); con.weightx = 1; con.weighty = 1; add( BrugerID, con );
BrugerID_IN = new JTextField(3); con = createGBC( 1, 0, 1, 1 ); con.anchor = GridBagConstraints.WEST; // con.fill = GridBagConstraints.HORIZONTAL; con.insets = new Insets(15, 5, 2, 15); con.weightx = 1; con.weighty = 1; add( BrugerID_IN, con );
JLabel Password = new JLabel( "Password" ); con = createGBC( 0, 1, 1, 1 ); con.anchor = GridBagConstraints.CENTER; con.insets = new Insets(2, 15, 2, 5); con.weightx = 1; con.weighty = 1; add( Password, con );
PasswordIN = new JTextField(); con = createGBC( 1, 1, 1, 1 ); con.anchor = GridBagConstraints.WEST; con.fill = GridBagConstraints.HORIZONTAL; con.insets = new Insets(2, 5, 2, 15); con.weightx = 1; con.weighty = 1; add( PasswordIN, con );
JLabel Fornavn = new JLabel( "Fornavn" ); con = createGBC( 0, 2, 1, 1 ); con.anchor = GridBagConstraints.CENTER; con.insets = new Insets(2, 15, 2, 5); con.weightx = 1; con.weighty = 1; add( Fornavn, con );
FornavnIN = new JTextField(); con = createGBC( 1, 2, 1, 1 ); con.anchor = GridBagConstraints.CENTER; con.fill = GridBagConstraints.HORIZONTAL; con.insets = new Insets(2, 5, 2, 15); con.weightx = 1; con.weighty = 1; add( FornavnIN, con );
JLabel Efternavn = new JLabel( "Efternavn" ); con = createGBC( 0, 3, 1, 1 ); con.anchor = GridBagConstraints.CENTER; con.insets = new Insets(2, 15, 2, 5); con.weightx = 1; con.weighty = 1; add( Efternavn, con );
EfternavnIN = new JTextField(); con = createGBC( 1, 3, 2, 1 ); con.anchor = GridBagConstraints.CENTER; con.fill = GridBagConstraints.HORIZONTAL; con.insets = new Insets(2, 5, 2, 15); con.weightx = 1; con.weighty = 1; add( EfternavnIN, con );
JLabel Adresse = new JLabel( "Adresse" ); con = createGBC( 0, 4, 1, 1 ); con.anchor = GridBagConstraints.CENTER; con.insets = new Insets(2, 15, 2, 5); con.weightx = 1; con.weighty = 1; add( Adresse, con );
AdresseIN = new JTextField(); con = createGBC( 1, 4, 2, 1 ); con.anchor = GridBagConstraints.CENTER; con.fill = GridBagConstraints.HORIZONTAL; con.insets = new Insets(2, 5, 2, 15); con.weightx = 1; con.weighty = 1; add( AdresseIN, con );
JLabel Adresse2 = new JLabel( "Adresse2" ); con = createGBC( 0, 5, 1, 1 ); con.anchor = GridBagConstraints.CENTER; con.insets = new Insets(2, 15, 2, 5); con.weightx = 1; con.weighty = 1; add( Adresse2, con );
Adresse2IN = new JTextField(); con = createGBC( 1, 5, 2, 1 ); con.anchor = GridBagConstraints.CENTER; con.fill = GridBagConstraints.HORIZONTAL; con.insets = new Insets(2, 5, 2, 15); con.weightx = 1; con.weighty = 1; add( Adresse2IN, con );
JLabel Postnummer = new JLabel( "Postnummer" ); con = createGBC( 0, 6, 1, 1 ); con.anchor = GridBagConstraints.CENTER; con.insets = new Insets(2, 15, 15, 5); con.weightx = 1; con.weighty = 1; add( Postnummer, con );
PostnummerIN = new JTextField(3); con = createGBC( 1, 6, 1, 1 ); con.anchor = GridBagConstraints.WEST; // con.fill = GridBagConstraints.HORIZONTAL; con.insets = new Insets(2, 5, 15, 95); con.weightx = 1; con.weighty = 1; add( PostnummerIN, con );
panel.setBorder( new CompoundBorder( new EmptyBorder( 0, 0, 0, 0 ), new CompoundBorder( new TitledBorder( new EtchedBorder( EtchedBorder.LOWERED ), ""), new EmptyBorder( 0, 0, 0, 0 ))));
layout = new GridBagLayout(); panel2 = new JPanel(); panel2.setLayout( layout );
JLabel Initialer = new JLabel( "Initialer" ); con = createGBC( 3, 0, 1, 1 ); con.anchor = GridBagConstraints.CENTER; con.insets = new Insets(15, 15, 2, 5); con.weightx = 1; con.weighty = 1; add2( Initialer, con );
InitialerIN = new JTextField(); con = createGBC( 4, 0, 1, 1 ); con.anchor = GridBagConstraints.EAST; con.fill = GridBagConstraints.HORIZONTAL; con.insets = new Insets(15, 5, 2, 100); con.weightx = 1; con.weighty = 1; add2( InitialerIN, con );
JLabel Tlf = new JLabel( "Tlf" ); con = createGBC( 3, 1, 1, 1 ); con.anchor = GridBagConstraints.CENTER; con.insets = new Insets(2, 15, 2, 5); con.weightx = 1; con.weighty = 1; add2( Tlf, con );
TlfIN = new JTextField(); con = createGBC( 4, 1, 1, 1 ); con.anchor = GridBagConstraints.EAST; con.fill = GridBagConstraints.HORIZONTAL; con.insets = new Insets(2, 5, 2, 35); con.weightx = 1; con.weighty = 1; add2( TlfIN, con );
JLabel MobilTlf = new JLabel( "MobilTlf" ); con = createGBC( 3, 2, 1, 1 ); con.anchor = GridBagConstraints.CENTER; con.insets = new Insets(2, 15, 2, 5); con.weightx = 1; con.weighty = 1; add2( MobilTlf, con );
MobilTlfIN = new JTextField(); con = createGBC( 4, 2, 1, 1 ); con.anchor = GridBagConstraints.EAST; con.fill = GridBagConstraints.HORIZONTAL; con.insets = new Insets(2, 5, 2, 35); con.weightx = 1; con.weighty = 1; add2( MobilTlfIN, con );
JLabel Email = new JLabel( "Email" ); con = createGBC( 3, 3, 1, 1 ); con.anchor = GridBagConstraints.CENTER; con.insets = new Insets(2, 15, 2, 5); con.weightx = 1; con.weighty = 1; add2( Email, con );
EmailIN = new JTextField(); con = createGBC( 4, 3, 2, 1 ); con.anchor = GridBagConstraints.EAST; con.fill = GridBagConstraints.HORIZONTAL; con.insets = new Insets(2, 5, 2, 35); con.weightx = 1; con.weighty = 1; add2( EmailIN, con );
JLabel GruppeID = new JLabel( "GruppeID" ); con = createGBC( 3, 4, 1, 1 ); con.anchor = GridBagConstraints.CENTER; con.insets = new Insets(2, 15, 15, 5); con.weightx = 1; con.weighty = 1; add2( GruppeID, con );
GruppeID_IN = new JTextField(3); con = createGBC( 4, 4, 1, 1 ); con.anchor = GridBagConstraints.WEST; // con.fill = GridBagConstraints.HORIZONTAL; con.insets = new Insets(2, 5, 15, 15); con.weightx = 1; con.weighty = 1; add2( GruppeID_IN, con );
panel2.setBorder( new CompoundBorder( new EmptyBorder( 0, 0, 0, 0 ), new CompoundBorder( new TitledBorder( new EtchedBorder( EtchedBorder.LOWERED ), ""), new EmptyBorder( 0, 0, 0, 0 ))));
layout = new GridBagLayout(); panel3 = new JPanel(); panel3.setLayout( layout );
OK = new JButton(" OK "); con = createGBC(1, 6, 1, 1); con.fill = GridBagConstraints.HORIZONTAL; con.insets = new Insets(2, 15, 2, 5); OK.addActionListener(this); con.weightx = 1; add3( OK, con );
Anuller = new JButton("ANULLER"); con = createGBC(4, 6, 1, 1); Anuller.addActionListener(this); con.fill = GridBagConstraints.HORIZONTAL; con.insets = new Insets(2, 5, 2, 35); con.weightx = 1; add3(Anuller, con );
panel3.setBorder( new CompoundBorder( new EmptyBorder( 0, 0, 0, 0 ), new CompoundBorder( new TitledBorder( new EtchedBorder( EtchedBorder.LOWERED ), ""), new EmptyBorder( 0, 0, 0, 0 ))));
getContentPane().setLayout( new FlowLayout() ); getContentPane().add(panel); getContentPane().add(panel2); getContentPane().add(panel3);
getContentPane().setLayout(new GridBagLayout( ));
con = createGBC(0, 0, 1, 1); con.fill = GridBagConstraints.HORIZONTAL; con.insets = new Insets(5, 5, 2, 5); getContentPane().add(panel , con);
con = createGBC(0, 1, 1, 1); con.fill = GridBagConstraints.HORIZONTAL; con.insets = new Insets(3, 5, 3, 5); getContentPane().add(panel2 , con);
con = createGBC(0, 2, 1, 1); con.fill = GridBagConstraints.HORIZONTAL; con.insets = new Insets(2, 5, 5, 5); getContentPane().add(panel3 , con);
prøvede du den her ... det er lidt at gribe efter strå .. ellers må du sætte System.out.println("1"); ind for hver linie og se hvor det går galt i sqlUpdate.
public void sqlUpdate(String sql) { try { if (con.isClosed) System.out.print("forbindelsen er væk"); Statement statement = con.createStatement(); statement.executeUpdate(" " + sql + " "); } catch ( SQLException e ) { System.out.println( e ); }
Helt klart... Ville også ønske man kunne sende en privat besked... For jeg har det på samme måde med at give mails ud til msn og uin til icq...
Synes godt om
Ny brugerNybegynder
Din løsning...
Tilladte BB-code-tags: [b]fed[/b] [i]kursiv[/i] [u]understreget[/u] Web- og emailadresser omdannes automatisk til links. Der sættes "nofollow" på alle links.