CSDN博客

img liuqhui

java连接数据库==>出现问题了

发表于2004/10/15 15:16:00  846人阅读

import java.awt.*;
import javax.swing.*;
import awt.event.*;
import java.io.*;
import java.sql.*;
public class conn extends JFrame implements ActionListener
{
public static void main(string[] args){
       conn frame = new conn ();
}
     JButton bu1;
     JTextArea ta1;
     public conn (){
 super("conn");
 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  {
 //加载JDBC驱动程序
 Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
          }
   catch(ClassNotFoundException e1)
 {
 System.out.println("Driver not found!");
 }
try  {
  //使用getConnection()方法建立连接ODBC的对象
 Connection con1=DriverManager.getConnetion("jdbc:odbc:成绩数据库"); //前面建立了一个数据库:成绩数据库
 //创建Statement对象,以执行SQL命令
 Statement st1=con1.createStatement();
 //执行SQL命令,取得scoretable数据表数据
 ResultSet rs1 = st1.executeQuery("select * from scoretable");
 //代是显示数据表内容
 while (rs1.next())
 {
     str1=str1+rs1.getString(1)+rs1.getString(2)+"/t"+rs1.getLong(4)+"/t"+rs1.getLong(5)+"/t"+rs1.getLong(6)+"/t"+rs1.getLong(7)+"/n";
 }
 //关闭Statement对象
 st1.close();
 //关闭ODBC连接对象
 con1.close();
 ta1.setText(str1);
 JOptionPane.showMessageDialog(null,"数据加载成功");
 }
 catch(SQLException e2)
  {
   System.out.println("table not found!");
  }
 }
}

错误如下:
conn.java [3:1] package awt.event does not exist
import awt.event.*;
^
conn.java [6:1] cannot resolve symbol
symbol  : class ActionListener
location: class conn
public class conn extends JFrame implements ActionListener
                                            ^
conn.java [8:1] cannot resolve symbol
symbol  : class string
location: class conn
public static void main(string[] args){
                        ^
conn.java [31:1] cannot resolve symbol
symbol  : class ActionEvent
location: class conn
public void actionperformed(ActionEvent e) {
                            ^
conn.java [20:1] addActionListener(java.awt.event.ActionListener) in javax.swing.AbstractButton cannot be applied to (conn)
        bu1.addActionListener(this);
           ^
conn.java [32:1] cannot resolve symbol
symbol  : class string
location: class conn
   string str1="";
   ^
conn.java [43:1] cannot resolve symbol
symbol  : method getConnetion (java.lang.String)
location: class java.sql.DriverManager
        Connection con1=DriverManager.getConnetion("jdbc:odbc:成绩数据库"); //前面建立了一个数据库:成绩数据库
                                     ^
7 errors
编译  conn. 时出错

目前在努力调试着,如果你懂,请给我点指引好吗?








阅读全文
0 0

相关文章推荐

img
取 消
img