java.lang.NullPointerException postgresql

java.lang.NullPointerException postgresql,java,mysql,sql,nullpointerexception,null,Java,Mysql,Sql,Nullpointerexception,Null,我在修复java.lang.NullPointerException错误时遇到问题: 我可以连接到数据库没有任何问题,但我看不到表或删除任何表(案例1,2)。Eclipse准确地向我显示了错误集中在st=connection.createStatement()的代码部分

我在修复java.lang.NullPointerException错误时遇到问题:

我可以连接到数据库没有任何问题,但我看不到表或删除任何表(案例1,2)。Eclipse准确地向我显示了错误集中在
st=connection.createStatement()的代码部分
这是我的密码:

public class projekt {

    public static Connection getConnect(){

        try {
            Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
        } catch (ClassNotFoundException e) {
            System.out.println("Nie odnaleziono sterownika JDBC?");
            e.printStackTrace();
            return null;
        }

        System.out.println("");
        Connection connection = null;

        try {
            connection = DriverManager
            .getConnection("jdbc:sqlserver://eos.inf.ug.edu.pl;databaseName=lrosiak;user=lrosiak;password=206268;");


        } catch (SQLException e) {
            System.out.println("Blad polaczenia");
            e.printStackTrace();
            return null;
        }

        if (connection != null) {
            System.out.println("Poprawnie polaczono!");
            return connection;

        } else {
            System.out.println("Blad polaczenia!");
            return null;
        }


    }


@SuppressWarnings("resource")
public static int menu(){
    System.out.println();
    System.out.println("     1. Wyswietl tabele");
    System.out.println("     2. Dodaj rekord");
    System.out.println("     3. Usun rekord");
    System.out.println("     4. Modyfikuj rekord");
    System.out.println("     0. Koniec");

    Scanner in = new Scanner(System.in);
    int w = in.nextInt();

    return w;
}


@SuppressWarnings("resource")
public static void main(String[] args) throws IOException, SQLException {
    // TODO Auto-generated method stub
    Connection connection = connect();

    Statement st = null;
    ResultSet rs = null;
    new Scanner(System.in);

    int wybor = menu();

    while(wybor!=0){
        switch(wybor){
            case 1:
                        System.out.println("Podaj nazwe tabeli (osoba, miasto, kraj, zawód):");
                        Scanner input = new Scanner(System.in);
                        String nazwa = input.nextLine();

                    if(nazwa.equals("osoba"))
                    {
                     st = connection.createStatement();
                     rs = st.executeQuery("SELECT osoba_id, osoba_imie, osoba_nazwisko, miasto_id, kraj_id, zawód_id FROM osoba");

                     System.out.println(String.format("%-15s%-15s%-15s%-15s%-15s%-15s%", "id", "imie", "nazwisko", "miasto", "kraj", "zawód"));
                     System.out.println();
                     while (rs.next()) {

                         System.out.println(String.format("%-15d%-15s%-15s%-15s%-15s%-15s%",rs.getInt(1), rs.getString(2), rs.getString(3), rs.getString(4), rs.getString(5), rs.getString(6)));

                     }
                    rs.close();
                    st.close();
                    }
                    else if(nazwa.equals("miasto"))
                    {
                    st = connection.createStatement();
                     rs = st.executeQuery("SELECT miasto_id, miasto_nazwa, miasto_kod FROM miasto");

                     System.out.println(String.format("%-15s%-15s%-15s%", "id", "nazwa", "kod"));
                     System.out.println();
                     while (rs.next()) {

                         System.out.println(String.format("%-15d%-15s%-15s%",rs.getInt(1), rs.getString(2), rs.getString(3)));

                     }
                    rs.close();
                    st.close();
                    }
                    else if(nazwa.equals("kraj"))
                    {
                         st = connection.createStatement();
                         rs = st.executeQuery("SELECT kraj_id, kraj_nazwa, kraj_kod FROM kraj");

                         System.out.println(String.format("%-15s%-15s%-15s%", "kraj", "nazwa", "kod"));
                         System.out.println();
                         while (rs.next()) {

                             System.out.println(String.format("%-15d%-15d%-15d%",rs.getInt(1), rs.getInt(2), rs.getInt(3)));

                         }  

                     rs.close();
                     st.close();
                    }   
                    else if(nazwa.equals("zawód"))
                    {
                         st = connection.createStatement();
                         rs = st.executeQuery("SELECT zawód_id, zawód_rodzaj, zawód_stanowisko, zawód_pensja FROM zawód");

                         System.out.println(String.format("%-15s%-15s%-15s%-15s", "zawód", "rodzaj", "stanowisko", "pensja"));
                         System.out.println();
                         while (rs.next()) {

                             System.out.println(String.format("%-15d%-15d%-15d%-15s",rs.getInt(1), rs.getInt(2), rs.getInt(3), rs.getDate(4)));

                         }  

                     rs.close();
                     st.close();
                    }
                    else
                    {
                    System.out.println("Podales zla nazwe tabeli");
                    } 


                    break;

            case 2:

                    System.out.println("Podaj nazwe tabeli (osoba, miasto, kraj, zawód):");
                    Scanner input1 = new Scanner(System.in);
                    nazwa = input1.nextLine();
                    st = connection.createStatement();

                    if(nazwa.equals("osoba"))
                    {
                        System.out.println("Podaj imie:");
                        Scanner input2 = new Scanner(System.in);
                        String imie = input2.nextLine();
                        System.out.println("Podaj nazwisko:");
                        input2 = new Scanner(System.in);
                        String nazwisko = input2.nextLine();
                        System.out.println("Podaj miasto:");
                        input2 = new Scanner(System.in);
                        String miasto = input2.nextLine();
                        System.out.println("Podaj kraj:");
                        input2 = new Scanner(System.in);
                        String kraj = input2.nextLine();
                        System.out.println("Podaj zawód:");
                        input2 = new Scanner(System.in);
                        String zawód = input2.nextLine();


                     st.executeUpdate("INSERT INTO osoba(osoba_imie, osoba_nazwisko, miasto, kraj, zawód) VALUES('"+imie+"', '"+nazwisko+"', '"+miasto+"', '"+kraj+"', '"+zawód+"')");
                     st.close();        
                    }
                    else if(nazwa.equals("miasto"))
                    {
                        System.out.println("Podaj nazwę:");
                        Scanner input2 = new Scanner(System.in);
                        String nazwa1 = input2.nextLine();
                        System.out.println("Podaj kod:");
                        input2 = new Scanner(System.in);
                        String kod = input2.nextLine();                         

                     st.executeUpdate("INSERT INTO miasto(miasto_nazwa, kod) VALUES('"+nazwa1+"', '"+kod+"')");
                     st.close();
                    } 
                    else if(nazwa.equals("kraj"))
                    {
                        System.out.println("Podaj id kraju:");
                        Scanner input2 = new Scanner(System.in);
                        String kraj_id = input2.nextLine();
                        System.out.println("Podaj nazwę:");
                        input2 = new Scanner(System.in);
                        String nazwa2 = input2.nextLine();
                        System.out.println("Podaj kod:");
                        input2 = new Scanner(System.in);
                        String kod = input2.nextLine();


                      st.executeUpdate("INSERT INTO kraj (kraj_id, nazwa, kod) VALUES("+kraj_id+", "+nazwa2+", '"+kod+"')");

                         st.close();    
                    }
                    else if(nazwa.equals("zawód"))
                    {
                        System.out.println("Podaj rodzaj:");
                        Scanner input2 = new Scanner(System.in);
                        String rodzaj = input2.nextLine();
                        System.out.println("Podaj stanowisko:");
                        input2 = new Scanner(System.in);
                        String stanowisko = input2.nextLine();  
                        System.out.println("Podaj pensję:");
                        input2 = new Scanner(System.in);
                        String pensja = input2.nextLine();

                     st.executeUpdate("INSERT INTO zawód(rodzaj, stanowisko, pensja) VALUES('"+rodzaj+"', '"+stanowisko+"', '"+pensja+"')");
                     st.close();
                    }
                    else
                    System.out.println("Podales zla nazwe tabeli");                     

                    break;

            case 3:

                System.out.println("Podaj nazwe tabeli (osoba, miasto, kraj, zawód):");
                 input1 = new Scanner(System.in);
                nazwa = input1.nextLine();


                    st = connection.createStatement();
                    if(nazwa.equals("osoba"))
                    {
                        System.out.println("Podaj id usuwanego wiersza:");
                        input1 = new Scanner(System.in);
                        String id = input1.nextLine();  
                    st.executeUpdate("DELETE FROM "+nazwa+" WHERE osoba_id="+id+"");
                    }
                    else if(nazwa.equals("miasto"))
                    {                  
                        System.out.println("Podaj id usuwanego wiersza:");
                        input1 = new Scanner(System.in);
                        String id = input1.nextLine();  
                    st.executeUpdate("DELETE FROM "+nazwa+" WHERE miasto_id="+id+"");
                    }
                    else if(nazwa.equals("kraj"))
                    {
                        System.out.println("Podaj id usuwanego wiersza:");
                        input1 = new Scanner(System.in);
                        String id = input1.nextLine();  
                       st.executeUpdate("DELETE FROM "+nazwa+" WHERE kraj_id="+id+""); 
                    }
                    else if(nazwa.equals("zawód"))
                    {
                        System.out.println("Podaj id usuwanego wiersza:");
                        input1 = new Scanner(System.in);
                        String id = input1.nextLine();  
                       st.executeUpdate("DELETE FROM "+nazwa+" WHERE zawód_id="+id+""); 
                    }
                    else
                    System.out.println("Podales zla nazwe tabeli"); 
                    st.close();  
                break;

            case 4:

                break;
        }

        System.out.println("\nWciśnij Enter, aby kontynuować...");
        System.in.read();

        wybor = menu();
    }

    System.out.println("     ******************");
    System.out.println("\n     Koniec\n\n");
}

private static Connection connect() {
    // TODO Auto-generated method stub
    return null;
}
}

