Skip to content

Instantly share code, notes, and snippets.

@stivenson
Created February 15, 2017 15:21
Show Gist options
  • Save stivenson/388880361d9f030fa51ea8055ecac4c4 to your computer and use it in GitHub Desktop.
Save stivenson/388880361d9f030fa51ea8055ecac4c4 to your computer and use it in GitHub Desktop.
Conection mysql with jdbc
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import javax.sql.rowset.CachedRowSet;
import com.sun.rowset.CachedRowSetImpl;
/**
*
* @author beastieux
*/
public class Ejm12_2_ConectarMySQL {
public CachedRowSet Function(String sql)
{
try
{
Class.forName("org.gjt.mm.mysql.Driver");
String url = "jdbc:mysql://127.0.0.1:3306/mysql";
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.gjt.mm.mysql.Driver");
String url = "jdbc:mysql://127.0.0.1:3306/mysql";
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