mardi 31 mars 2015

Trying to get from DB varchar strings and put them to the label

Started to learn Java not a long ago and meet that type of problem: I cant take any varchar elements to label from my DB. Int elements are putting well.



my Java code: `import java.awt.*;
import java.awt.event.*;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import javax.swing.*;

public class DiplomaTry extends JFrame{
private String url="jdbc:oracle:thin:@tlayshev:1521:TEST",
login="tester",
password="qwe123",
query="SELECT b FROM DIPLOMA WHERE a > ?";
JCheckBox checkBox = new JCheckBox();
List<String> querylist = new ArrayList<String>();
Connection con;

public void init(){
try {
DriverManager.registerDriver (new oracle.jdbc.driver.OracleDriver());
}

catch (Exception e) {
e.printStackTrace();

}

}
public List<String> adr(){

ResultSet rs = null;

try{
if(con==null){
con= DriverManager.getConnection(url, login, password);
}
PreparedStatement pstmt = con.prepareStatement(query);
pstmt.setInt(1, 9);
rs = pstmt.executeQuery();
while (rs.next()) {
String str = rs.getString(1);
System.out.println(str);
querylist.add(str);
//querylist.add(rs.getString(1));
}
}
catch (SQLException e) {
e.printStackTrace();
}
return querylist;
}
ActionListener actionListener = new ActionListener() {
public void actionPerformed(ActionEvent actionEvent) {
init();
List<String> querylist2=(List<String>) adr();
AbstractButton abstractButton = (AbstractButton)actionEvent.getSource();
boolean selected = abstractButton.getModel().isSelected();
String newLabel = (selected ? querylist2.get(7) : querylist2.get(3));
abstractButton.setText(newLabel);
}
};
DiplomaTry(String s){
super(s);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
try {
con = DriverManager.getConnection(url, login, password);
} catch (SQLException e) {

e.printStackTrace();
}
Font f= new Font("Serif", Font.BOLD, 15);
//setFont(f);
checkBox.addActionListener(actionListener);
checkBox.setMnemonic(KeyEvent.VK_S);
Container contentPane = getContentPane();
contentPane.add(checkBox, BorderLayout.NORTH);
setSize(400, 400);
//setVisible(true);

}





public static void main(String[] args) {
new DiplomaTry("Selecting CheckBox").setVisible(true);



}


} `


and the error is: Exception in thread "AWT-EventQueue-0" java.lang.IndexOutOfBoundsException: Index: 7, Size: 0 at java.util.ArrayList.rangeCheck(Unknown Source) at java.util.ArrayList.get(Unknown Source) at DiplomaTry$1.actionPerformed(DiplomaTry.java:62) at javax.swing.AbstractButton.fireActionPerformed(Unknown Source) at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source) at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source) at javax.swing.JToggleButton$ToggleButtonModel.setPressed(Unknown Source) at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source) at java.awt.Component.processMouseEvent(Unknown Source) at javax.swing.JComponent.processMouseEvent(Unknown Source) at java.awt.Component.processEvent(Unknown Source) at java.awt.Container.processEvent(Unknown Source) at java.awt.Component.dispatchEventImpl(Unknown Source) at java.awt.Container.dispatchEventImpl(Unknown Source) at java.awt.Component.dispatchEvent(Unknown Source) at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source) at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source) at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source) at java.awt.Container.dispatchEventImpl(Unknown Source) at java.awt.Window.dispatchEventImpl(Unknown Source) at java.awt.Component.dispatchEvent(Unknown Source) at java.awt.EventQueue.dispatchEventImpl(Unknown Source) at java.awt.EventQueue.access$500(Unknown Source) at java.awt.EventQueue$3.run(Unknown Source) at java.awt.EventQueue$3.run(Unknown Source) at java.security.AccessController.doPrivileged(Native Method) at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source) at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source) at java.awt.EventQueue$4.run(Unknown Source) at java.awt.EventQueue$4.run(Unknown Source) at java.security.AccessController.doPrivileged(Native Method) at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source) at java.awt.EventQueue.dispatchEvent(Unknown Source) at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source) at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source) at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source) at java.awt.EventDispatchThread.pumpEvents(Unknown Source) at java.awt.EventDispatchThread.pumpEvents(Unknown Source) at java.awt.EventDispatchThread.run(Unknown Source)


I made DB like create table DIPLOMA ( a number, b varchar2(100) ); INSERT INTO DIPLOMA (A, B) VALUES ('20', 'SOME TET10'); vs changing arguments in last query





Aucun commentaire:

Enregistrer un commentaire