-- 作者:admin
-- 发布时间:2007/4/18 23:10:02
-- 回复
错误的原因在于SQL语句中使用了table这个关键词,加上中括号即可解决问题
import java.awt.*; import java.awt.event.*; import java.sql.*; import javax.swing.*; import java.io.*; import java.awt.event.ActionListener; import java.util.EventListener;
public class DataBaseDemo extends JFrame implements ActionListener { public static void main(String[] args) { DataBaseDemo frame = new DataBaseDemo(); }
JButton bu1; JTextArea ta1; public DataBaseDemo() { super("DataBaseDemo"); Container c = getContentPane(); c.setLayout(null); bu1 = new JButton("读取odbc数据库"); bu1.setSize(150, 20); bu1.setLocation(160, 20); bu1.addActionListener(this); c.add(bu1); ta1 = new JTextArea(); ta1.setSize(420, 280); ta1.setLocation(20, 50); c.add(ta1); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(500, 400); setVisible(true); }
public void actionPerformed(ActionEvent e) { String str1 = ""; try { Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); } catch (ClassNotFoundException e1) { System.out.println("Driver not found!"); } try { Connection con1 = DriverManager.getConnection("jdbc:odbc:temp"); Statement st1 = con1.createStatement(); ResultSet rs1 = st1.executeQuery("select * from [table]"); while (rs1.next()) { str1 = str1 + rs1.getString(1) + "\\t" + rs1.getString(2) + "\\t" + rs1.getBoolean(3) + "\\n"; } st1.close(); con1.close(); ta1.setText(str1); javax.swing.JOptionPane.showMessageDialog(null, "chenggong!"); } catch (SQLException e2) { System.out.println(e2.getMessage()); } }
}
[此贴子已经被作者于2010-12-13 19:55:42编辑过]
|