import java.sql.*;
import java.io.*;
public class prog1 {
public static void main(String args[]) {
String dbname = "BD22";
String dbuser = "postgres";
String password = "12345";
String url = "jdbc:postgresql://localhost:5432/" + dbname;
try {
BufferedReader in;
in = new BufferedReader( new InputStreamReader( System.in ));
System.out.println("Insira um novo passageiro");
System.out.print(">> Nome: "); String nomePassageiro = in.readLine();
System.out.print(">> Morada: "); String destinopretendido = in.readLine();
System.out.println("Nome: " + nomePassageiro + " Morada: " + destinopretendido);
Connection c = DriverManager.getConnection(url);
c.setAutoCommit(false);
Statement stmt = c.createStatement();
String sql = "INSERT INTO passageiros(nomeP,destinopretendido) VALUES ('" +
nomePassageiro + "','" + destinopretendido + "');";
stmt.executeUpdate(sql);
stmt.close();
c.commit();
c.close();
System.out.println("Estudio " + nomePassageiro + " inserido.");
}
catch (Exception e) {
System.err.println( e.getClass().getName()+": "+ e.getMessage() );
System.exit(0);
}
}
}
当我启动代码时,我可以编写所有内容,但当程序启动“阶段”(在该阶段中,它应该将所编写的内容添加到数据库中)时,标题错误出现,当我调试它时,它出现:已断开与目标VM的连接,地址:“127.0.0.1:54160”,运输:“插座”有人能帮我吗?
1条答案
按热度按时间k2arahey1#
我解决了这个问题,把第18行(其中有命令-连接c =驱动管理器.getConnection(url);)下面的命令- Connection c =驱动器管理器.getConnection(url,dbuser,密码);