Created
February 15, 2017 15:17
-
-
Save stivenson/6db6312788aa333edd070b53e7a338a7 to your computer and use it in GitHub Desktop.
Conection Postgresql with jdbc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.sql.Connection; | |
import java.sql.DriverManager; | |
import java.sql.Statement; | |
import java.sql.ResultSet; | |
import javax.sql.rowset.CachedRowSet; | |
import com.sun.rowset.CachedRowSetImpl; | |
/** | |
* | |
* @author beastieux | |
*/ | |
public class Ejm12_1_ConectarPostgreSQL { | |
public CachedRowSet Function(String sql) | |
{ | |
try | |
{ | |
Class.forName("org.postgresql.Driver"); | |
String url = "jdbc:postgresql://127.0.0.1:5432/postgres"; | |
Connection con = DriverManager.getConnection(url, "usuario","contraseña"); | |
Statement s = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, | |
ResultSet.CONCUR_READ_ONLY); | |
ResultSet rs= s.executeQuery(sql); | |
CachedRowSet crs = new CachedRowSetImpl(); | |
crs.populate(rs); | |
rs.close(); | |
s.close(); | |
con.close(); | |
return crs; | |
} | |
catch(Exception e) | |
{ | |
System.out.println(e.getMessage()); | |
} | |
return null; | |
} | |
public void StoreProcedure(String sql) | |
{ | |
try | |
{ | |
Class.forName("org.postgresql.Driver"); | |
String url = "jdbc:postgresql://127.0.0.1:5432/postgres"; | |
Connection con = DriverManager.getConnection(url, "usuario","contraseña"); | |
Statement s = con.createStatement(); | |
s.execute(sql); | |
s.close(); | |
con.close(); | |
} | |
catch(Exception e) | |
{ | |
System.out.println(e.getMessage()); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment