【发布时间】:2023-02-08 22:07:13
【问题描述】:
我是 java 和 sql 的新手,我正在尝试连接到 postgresql 程序,但出现错误并且没有任何反应。我不知道出了什么问题
Unused import statement
Class 'ConnectPG' is never used
Method 'connectBD()' is never used
Method 'disconnection(java.sql.Connection)' is never used
我有两个 java 类文件,一个“ConnectPG”我想通过它连接到 postgresql,文件“insertRecordExample”通过它我尝试将值输入到表中。但没有任何效果。当我在“insertRecordExample”文件上开始单独调试时,程序出现错误:
“ 16:53:59:执行“:app:InsertRecordExample.main()”...... 在此处输入代码`FAILURE:构建因异常而失败。
块引用
-
在哪里: 初始化脚本 'C:\Users\fff\AppData\Local\Temp\InsertRecordExample_main__.gradle' 行:41
-
什么地方出了错: 配置项目“:app”时出现问题。 无法创建任务 ':app:InsertRecordExample.main()'。 找不到名称为“main”的 SourceSet。
-
尝试: 使用 --stacktrace 选项运行以获取堆栈跟踪。 使用 --info 或 --debug 选项运行以获得更多日志输出。 使用 --scan 运行以获得完整的见解。
-
在https://help.gradle.org 获得更多帮助
在 110 毫秒内构建失败 此构建中使用了已弃用的 Gradle 功能,使其与 Gradle 8.0 不兼容。 您可以使用“--warning-mode all”来显示各个弃用警告并确定它们是否来自您自己的脚本或插件。 见https://docs.gradle.org/7.4/userguide/command_line_interface.html#sec:command_line_warnings 16:54:00:执行完成 ':app:InsertRecordExample.main()'。 “
Java 文件连接PG:
package com.example.a112new;
import java.sql.Connection;
import java.sql.Driver;
import java.sql.DriverManager;
public class ConnectPG {
Connection connect=null;
public Connection connectBD(){
try{
Class.forName("org.postgresql.Driver");
// localhost
connect=DriverManager.getConnection( "jdbc:postgresql://127.0.0.1:5432/112", "postgresql", "430890");
}catch (Exception er) {
System.err.println(er.getMessage());
}
return connect;
}
//
protected void disconnection(Connection con)throws Exception {
con.close();
}
}
Java 文件插入记录示例:
package com.example.a112new;
//package net.javaguides.postgresql.tutorial;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
/**
* Insert PrepareStatement JDBC Example
*
* @author Ramesh Fadatare
*
*/
public class InsertRecordExample {
//localhost
private final String url = "jdbc:postgresql://127.0.0.1:5432/112";
private final String user = "postgres";
// root
private final String password = "111111";
private static final String INSERT_USERS_SQL = "INSERT INTO users" +
" (user_id, lastname, firstname, patronymic, birth,phone,email,password) VALUES
" +
" (?, ?, ?, ?, ?, ?, ?, ?);";
public static void main(String[] argv) throws SQLException {
InsertRecordExample createTableExample = new InsertRecordExample();
createTableExample.insertRecord();
}
public void insertRecord() throws SQLException {
System.out.println(INSERT_USERS_SQL);
// Step 1: Establishing a Connection
try (Connection connection = DriverManager.getConnection(url, user, password);
// Step 2:Create a statement using connection object
PreparedStatement preparedStatement = connection.prepareStatement(INSERT_USERS_SQL)) {
preparedStatement.setInt(1, 1);
preparedStatement.setString(2, "FFFF");
preparedStatement.setString(3, "FFFF");
preparedStatement.setString(4, "FFFFF");
preparedStatement.setString(5, "2005-01-12");
preparedStatement.setString(6, "+79888888888");
preparedStatement.setString(7, "11111@gmail.com");
preparedStatement.setString(8, "1234567");
System.out.println(preparedStatement);
// Step 3: Execute the query or update query
preparedStatement.executeUpdate();
} catch (SQLException e) {
// print SQL exception information
printSQLException(e);
}
// Step 4: try-with-resource statement will auto close the connection.
}
public static void printSQLException(SQLException ex) {
for (Throwable e: ex) {
if (e instanceof SQLException) {
e.printStackTrace(System.err);
System.err.println("SQLState: " + ((SQLException) e).getSQLState());
System.err.println("Error Code: " + ((SQLException) e).getErrorCode());
System.err.println("Message: " + e.getMessage());
Throwable t = ex.getCause();
while (t != null) {
System.out.println("Cause: " + t);
t = t.getCause();
}
}
}
}}
请帮助我了解我做错了什么。 我试图通过 logcat "--warning-mode=all" 来确定问题,但是没有用,它根本不给出任何错误。当我运行它时,只给出错误 InsertRecordExample !如果我运行整个程序,不会有这样的错误,只有我上面描述的错误。我提前为我的英语道歉。
【问题讨论】:
标签: java android sql postgresql gradle