Oracle + JDBC A JDBC egy Java alapú kapcsolatot biztosít az

Cecil Allen | Download | HTML Embed
  • Mar 4, 2011
  • Views: 15
  • Page(s): 4
  • Size: 74.88 kB
  • Report

Share

Transcript

1 Oracle + JDBC A JDBC egy Java alap kapcsolatot biztost az adatbzis szerver s egy Java alap kliens kztt. Oracle Express Edition belltsok (Linuxon): JAVA_HOME=/usr/java/jdk1.6.0_24/ ORACLE_HOME=/usr/lib/oracle/xe/app/oracle/product/10.2.0/server/ LD_LIBRARY_PATH=$LD_LIBRARY_PATH:$ORACLE_HOME/lib CLASSPATH=$ORACLE_HOME/jdbc/lib/ojdbc14.jar:$ORACLE_HOME/jlib/orai18n.jar: $JAVA_HOME/src.zip Pldaprogram: importjava.sql.*; importoracle.jdbc.*; importoracle.jdbc.pool.OracleDataSource; importjavax.swing.*; importjavax.swing.table.*; importjava.awt.*; importjava.awt.event.*; importjava.util.*; classJDBCTestGUI_xeextendsJFrameimplementsActionListener,ItemListener { privateJTableresult_table; privateJButtonsearch_button; privateTextFieldname_field; privateJPanelinput_panel; privateJPaneloutput_panel; privateJPanelstatic_main_panel; privateDefaultTableModeltable_model; privateJScrollPanescroll_pane; privateVectorcolumn_names_vector; privateChoicefield_choice; privateStringchoice_string; privateJTabbedPanetabbed_pane; privateResultSetrs; privateStatementstmt; /** *Constuctor.Creatingandinitializingobjects. **/ publicJDBCTestGUI_xe(Stringtitle){ super(title); this.setSize(600,400); this.static_main_panel=newJPanel(); Konstruktor, belltsok this.output_panel=newJPanel(); this.input_panel=newJPanel(); this.name_field=newTextField(50); this.search_button=newJButton("Search"); this.field_choice=newChoice(); createGUI(); try{ /*ConnecttotheOracleDatabaseandusingthe"HR"user'sschema*/ OracleDataSourceods=newOracleDataSource(); Kapcsolds az adatbzishoz ods.setURL("jdbc:oracle:thin:hr/[email protected]:1521/XE"); Connectionconn=ods.getConnection("HR","HR"); stmt=conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_READ_ONLY);

2 }catch(Exceptionex){ ex.printStackTrace(); } } /** *Creatingthegraphicaluserinterfacefortheprogram. **/ publicvoidcreateGUI(){ tabbed_pane=newJTabbedPane(JTabbedPane.TOP);//thetabbedpanewillbeshowmoresheets tabbed_pane.addTab("StaticSQL",this.static_main_panel);//actuallyonlyonesheetisused this.static_main_panel.setLayout(newBorderLayout());//themainpanelfordemonstratesthestaticSQLqueries this.input_panel.setLayout(newGridLayout(3,3)); this.output_panel.setLayout(newBorderLayout()); Grafikus kezeli fellet, elrendezs belltsa //thelabelsandinputfields this.input_panel.add(newLabel("Content:")); this.input_panel.add(this.name_field); this.input_panel.add(newLabel("Searchinfield:")); this.input_panel.add(this.field_choice); this.input_panel.add(this.search_button); this.getContentPane().add(tabbed_pane); this.static_main_panel.add(input_panel,BorderLayout.NORTH); this.static_main_panel.add(output_panel,BorderLayout.CENTER); //addingeventlisteners this.field_choice.addItemListener(this); this.search_button.addActionListener(this); Esemnykezels hozzrendelse //columnnamesinavector this.column_names_vector=newVector(); this.column_names_vector.add("Firstname"); this.column_names_vector.add("Lastname"); this.column_names_vector.add("Email"); this.column_names_vector.add("Salary"); this.column_names_vector.add("Department"); //needtouseatablemodelfordynamictablehandling this.table_model=newDefaultTableModel(column_names_vector,0); this.result_table=newJTable(table_model); this.scroll_pane=newJScrollPane(result_table); this.output_panel.add(scroll_pane); Tbla belltsa az eredmnyekhez //thecolumnnameslistedintoadropdownlist this.field_choice.add("Firstname"); this.field_choice.add("Lastname"); this.field_choice.add("Email"); this.field_choice.add("Department"); choice_string="Firstname"; } publicvoidactionPerformed(ActionEvente){ if(e.getSource()==this.search_button){ /***************Startingqueries***************/ Stringsql=""; if(this.name_field.getText().equals("")){ sql="SELECTfirst_name,last_name,email,salary,department_nameFROMemployees,departmentsWHERE employees.department_id=departments.department_idORDERBYlast_name";

3 }else{ if(choice_string=="Lastname"){ sql="SELECTfirst_name,last_name,email,salary,department_nameFROMemployees,departmentsWHERE employees.department_id=departments.department_idANDlast_nameLIKE'"+name_field.getText()+ "'ORDERBYlast_name"; }elseif(choice_string=="Firstname"){ sql="SELECTfirst_name,last_name,email,salary,department_nameFROMemployees,departmentsWHERE employees.department_id=departments.department_idANDfirst_nameLIKE'"+name_field.getText()+ "'ORDERBYlast_name"; }elseif(choice_string=="Email"){ sql="SELECTfirst_name,last_name,email,salary,department_nameFROMemployees,departmentsWHERE employees.department_id=departments.department_idANDemailLIKE'"+name_field.getText()+ "'ORDERBYlast_name"; }elseif(choice_string=="Department"){ sql="SELECTfirst_name,last_name,email,salary,department_nameFROMemployees,departmentsWHERE employees.department_id=departments.department_idANDdepartment_nameLIKE'"+ name_field.getText()+"'ORDERBYlast_name"; } } try{ System.out.println(sql); rs=stmt.executeQuery(sql); //removingallrowsfromthetable intcount=table_model.getRowCount(); for(inti=count1;i>=0;i){ table_model.removeRow(i); } repaint(); while(rs.next()){ Stringrow[]={rs.getString(1),rs.getString(2),rs.getString(3),rs.getInt(4)+"",rs.getString(5)}; this.table_model.addRow(row);//addingnewrowintothetable } repaint(); }catch(SQLExceptionex){ ex.printStackTrace(); } } } publicvoiditemStateChanged(ItemEvente){ this.choice_string=field_choice.getSelectedItem(); } publicstaticvoidmain(Stringargs[])throwsSQLException { OracleDataSourceods=newOracleDataSource(); ods.setURL("jdbc:oracle:thin:hr/[email protected]:1521/XE"); Connectionconn=ods.getConnection("HR","HR"); //CreateOracleDatabaseMetaDataobject DatabaseMetaDatameta=conn.getMetaData(); /**************/ JDBCTestGUI_xegui=newJDBCTestGUI_xe("TestapplicationtotryJDBCwithOracle"); gui.setVisible(true); gui.show(); //thisisanimplementationofwindowlistener //theprogramwillbestoppedifyouclosethemainframe WindowListenerlistener=newWindowAdapter(){ publicvoidwindowClosing(WindowEventwe){ System.exit(0); }

4 }; gui.addWindowListener(listener); /**************/ } }

Load More