ysyyrps1 2009-07-17 19:09
浏览 151
已采纳

新手请教个问题

import java.sql.*;
import java.awt.Graphics;
import java.io.Serializable;
public class JdbcDemo{
public JdbcDemo()
{
int id[]=new int[200];
int style[]=new int[200];
Double p1[]=new Double[200];
Double p2[]=new Double[200];
Double p3[]=new Double[200];
Double p4[]=new Double[200];
Double p5[]=new Double[200];
Double p6[]=new Double[200];
int i=0;
String query="SELECT id,style,p1,p2,p3,p4,p5,p6 FROM draw";
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con =DriverManager.getConnection ("jdbc:odbc:CAXA");
Statement stmt=con.createStatement();
ResultSet rs=stmt.executeQuery(query);
while (rs.next()){
id[i]=rs.getInt("id");
style[i]=rs.getInt("style");
p1[i]=rs.getDouble("p1");
p2[i]=rs.getDouble("p2");
p3[i]=rs.getDouble("p3");
p4[i]=rs.getDouble("p4");
p5[i]=rs.getDouble("p5");
p6[i]=rs.getDouble("p6");
System.out.println(id[i]+","+style[i]+","+p1[i]+","+p2[i]+","+p3[i]+","+p4[i]+","+p5[i]+","+p6[i]);
i++;
}

             con.close();
 }
 catch(ClassNotFoundException e){
    e.printStackTrace();
 }
 catch(SQLException e){
    e.printStackTrace();
 }
  System.out.println(i);
}

}
class Execute extends java.applet.Applet implements Serializable
{
public Execute()
{
JdbcDemo x = new JdbcDemo();

   }
  public void paint(Graphics g)
  {
   for(int k=0;k<=i;k++)
     {
       if(style[k]==1)
        g.drawLine(p1[k],p2[k],p3[k],p4[k]);
       if(style[k]==2)
        g.drawOval(p1[k],p2[k],p3[k],p4[k]);
     }

  }

}
以上是我做一个题的代码,读取数据库的类没有问题但画图的类怎么也找不到 JdbcDemo类中读取的数组,
而且我将 JdbcDemo类已经设为了public,还望指点!

  • 写回答

1条回答 默认 最新

  • wanghaolovezlq 2009-07-17 19:25
    关注

    你这代码问题太多了,
    JdbcDemo类中读取的数组都是局部变量,你要定义成成员变量!!!

    这样才能编译通过,并运行,逻辑没动你的
    [code="java"]
    import java.sql.*;
    import java.awt.Graphics;
    import java.io.Serializable;

    public class JdbcDemo {

    public int id[] = new int[200];
    public int style[] = new int[200];
    public Double p1[] = new Double[200];
    public Double p2[] = new Double[200];
    public Double p3[] = new Double[200];
    public Double p4[] = new Double[200];
    public Double p5[] = new Double[200];
    public Double p6[] = new Double[200];
    public int i = 0;
    
    public JdbcDemo() {
    
        String query = "SELECT id,style,p1,p2,p3,p4,p5,p6 FROM draw";
        try {
            Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
            Connection con = DriverManager.getConnection("jdbc:odbc:CAXA");
            Statement stmt = con.createStatement();
            ResultSet rs = stmt.executeQuery(query);
            while (rs.next()) {
                id[i] = rs.getInt("id");
                style[i] = rs.getInt("style");
                p1[i] = rs.getDouble("p1");
                p2[i] = rs.getDouble("p2");
                p3[i] = rs.getDouble("p3");
                p4[i] = rs.getDouble("p4");
                p5[i] = rs.getDouble("p5");
                p6[i] = rs.getDouble("p6");
                System.out.println(id[i] + "," + style[i] + "," + p1[i] + ","
                        + p2[i] + "," + p3[i] + "," + p4[i] + "," + p5[i] + ","
                        + p6[i]);
                i++;
            }
    
            con.close();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        System.out.println(i);
    }
    

    }

    class Execute extends java.applet.Applet implements Serializable {
    public JdbcDemo x = new JdbcDemo();

    public Execute() {
    
    }
    
    public void paint(Graphics g) {
        for (int k = 0; k <= x.i; k++) {
            if (x.style[k] == 1)
                g.drawLine(x.p1[k].intValue(), x.p2[k].intValue(), x.p3[k]
                        .intValue(), x.p4[k].intValue());
            if (x.style[k] == 2)
                g.drawOval(x.p1[k].intValue(), x.p2[k].intValue(), x.p3[k]
                        .intValue(), x.p4[k].intValue());
        }
    
    }
    

    }

    [/code]

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 slaris 系统断电后,重新开机后一直自动重启
  • ¥15 51寻迹小车定点寻迹
  • ¥15 谁能帮我看看这拒稿理由啥意思啊阿啊
  • ¥15 关于vue2中methods使用call修改this指向的问题
  • ¥15 idea自动补全键位冲突
  • ¥15 请教一下写代码,代码好难
  • ¥15 iis10中如何阻止别人网站重定向到我的网站
  • ¥15 滑块验证码移动速度不一致问题
  • ¥15 Utunbu中vscode下cern root工作台中写的程序root的头文件无法包含
  • ¥15 麒麟V10桌面版SP1如何配置bonding