我是研究Java,SQL的junho。
现在我正在为以下问题而苦苦挣扎。
首先,当我尝试将20020202放入此字段时,发生有关意外数据类型的错误。但是,我不知道如何将数字更改为“日期”
第二,我不知道如何使JTable方法输入到下面这部分。
DefaultTableModel model = new DefaultTableModel(data, columnNames);
JTable table1 = new JTable(model);
table1.setColumnSelectionAllowed(true);
table1.setCellSelectionEnabled(true);
JScrollPane scrollPane = new JScrollPane(table1);
scrollPane.setBounds(10, 53, 600, 200);
addGrid(gbl,gbc,scrollPane,0,4,2,3,0,0);
因为我不输入以下代码,
import java.awt.Component;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.Date;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.Statement;
import java.util.Vector;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableColumn;
public class DbPanel extends JPanel{
public DbPanel() {
Vector columnNames = new Vector();
Vector data = new Vector();
JPanel panel = new JPanel(); //
JPanel panel2 =new JPanel();
GridBagLayout gbl = new GridBagLayout();
GridBagConstraints gbc = new GridBagConstraints();
gbc.fill = GridBagConstraints.BOTH;
setLayout(gbl);
//DB 관리
/* String url = "jdbc:oracle:thin:@localhost:1521:xe";
String user = "madang";
String password = "madang";
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection con = DriverManager.getConnection(url, user, password);
String sql = "select * from STOCK_INFO order by DATE_STOCK";
Statement statement = con.createStatement();
ResultSet resultSet = statement.executeQuery(sql);
ResultSetMetaData metaData = resultSet.getMetaData();
int columns = metaData.getColumnCount();
for (int i = 1; i <= columns; i++) {
columnNames.addElement(metaData.getColumnName(i));
}
while (resultSet.next()) {
Vector row = new Vector(columns);
for (int i = 1; i <= columns; i++) {
row.addElement(resultSet.getObject(i));
}
data.addElement(row);
}
resultSet.close();
statement.close();
} catch (Exception e) {
System.out.println(e);
}*/
//DB 끝
JLabel label = new JLabel("start_date");
addGrid(gbl,gbc,label,0,0,1,1,0,0);
JTextField textfield = new JTextField(8);
addGrid(gbl,gbc,textfield,0,1,1,1,0,0);
JLabel label2 = new JLabel("end_date");
addGrid(gbl,gbc,label2, 2,0,1,1,0,0);
JTextField textfield2 = new JTextField(8);
addGrid(gbl,gbc,textfield2, 2,1,1,1,0,0);
JButton btn = new JButton("search");
addGrid(gbl, gbc, btn, 2, 4, 1, 3, 0, 0);
JLabel lblNewLabel = new JLabel("Stock_database");
lblNewLabel.setHorizontalAlignment(SwingConstants.CENTER);
lblNewLabel.setFont(new Font("Tahoma", Font.BOLD, 14));
/* lblNewLabel.setBounds(10, 11, 927, 31);*/
addGrid(gbl,gbc,lblNewLabel,0,3,4,1,1,0);
DefaultTableModel model = new DefaultTableModel(data, columnNames);
JTable table1 = new JTable(model);
table1.setColumnSelectionAllowed(true);
table1.setCellSelectionEnabled(true);
JScrollPane scrollPane = new JScrollPane(table1);
scrollPane.setBounds(10, 53, 600, 200);
addGrid(gbl,gbc,scrollPane,0,4,2,3,0,0);
btn.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String url = "jdbc:oracle:thin:@localhost:1521:xe";
String user = "madang";
String password = "madang";
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection con = DriverManager.getConnection(url, user, password);
String sql = "select * from stock_info between"+textfield.getText()+"and"+textfield2.getText();
Statement statement = con.createStatement();
ResultSet resultSet = statement.executeQuery(sql);
ResultSetMetaData metaData = resultSet.getMetaData();
int columns = metaData.getColumnCount();
for (int i = 1; i <= columns; i++) {
columnNames.addElement(metaData.getColumnName(i));
}
while (resultSet.next()) {
Vector row = new Vector(columns);
for (int i = 1; i <= columns; i++) {
row.addElement(resultSet.getObject(i));
}
data.addElement(row);
}
resultSet.close();
statement.close();
} catch (Exception e1) {
System.out.println(e1);
}
}
});
/* TableColumn column;
for (int i = 0; i < table1.getColumnCount(); i++) {
column = table1.getColumnModel().getColumn(i);
column.setMaxWidth(250);
}*/
}
private void addGrid(GridBagLayout gbl, GridBagConstraints gbc, Component c, int gridx, int gridy, int gridwidth, int gridheight, int weightx, int weighty) {
gbc.gridx = gridx;
gbc.gridy = gridy;
gbc.gridwidth = gridwidth;
gbc.gridheight = gridheight;
gbc.weightx = weightx;
gbc.weighty = weighty;
gbl.setConstraints(c, gbc);
add(c);
}
}
该表未显示。
因此,我想知道如何解决此问题。 我的英语写作能力不好。因此,对于您而言,这将是不够的信息。 我希望有人给我建议。
这是我编写的整个代码。 我希望它能帮助您了解我正在努力解决的问题。 非常感谢。
{{1}}
答案 0 :(得分:0)
首先,当我尝试将20020202放入此字段时,发生有关意外数据类型的错误。但是,我不知道如何将数字更改为“日期”
将String日期解析为年,月和日值。然后,您可以创建一个Calendar
对象:
Calendar date = new Calendar(...);
一旦有了Calendar对象,就可以使用以下方法创建SQLDate:
Date sqlDate = new Date( date.getTimeInMillis() );
请注意,您应该使用java.sql.Date类,而不是java.util.Date类。
该表未显示。
该表未显示,因为创建模型时列为空。拥有有效数据后,您需要创建模型。
因此代码应类似于:
//DefaultTableModel model = new DefaultTableModel(data, columnNames);
//JTable table1 = new JTable(model);
JTable table1 = new JTable(); // create empty table to add to scroll pane
然后,从ResultSet中获取数据之后,您可以创建模型并将其分配给表:
resultSet.close();
statement.close();
DefaultTableModel model = new DefaultTableModel(data, columnNames);
table1.setModel( model );