还有我的桌子,但我认为问题不在于桌子,只是以防万一:

CREATE TABLE [kraj] ([kraj_id] int IDENTITY(1,1) NOT NULL,[kraj_nazwa] varchar(20) NULL,[kraj_kod] varchar(5) NULL,PRIMARY KEY CLUSTERED ([kraj_id])) GO

CREATE TABLE [miasto] ([miasto_id] int IDENTITY(1,1) NOT NULL,[miasto_nazwa] varchar(100)NOT NULL,[miasto_kod] varchar(5) NOT NULL,PRIMARY KEY CLUSTERED ([miasto_id]))GO

CREATE TABLE [zawód] ([zawód_id] int IDENTITY(1,1) NOT NULL,[zawód_rodzaj] varchar(100)NULL,[zawód_stanowisko] varchar(100) NULL,[zawód_pensja] varchar(5) NULL,PRIMARY KEY CLUSTERED ([zawód_id]))GO

CREATE TABLE [osoba] ([osoba_id] int IDENTITY(1, 1) NOT NULL,[osoba_imie] varchar(100) NULL,[osoba_nazwisko] varchar(100) NULL,[miasto_id] int NOT NULL REFERENCES miasto(miasto_id),[kraj_id] int NOT NULL REFERENCES kraj(kraj_id),[zawód_id] int NOT NULL REFERENCES zawód(zawód_id),PRIMARY KEY CLUSTERED ([osoba_id]))GO
您的connect()方法返回null

private static Connection connect() {
    // TODO Auto-generated method stub
    return null;
}
您需要修复此方法。比如:

private static Connection connect() {
    // TODO Auto-generated method stub
    return getConnect();
}

此问题被标记为
postgresql
,但似乎您正在连接到
MS SQL Server
DB。只需使用
Connection Connection=getConnect()
您不可能使用Postgres(或您也添加到标记中的MySQL),因为您使用的是SQL Server JDBC驱动程序,并且您的
create table
语句对Postgres无效。