为什么在这个Java项目中我会得到一个语法错误?

huangapple go评论54阅读模式
英文:

why do i get a syntax error in this java project?

问题

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;

public class ConnectingSQL {

    public Connection connection(String dbname, String user, String pass){
        Connection conn = null;
        try{
            Class.forName("org.postgresql.Driver");
            conn = DriverManager.getConnection("jdbc:postgresql://localhost:5432/"+dbname,user,pass);
            if(conn!=null){
                System.out.println("Connection Established");
            }else{
                System.out.println("Connection failed.");
            }
        }
        catch (Exception e){
            System.out.println(e);
        }
        return conn;
    }

    public void createTable(Connection conn, String table_name){
        Statement statement;
        try{
            //the problem is here
            String query = "CREATE TABLE "+ table_name+" (id SERIAL PRIMARY KEY, first_name VARCHAR(100), last_name VARCHAR(100), phone_number int(50), departament_code int(20), email_address VARCHAR(200), password VARCHAR(50))";
            statement = conn.createStatement();
            statement.executeUpdate(query);
            System.out.println("Table created");
        }
        catch (Exception e){
            System.out.println(e);
        }
    }
}
英文:
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;

public class ConnectingSQL {

    public Connection connection(String dbname, String user, String pass){
        Connection conn = null;
        try{
            Class.forName("org.postgresql.Driver");
            conn = DriverManager.getConnection("jdbc:postgresql://localhost:5432/"+dbname,user,pass);
            if(conn!=null){
                System.out.println("Connection Established");
            }else{
                System.out.println("Connection failed.");
            }
        }
        catch (Exception e){
            System.out.println(e);
        }
        return conn;
    }

    public void createTable(Connection conn, String table_name){
        Statement statement;
        try{
        //the problem is here
            String query ="CREATE TABLE "+ table_name+" (id SERIAL PRIMARY KEY, first_name VARCHAR(100), last_name VARCHAR(100), phone_number int(50), departament_code int(20), email_address VARCHAR(200), password VARCHAR(50))";
            statement = conn.createStatement();
            statement.executeUpdate(query);
            System.out.println("Table created");
        }
        catch (Exception e){
            System.out.println(e);
        }
    }
}

This is my first time trying to connect a database to my java project so i tried to follow a youtube video and even though i followed it exactly i get this error: syntax error at or near "(", i tried to change everything around it but i still get the same error.

答案1

得分: 1

我认为错误出在你的查询中,尽管我在你发布的代码中没有看到对 createTable 方法的调用。

在 PostgreSQL 中,移除 int 的长度参数,因为它们不被接受。只需使用 int 而不是 int(n)

英文:

I think the error is in your query, even though I don't see a call to the createTable method in the code you posted.

Remove the length parameters from int as they are not accepted in PostgreSQL. Simply use int instead of int(n).

huangapple
  • 本文由 发表于 2023年6月19日 16:23:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/76504870.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定