找不到符号java和JDBC

时间:2016-10-16 06:29:47

标签: java jdbc prepared-statement

我已经尝试过堆栈溢出的解决方案,但它没有解决我的问题,所以我想问一下

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 个答案:

答案 0 :(得分:0)

您的代码存在很多问题,其中一些可以避免1)仔细审查您的代码和2)使用IDE。

一些问题:

  1. 您有拼写错误,例如con.preparedStatement而不是con.prepareStatement
  2. 您需要在对象上调用setXXX方法,例如pstmnt
  3. 您需要了解Java中的块范围,例如,在while块的块中声明的变量ans不能在该块之外使用,
  4. 您尝试使用尚未声明的变量名称,例如inner1