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

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

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

问题

  1. import java.sql.Connection;
  2. import java.sql.DriverManager;
  3. import java.sql.Statement;
  4. public class ConnectingSQL {
  5. public Connection connection(String dbname, String user, String pass){
  6. Connection conn = null;
  7. try{
  8. Class.forName("org.postgresql.Driver");
  9. conn = DriverManager.getConnection("jdbc:postgresql://localhost:5432/"+dbname,user,pass);
  10. if(conn!=null){
  11. System.out.println("Connection Established");
  12. }else{
  13. System.out.println("Connection failed.");
  14. }
  15. }
  16. catch (Exception e){
  17. System.out.println(e);
  18. }
  19. return conn;
  20. }
  21. public void createTable(Connection conn, String table_name){
  22. Statement statement;
  23. try{
  24. //the problem is here
  25. 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))";
  26. statement = conn.createStatement();
  27. statement.executeUpdate(query);
  28. System.out.println("Table created");
  29. }
  30. catch (Exception e){
  31. System.out.println(e);
  32. }
  33. }
  34. }
英文:
  1. import java.sql.Connection;
  2. import java.sql.DriverManager;
  3. import java.sql.Statement;
  4. public class ConnectingSQL {
  5. public Connection connection(String dbname, String user, String pass){
  6. Connection conn = null;
  7. try{
  8. Class.forName("org.postgresql.Driver");
  9. conn = DriverManager.getConnection("jdbc:postgresql://localhost:5432/"+dbname,user,pass);
  10. if(conn!=null){
  11. System.out.println("Connection Established");
  12. }else{
  13. System.out.println("Connection failed.");
  14. }
  15. }
  16. catch (Exception e){
  17. System.out.println(e);
  18. }
  19. return conn;
  20. }
  21. public void createTable(Connection conn, String table_name){
  22. Statement statement;
  23. try{
  24. //the problem is here
  25. 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))";
  26. statement = conn.createStatement();
  27. statement.executeUpdate(query);
  28. System.out.println("Table created");
  29. }
  30. catch (Exception e){
  31. System.out.println(e);
  32. }
  33. }
  34. }

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:

确定