package conexaopostgresql;
import java.sql.Statement;
import java.sql.Connection;
import java.sql.DriverManager;
public class Main {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
try {
String url = "jdbc:postgresql://localhost:5432/TesteJava";
String usuario = "postgres";
String senha = "123456";
Class.forName("org.postgresql.Driver");
Connection con;
con = DriverManager.getConnection(url, usuario, senha);
System.out.println("Conexão realizada com sucesso.");
Statement stm = con.createStatement();
// stm.executeQuery("INSERT INTO teste VALUES (1,'Cynthia')");
stm.executeUpdate("INSERT INTO teste VALUES (1,'Cynthia')");
//Editado 21/09/2011 para correção: executeQuery é usado para pesquisa, executeUpdate deve ser usado para inserir
con.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
Realiza a conexão com o Banco de Dados.
Be the first to comment
You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.