簡體   English   中英

找不到符號 java 和 JDBC

[英]Cannot find symbol java and JDBC

我已經嘗試過堆棧溢出的解決方案,但它沒有解決我的問題,所以我想問一下

import java.*;
import java.lang.*;
import java.sql.*;
import java.io.*;
import java.util.*;
public class mysql1
{
    static final String url = "jdbc:mysql://localhost:3306/test";
    static final String usr = "root";
    static final String passwd = "";
    public static void main(String [] ar)
{
        Connection con = null;
//      boolean flag = True;
        int ch;
        String y;
        try
        {
            System.out.println("connecting to db...\n");
            con = DriverManager.getConnection(url,usr,passwd);
            System.out.println("Database Connected.\n");
            CallableStatement cs = null;
            Statement stmnt = con.createStatement();
            do
            {
                Scanner in = new Scanner(System.in);
                System.out.println("1.Create Table\n");
                System.out.println("2.Insert Table\n");
                System.out.println("3.Display Table\n");
                System.out.println("4.Update Table\n");
                System.out.println("5.Delete Table\n");
                System.out.println("6.Creating index\n");
                System.out.println("7.Show index\n");
                System.out.println("8.Create View\n");
                System.out.println("9.Show view\n");
                System.out.println("10. Inner Join\n");
                System.out.println("11. Right outer join\n");
                System.out.println("12. Left outer join\n");
                System.out.println("13. Cross join\n");
                System.out.println("14. Exit\n");
                System.out.println("Enter your choice");
                ch = in.nextInt();
                switch(ch)
                {
                case 1:
                    System.out.println("Creating table 1.\n");
                    String t1 = "create table employe(id int(10),"+"fname varchar(20),"+"lname varchar(15))";
                    stmnt.executeUpdate(t1);
                    System.out.println("Table 1 Created..\n");
                    System.out.println("Creating table 2.\n");
                    String t2 = "create table salary(id int(10),"+"fname varchar(20),"+"salray int(10))";
                    stmnt.executeUpdate(t2);
                    System.out.println("Table 2 Created..\n");
                    break;
                case 2:
                    System.out.println("Entering data in 1st table\n");
                    do
                    {
                    System.out.println("Enter id:\n");
                    int id = in.nextInt();
                    String str = in.nextLine();
                    System.out.println("Enter respective fname:\n");
                    String f = in.nextLine();
                    System.out.println("Enter lname:\n");
                    String l = in.nextLine();
                    PreparedStatement pstmnt = con.preparedStatement("insert into employe values(?,?,?)");
                    pstmnt = setInt(1,id);
                    pstmnt = setString(2,f);
                    pstmnt = setString(3,f);
                    pstmnt.executeUpdate();
                    System.out.println("Do you want to continue? y/n");
                    String ans = in.nextLine();
                    }while(ans=='y');
                    System.out.println("Entering data in 2nd table\n");
                    do
                    {
                    System.out.println("Enter id:\n");
                    int id1 = in.nextInt();
                    String str1 = in.nextLine();
                    System.out.println("Enter respective fname:\n");
                    String f1 = in.nextLine();
                    System.out.println("Enter the salary\n");
                    int sal = in.nextInt();
                    PreparedStatement pstmnt = con.preparedStatement("insert into salary values(?,?,?)");
                    pstmnt = setInt(1,id);
                    pstmnt = setString(2,f);
                    pstmnt = setInt(3,f);
                    pstmnt.executeUpdate();
                    System.out.println("Do you want to continue? y/n");
                    String ans = in.nextLine();
                    }while(ans=='y');
                    break;
                case 3:
                    System.out.println("Displaying Table 1.....\n");
                    String display = "select * from employe";
                    ResultSet rs1 = stmnt.executeQuery(display);
                    System.out.println("ID \t FNAME \t LNAME");
                    while(rs1.next())
                    {
                        System.out.println(rs1.getInt(1)+"\t"+rs1.getString(2)+"\t"+rs1.getString(3));
                    }
                    System.out.println("Displaying Table 2.....\n");
                    String display1 = "select * from salary";
                    ResultSet rs4 = stmnt.executeQuery(display);
                    System.out.println("ID \t FNAME \t SALARY");
                    while(rs4.next())
                    {
                        System.out.println(rs4.getInt(1)+"\t"+rs4.getString(2)+"\t"+rs4.getInt(3));
                    }
                    break;
                case 4:
                    String updat = "update employe set fname=?, lname=? where id=?";
                    System.out.println("Enter the id of the employe that you want to update:\n");
                    int id1 = in.nextInt();
                    System.out.println("Enter new fname:\n");
                    String f1 = in.nextLine();
                    System.out.println("Enter new lname:\n");
                    String l1 = in.nextLine();
                    PreparedStatement ps = con.preparedStatement(updat);
                    ps.setString(1,f1);
                    ps.setString(2,l1);
                    ps.setInt(3,id1);
                    ps.executeUpdate();
                    break;
                case 5:
                    System.out.println("Delete enteries from table using id......\n");
                    String del = "delete from employe where id=?";
                    System.out.println("Enter the ID:\n");
                    int id2 = in.nextInt();
                    PreparedStatement ps1 = con.PreparedStatement(del);
                    ps1.setInt(1,id2);
                    ps.executeUpdate();
                    break;
                case 6:
                    System.out.println("Creating Index.....\n");
                    String ind = "create index inde on employe(fname)";
                    stmnt.executeUpdate(ind);
                    System.out.println("Index created .....n");
                    break;
                case 7:
                    System.out.println("Display index.....\n");
                    String inde = "show index from employe";
                    ResultSet rs2 = stmnt.executeQuery(inde);
                    System.out.println(rs2.getString(1)+"\t"+rs2.getString(2)+"\t"+rs2.getString(3)+"\t"+rs2.getString(4)+"\t"+rs2.getString(5)+"\t"+rs2.getString(6)+"\t"+rs2.getString(7)+"\t"+rs2.getString(8)+"\t"+rs2.getString(9)+"\t"+rs2.getString(10)+"\t"+rs2.getString(11)+"\t"+rs2.getString(12)+"\t\n");
                    break;
                case 8:
                    System.out.println("Create view......\n");
                    String vew = "create view v1 as select id,lname from employe";
                    stmnt.executeUpdate(vew);
                    System.out.println("View created\n");
                    break;
                case 9:
                    System.out.println("Displaying View......\n");
                    String disv = "select * from v1";
                    ResultSet rs3 = stmnt.executeQuery(disv);
                    while(rs3.next())
                    {
                        System.out.println(rs3.getInt(1)+"\t"+rs3.getString(2));
                    }
                    break;
                case 10:
                    String inner = "select * from employe e inner join salary sa on e.id=sa.id";
                    ResultSet rs5 = stmnt.executeQuery(inner);
                    System.out.println("ID\tFNME\tlname\n");
                    while(rs5.next())
                    {
                        System.out.println(rs5.getInt(1)+"\t"+rs5.getString(2)+"\t"+rs5.getString(3)+"\n");
                    }
                    inner1 = "select * from salary sal employe e on sal.id=e.id";
                    rs5 = stmnt.executeQuery(inner1);
                    System.out.println("ID\tFNME\tSalary\n");
                    while(rs5.next())
                    {
                        System.out.println(rs5.getInt(1)+"\t"+rs5.getString(2)+"\t"+rs5.getInt(3)+"\n");
                    }
                    break;
                case 11:
                    String rj = "select * from employe e right outer join salary s on e.id=s.id";
                    ResultSet rs6= stmnt.executeQuery(rj);
                    System.out.println("ID\tFnAME\tLNAME \n");
                    while(rs6.next())
                    {
                        System.out.println(rs6.getInt(1)+"\t"+rs6.getString(2)+"\t"+rs6.getString(3)+"\n");
                    }
                    rj = "select * from employe e right outer join salary s on e.id=s.id";
                    rs6= stmnt.executeQuery(rj);
                    System.out.println("ID\tFnAME\tLNAME \n");
                    while(rs6.next())
                    {
                        System.out.println(rs6.getInt(1)+"\t"+rs6.getString(2)+"\t"+rs6.getString(3)+"\n");
                    }
                }
                    System.out.println("Do you want to continue y/n :\n");
                    y = in.nextLine();
                }while(y=="y");
            }
            catch(SQLException e)
            {
                System.err.println("Cannot Connect to the db\n");
                e.printStackTrace();
            }
            finally
            {
                System.out.println("Closing connection.......\n");
                if(con!=null)
                {
                    try
                    {
                        con.close();
                    }
                    catch(SQLException ig)
                    {}
                }
            }
}
}

