Tabela wygląda tak:
CREATE TABLE TKluby (
id INT not null AUTO_INCREMENT,
nazwa VARCHAR (75) not null,
adreswww VARCHAR (75) not null,
godlo VARCHAR (50) not null,
PRIMARY KEY (id),
INDEX (id, nazwa),
UNIQUE (id)
)
Plik Main.java:
package pl.rozl.testy;
import java.sql.*;
public class Main {
/**
* @param args
*/
public static void main(String[] args) {
Connection connection = null;
try {
// Load the JDBC driver
String driverName = "org.gjt.mm.mysql.Driver"; // MySQL MM JDBC driver
Class.forName(driverName);
// Create a connection to the database
String serverName = "mysql4.yoyo.pl";
String mydatabase = "nazwa_bazy";
String url = "jdbc:mysql://" + serverName + "/" + mydatabase; // a JDBC url
String username = "nazwa_usera";
String password = "moje_haslo";
connection = DriverManager.getConnection(url, username, password);
Statement stmt = connection.createStatement();
ResultSet rs = stmt.executeQuery("select nazwa from TKluby");
rs.next();
System.out.println(rs.getString("adreswww"));
int n;
n = stmt.executeUpdate("INSERT INTO TKluby(nazwa, adreswww, godlo) VALUES('afds', 'fds', 'fdsafa')");
connection.close();
} catch (ClassNotFoundException e) {
// Could not find the database driver
} catch (SQLException e) {
// Could not connect to the database
}
}
}
Dlaczego w konsoli nic się nie wyświetla ani nie dodaje się rekord do bazy danych?
Błędów też żadnych nie dostaję czyli połączenie jest chyba OK?
Edit. Teraz sprawdziłem że dostaje wyjątek SQLException. O co chodzi?