新手请教个问题

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个回答

你这代码问题太多了,
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]

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问