Detto fatto
codice:
import java.sql.*;
import java.io.*;
public class JDBCTest {
public static void main(String[] args) {
try {
System.setProperty("jdbc.drivers","com.mysql.jdbc.Driver");
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost/NOMEDATABASE?user=CAMBIAQUI&password=CAMBIAANCHEQUI");
Statement stat =conn.createStatement();
stat.execute("CREATE TABLE Greetings (Name CHAR(20))");
stat.execute("INSERT INTO Greetings VALUES ('Hello, world!')");
ResultSet result = stat.executeQuery("SELECT * FROM Greetings");
result.next();
System.out.println(result.getString(1));
result.close();
stat.execute("DROP TABLE Greetings");
stat.close();
conn.close();
}
catch(SQLException ex) {
while( ex != null) {
ex.printStackTrace();
ex.getNextException();
}
}
//catch(IOException ex) {
// ex.printStackTrace();
//}
}
}
questo e' un programmino del cavolo che uso per vedere se va la connessione al db.
Devi solo cambiare NOMEDATABASE con il nome del database al quale ti vuoi connettere (con mysql puoi usare il database "test" per fare delle prove),
CAMBIAQUI con il nome utente con cui ti colleghi al database e CAMBIAANCHEQUI con la relativa password.