当我运行代码时,它并没有达到我的期望

时间:2018-12-09 13:27:59

标签: java netbeans

我试图在main方法中调用构造函数,但我不断遇到错误, 我不知道为什么会有这个问题,如果不调用构造函数,程序将运行,但不显示任何内容,当我尝试将其命名为new EmailLists()时;它还会导致需要String的错误,我尝试在其中添加String,但仍然无法正常工作,

  private BufferedReader br;
  private Connection conn;
  private Statement stmt;
  private String emailadd;
  private String accname;
  private Random rc;
  private String[] acc_name = {"John michael","Christopher Binlayan","Lancelot Binlayan","Jerry Damoyan","Jonathan Kedawen"};
  private Integer numlength = acc_name.length;
  private Integer useracc;


public EmailLists(String username,String Password)throws Exception
{
  username = "root";
  Password = "root";
  Class.forName(Driver1);
  Class.forName(Driver2);
  conn =DriverManager.getConnection(URL,username,Password);
  stmt = conn.createStatement();
  br = new BufferedReader(new InputStreamReader(System.in));
  rc = new Random();

  start();

    }
    public void start()throws Exception
    {
        useracc = rc.nextInt(numlength);
        System.out.println("Enter Email Address to be notified:");
        emailadd=br.readLine();

          String insertinto =("Insert Into EMAIL_LIST(EMAIL ADDRESS) VALUES ('"+emailadd+"')");
          stmt.executeUpdate(insertinto);
          System.out.println("Succesfully Added!! Welcome Username: "+acc_name[useracc]+"");

    }


public static void main(String[] args){
   new EmailLists(String username,String password);



}

}

1 个答案:

答案 0 :(得分:0)

您已写

DriverManager.getConnection(URL,username,Password)

,并且编译良好。注意,您所做的只是传递您想要的参数。我建议您对构造函数

做同样的事情
new EmailLists(username, password);

即只需重复已经起作用的内容即可。

顺便说一句,必须先将这些变量设置为某种值,否则您可以给它们赋值。

new EmailLists("myusername", "mypassword");

注意由于您在构造函数中定义了这些值,因此无需传递它们。

public EmailLists() throws Exception
{
  String username = "root";
  String password = "root";

然后致电

new EmailLists();