英文:
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)
.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论