在程序MULTIPLE QUESTIONS中改变Java程序以使用SQL数据而不是Array

时间:2016-09-03 16:36:58

标签: java sql oracle swing

我试图删除数组并使用SQL程序为Swing程序提取数据。第10行我收到错误的构造'错误,并且已经在第173行定义了“DataClass”。我的问题是我如何修复错位的构造错误,第二个我需要删除或添加什么以从SQL获取数据对阵阵?我相信我已经正确加载了Oracle Thin Driver。

package Assignment_2;

import java.net.URL;
import java.sql.*;

import oracle.jdbc.OracleResultSetMetaData;

public class OracleDBAssignment_02Source{

    public static void main(String args[]){ /*error message 'Syntax error on token(s), misplaced construct(s)'*/

    try{
    DriverManager.registerDriver (new oracle.jdbc.OracleDriver());      
    Connection con = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:XE", "student1","pass");
    System.out.println("Connection established - now executing a select");

    Statement stmt = con.createStatement( );
    ResultSet rs = stmt.executeQuery("SELECT * FROM ADDRESS");

    System.out.println("Received Results:");

    int i = rs.getMetaData().getColumnCount( );

    while(rs.next( ) ){
        for(int x = 1; x <= i; ++x){
            System.out.println(rs.getString(x));
            }
        System.out.println("");
        }

    stmt.close( );
    con.close();
    }

    catch (java.lang.Exception ex){
        ex.printStackTrace();
    }
  } 
}

import javax.swing.*;
import java.awt.*;

class Assignment_02Source extends JFrame{
   private JButton buttonPrev = new JButton("Prev");
   private JButton buttonReset = new JButton("Reset");
   private JButton buttonNext = new JButton("Next");
   private JLabel labelHeader = new JLabel("Database Browser", JLabel.CENTER);
   private JLabel labelName = new JLabel("Name");
   private JLabel labelAddress = new JLabel("Address");
   private JLabel labelCity = new JLabel("City");
   private JLabel labelState = new JLabel("State");
   private JLabel labelZip = new JLabel("Zip");
   private JTextField textFieldName = new JTextField();
   private JTextField textFieldAddress = new JTextField();
   private JTextField textFieldCity = new JTextField();

   private JTextField textFieldState = new JTextField();

   private JTextField textFieldZip = new JTextField();

   DataClass[] DataClassArray = {

     new DataClass("James", "William", "101 Here", "Ok", "123234"),

     new DataClass("Martin", "Edan", "102 There", "Androed", "123235"),

     new DataClass("Ebenzer", "Rachel", "103 No Where", "OK", "123236") };

   int arrayPointer = 0;

   public Assignment_02Source(String title)

   {
   super(title);
   setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
    JPanel cp = (JPanel) getContentPane( ) ;         

    labelHeader.setFont(new Font("CourierNew", Font.ITALIC, 12));
    labelHeader.setBounds(40, 10, 300, 50);
    buttonPrev.setBounds(30, 250, 80, 25);
    buttonReset.setBounds(150, 250, 80, 25);
    buttonNext.setBounds(270, 250, 80, 25);

    labelName.setBounds(10, 80, 80, 25);
    labelAddress.setBounds(10, 110, 80, 25);
    labelCity.setBounds(10, 140, 80, 25);
    labelState.setBounds(10, 170, 80, 25);
    labelZip.setBounds(10, 200, 80, 25);
    textFieldName.setBounds(120, 80, 250, 25);
    textFieldAddress.setBounds(120, 110, 250, 25);
    textFieldCity.setBounds(120, 140, 250, 25);
    textFieldState.setBounds(120, 170, 250, 25);
    textFieldZip.setBounds(120, 200, 250, 25);

    cp.setLayout(null);
    cp.add(labelHeader);
    cp.add(buttonPrev);
    cp.add(buttonReset);
    cp.add(buttonNext);

    cp.add(labelName);
    cp.add(textFieldName);
    cp.add(labelAddress);
    cp.add(textFieldAddress);
    cp.add(labelCity);
    cp.add(textFieldCity);
    cp.add(labelState);
    cp.add(textFieldState);
    cp.add(labelZip);
    cp.add(textFieldZip);
    addWindowListener(new java.awt.event.WindowAdapter( ){

        public void windowClosing(java.awt.event.WindowEvent evt){
            shutDown( );
        }
    });

    buttonPrev.addActionListener(new java.awt.event.ActionListener( )
    {            
    public void actionPerformed(java.awt.event.ActionEvent evt)
    {              
    if (--arrayPointer < 0){                   
        arrayPointer = DataClassArray.length - 1;
    }               
    setFields(arrayPointer);
    }        
});        

buttonNext.addActionListener(new java.awt.event.ActionListener(){

public void actionPerformed(java.awt.event.ActionEvent evt){
    if (++arrayPointer == DataClassArray.length)
        arrayPointer = 0;
        setFields(arrayPointer);
    }
});

buttonReset.addActionListener(new java.awt.event.ActionListener(){

    public void actionPerformed(java.awt.event.ActionEvent evt){

        arrayPointer = 0;
        setFields(arrayPointer);             
    }
});

setFields(arrayPointer);
}

private void setFields(int position){
    textFieldName.setText(DataClassArray[position].getName( ));
    textFieldAddress.setText(DataClassArray[position].getAddress( ));
    textFieldCity.setText(DataClassArray[position].getCity( ));
    textFieldState.setText(DataClassArray[position].getState( ));
    textFieldZip.setText(DataClassArray[position].getZip( ));
}

private void shutDown( ){       
    int returnVal = JOptionPane.showConfirmDialog(this,"Are you you want to EXIT?");       
    if (returnVal == JOptionPane.YES_OPTION){            
        System.exit(0);
    }
}     

public static void main(String args[ ]){         
    Assignment_02Source a2= new Assignment_02Source ("Database Browser");
    a2.setSize(400, 350);
    a2.setVisible(true);
    }
}

class DataClass{    /*Error Message 'The type DataClass is already defined'*/

String name, address, city, state, zipCode;
DataClass(String name, String address, String city, String state,String zipCode){
    this.name = name;       
    this.address = address;        
    this.city = city;        
    this.state = state;        
    this.zipCode = zipCode;    
}

String getName( ){   
    return this.name;    
}

String getAddress( ){      
    return this.address;
}
String getCity( ){
    return this.city;    
}    
String getState( ){       
    return this.state;    
}    
String getZip(){       
    return this.zipCode;    
  }
}

0 个答案:

没有答案