上面的代碼仍然缺少一些情況這里是錯誤

mysql1.java:66: error: cannot find symbol
                    PreparedStatement pstmnt = con.preparedStatement("insert into employe values(?,?,?)");
                                                  ^
  symbol:   method preparedStatement(String)
  location: variable con of type Connection
mysql1.java:67: error: cannot find symbol
                    pstmnt = setInt(1,id);
                             ^
  symbol:   method setInt(int,int)
  location: class mysql1
mysql1.java:68: error: cannot find symbol
                    pstmnt = setString(2,f);
                             ^
  symbol:   method setString(int,String)
  location: class mysql1
mysql1.java:69: error: cannot find symbol
                    pstmnt = setString(3,f);
                             ^
  symbol:   method setString(int,String)
  location: class mysql1
mysql1.java:73: error: cannot find symbol
                    }while(ans=='y');
                           ^
  symbol:   variable ans
  location: class mysql1
mysql1.java:84: error: cannot find symbol
                    PreparedStatement pstmnt = con.preparedStatement("insert into salary values(?,?,?)");
                                                  ^
  symbol:   method preparedStatement(String)
  location: variable con of type Connection
mysql1.java:85: error: cannot find symbol
                    pstmnt = setInt(1,id);
                                      ^
  symbol:   variable id
  location: class mysql1
