PostgreSQL Datenbank.
Habe mal folgende Methode zum einfügen eines Datensatzes geschrieben:
Daten werde auch richtig eingefügt, aber er meckert dies an:
Exception in thread "main" java.sql.SQLException: No ResultSet was produced
at sun.jdbc.odbc.JdbcOdbcPreparedStatement.executeQuery(JdbcOdbcPreparedStatement.java:97)
at Postgres.datenEinfügen(Postgres.java:104)
at Postgres.main(Postgres.java:120)
???:L
Habe mal folgende Methode zum einfügen eines Datensatzes geschrieben:
Code:
import java.sql.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Postgres
{
int summe = 0;
int ergebnis = 0;
private Connection con;
ResultSet rs;
PreparedStatement st;
ResultSet result;
PreparedStatement statement;
ResultSet res;
PreparedStatement stat;
public void verbindungHerstellen() throws SQLException, ClassNotFoundException
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
String url = "jdbc:odbc:PostgreSQL";
con = DriverManager.getConnection(url, "user", "passwd");
}
public void nameAbfragen() throws SQLException, ClassNotFoundException
{
// 2. Anfrage
statement = con.prepareStatement("Select name from bank");
// 2. Ergebnis
result = statement.executeQuery();
int numberCols = result.getMetaData().getColumnCount();
while (result.next())
{
for(int i = 1; i <= numberCols; i++)
System.out.print(result.getString(i) + "");
System.out.println();
}
result.close();
statement.close();
con.close();
}
public void guthabenAbfragen() throws SQLException, ClassNotFoundException
{
// 1. Anfrage
st = con.prepareStatement("Select guthaben from DB");
// 1. Ergebnis
rs = st.executeQuery();
int numCols = rs.getMetaData().getColumnCount();
while(rs.next())
{
for(int j = 1; j <= numCols; j++)
System.out.print(rs.getString(j) + "" );
System.out.println();
}
rs. close();
st.close();
con.close();
}
public void datenEinfügen() throws SQLException, ClassNotFoundException
{
stat = con.prepareStatement("INSERT INTO finanzdaten VALUES (150, 30, 90)");
res = stat.executeQuery();
int numCols = res.getMetaData().getColumnCount();
res. close();
stat.close();
con.close();
}
public void guthabenSumme() throws SQLException, ClassNotFoundException
{
// 1. Anfrage
st = con.prepareStatement("Select guthaben from bank");
// 1. Ergebnis
rs = st.executeQuery();
int numCols = rs.getMetaData().getColumnCount();
while(rs.next())
{
for(int j = 1; j <= numCols; j++)
{
summe = summe + rs.getInt(j);
}
}
System.out.println(summe);
rs. close();
st.close();
con.close();
}
public static void main (String [] args) throws SQLException, ClassNotFoundException
{
Postgres test = new Postgres();
test.verbindungHerstellen();
//test.nameAbfragen();
//test.guthabenAbfragen();
//test.guthabenSumme();
test.datenEinfügen();
}
}
Daten werde auch richtig eingefügt, aber er meckert dies an:
Exception in thread "main" java.sql.SQLException: No ResultSet was produced
at sun.jdbc.odbc.JdbcOdbcPreparedStatement.executeQuery(JdbcOdbcPreparedStatement.java:97)
at Postgres.datenEinfügen(Postgres.java:104)
at Postgres.main(Postgres.java:120)
???:L