0
我不久前开始学习Java,遇到了这种类型的问题,我无法从我的数据库中获取任何varchar
元素的标签。 Int
元素放好。试图从数据库varchar字符串获取并将它们放到标签
我的Java代码:
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);
}
}
和错误是:
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)
我所做的DB,如:
create table DIPLOMA (
a number,
b varchar2(100)
);
INSERT INTO DIPLOMA (A, B)
VALUES ('20', 'SOME TET10');
VS在过去的查询变化的参数。
我知道,但我不明白为什么querylist不能从DB – 2015-03-31 14:00:52
'rs = pstmt.executeQuery();'给你零结果?我猜问题不是查询,但对数据的处理... – 2015-03-31 15:03:20
与postegre sql它的作品!我只是改变了连接到postegre驱动程序的方法...并尝试我的程序在MAC上,它终于开始工作...我认为问题是在数据格式的oracl数据库 – 2015-03-31 21:25:29