mysql1.java:86: error: cannot find symbol
                    pstmnt = setString(2,f);
                                         ^
  symbol:   variable f
  location: class mysql1
mysql1.java:87: error: cannot find symbol
                    pstmnt = setInt(3,f);
                                      ^
  symbol:   variable f
  location: class mysql1
mysql1.java:91: error: cannot find symbol
                    }while(ans=='y');
                           ^
  symbol:   variable ans
  location: class mysql1
mysql1.java:119: error: cannot find symbol
                    PreparedStatement ps = con.preparedStatement(updat);
                                              ^
  symbol:   method preparedStatement(String)
  location: variable con of type Connection
mysql1.java:130: error: cannot find symbol
                    PreparedStatement ps1 = con.PreparedStatement(del);
                                               ^
  symbol:   method PreparedStatement(String)
  location: variable con of type Connection
mysql1.java:169: error: cannot find symbol
                    inner1 = "select * from salary sal employe e on sal.id=e.id";
                    ^
  symbol:   variable inner1
  location: class mysql1
mysql1.java:170: error: cannot find symbol
                    rs5 = stmnt.executeQuery(inner1);
                                             ^
  symbol:   variable inner1
  location: class mysql1
14 errors

我正在使用linux終端進行編譯。 我嘗試將類和方法名稱更改為大寫/小寫。 我還更改了類名並將整個程序復制到不同的文件中,然后進行編譯。 我還檢查了“Eclipse IDE”中的代碼,所以我也沒有發現任何錯別字。 另外我想知道如果沒有連接器是否會自動調用,那么我應該進一步添加什么?

我是初學者,這是我的第一個 JDBC 代碼,所以請幫忙。

您的代碼存在很多問題,其中一些問題可以通過 1) 仔細檢查您的代碼和 2) 使用 IDE 來避免。

一些問題:

  1. 你有錯別字,例如con.preparedStatement而不是con.prepareStatement
  2. 您需要在對象上調用setXXX方法,例如在pstmnt
  3. 您需要了解 Java 中的塊作用域,例如在 while 循環塊中聲明的變量ans在該塊之外使用,
  4. 您嘗試使用尚未聲明的變量名稱,例如inner1

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM