Aqui un pequeño ejemplo ademas de conexion a base de datos mediante java y sql.Connection:
Todo en uno :p
package ejecutaexes; import java.sql.*; public class connectURL { static String connectionUrl = "jdbc:sqlserver://VOSTRO-PC\\SQLEXPRESS;DataBaseName=PruebaDB"; public static void main(String[] args) { // Declare the JDBC objects. Connection con = null; Statement stmt = null; ResultSet rs = null; try { // Establecemos la conexion con = newConnection(); // Creamos y ejecutamos un SQL statement que regresa las personas String SQL = "select Clave, Nombre from Personas"; stmt = con.createStatement(); rs = stmt.executeQuery(SQL); // Iterate el resulset... while (rs.next()) { System.out.println(rs.getString("Clave") + " " + rs.getString("Nombre")); } } // Handle any errors that may have occurred. catch (Exception e) { e.printStackTrace(); } finally { if (rs != null) try { rs.close(); } catch(Exception e) {} if (stmt != null) try { stmt.close(); } catch(Exception e) {} if (con != null) try { con.close(); } catch(Exception e) {} } } public static Connection newConnection() throws SQLException, ClassNotFoundException { Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver"); Connection conn = null; conn = DriverManager.getConnection ( connectionUrl, "sa", "." ); return conn; } }
........
Una vez ejecutada la aplicación, podemos ver el resultado:
No hay comentarios.:
Publicar un comentario
Escribe tu comentario