Skip to main content

Conexion MySQL con driver Spring



Una de las maneras más recomendables de hacer conexiones a una base de datos es con los data source. Teniendo las librerías de Spring y el "MySQL JDBC Driver", a continuación el código:
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.DriverManagerDataSource;

//OJO: En Netbeans ya están todas las librerías, se importan las dos de Spring y la de MySQL JDBC

//Conexion
DriverManagerDataSource dataSource = new DriverManagerDataSource();
dataSource.setDriverClassName("org.gjt.mm.mysql.Driver");
dataSource.setUrl("jdbc:mysql://localhost/desarrollo");
 //donde localhost/desarrollo es donde tengan montanda la base de datos
dataSource.setUsername("root"); dataSource.setPassword("root");

Para actualizar, borrar o insertar: jdbcTemplate.update("update dummy set col=3; ")

Para consultar:
        String sql = "SELECT id,titulo,texto FROM NOTA";

        List<Map> rows = jdbcTemplate.queryForList(sql);
        for (Map row : rows) {
            Nota nota = new Nota();
            nota.setId((Integer) (row.get("id")));
            nota.setTitulo((String) row.get("titulo"));
            nota.setTexto((String) row.get("texto"));

        }

   
 



Comments

Popular posts from this blog

Configurar pantalla de inicio de sesión Ubuntu

Configurar pantalla de inicio de sesión Si quieres personalizar el inicio de sesión (pantalla de logeo), puedes instalar un programa llamado Ubuntu Tweak muy intuitivo y que te permite modificar el fondo de la pantalla de logeo entre otras cosas, primero vamos a instalar Ubuntu Tweak: sudo add-apt-repository ppa:tualatrix/ppa  sudo apt-get update  sudo apt-get install ubuntu-tweak  Una vez instalado, al entrar en System Settings deberia aparecerte el icono de Ubuntu Tweak: Luego de darle click a Ubuntu Tweak, dale click a la pestaña de "Tweaks" y a Login Settings: Para modificar la pantalla de Login se requiere permisos de superuser, dale a la opcion de "Unlock" para que puedas realizar cambios: Le das click a la imagen para cambiar el background y al Gtk theme, por ejemplo; mi resultado fue: Espero haber sido de ayuda. Fue hecho en: Ubuntu 12.04

Solving Groovy compiler in GGTS (Groovy: compiler mismatch)

Hello world, So I was trying to run a Grails 2.3.5 project in my recently installed GGTS 3.4, and I was getting the next error: Description    Resource    Path    Location    Type Groovy: compiler mismatch Project level is: 2.1 Workspace level is 2.0 Groovy compiler level expected by the project does not match workspace compiler level. I couldn't find the Groovy Compiler 2.1 and didn't know how to install it. Here is how you do it: " ...To install the Groovy 2.1 compiler in GGTS, first click on the Dashboard tab. If it is not visible, goto Help -> Dashboard. at the bottom left of the window, click Extensions. Scroll down the list and check Groovy 2.1 Compiler for Groovy-Eclipse. Then click Install on the lower right of the window. Follow the defaults to install, and it will configure itself. I have found it cumbersome to get existing projects to fully correct themselves, so I usually just delete the project an...

Change Unity in ubuntu: 2 Steps

If you like other elements of Ubuntu, but you don't like the user interface (desktop environment or login screen) or if you want to try a different UI, you don't need to change to another distro! You could install a new desktop environment or configure the new login screen. Also, you can get back to Unity if you like it better, every time you log in you will be able to choose between the installed desktop environments. Steps: Step 1 .  First, you need to install the desktop environment: For Gnome Shell(gnome3 ): sudo apt-get install gnome-shell sudo apt-get install gnome-tweak-tool Clasic Gnome (gnome2): sudo apt-get install gnome-panel KDE: sudo apt-get install kde-standard or sudo apt-get install kubuntu-desktop (Taken from: http://kde.org/announcements/announce_4.2-beta1/desktop.png) Light and fast XFCE: sudo apt-get install xfce4 (Taken from: https://es.wikipedia.org/wiki/Xfce ) A fork of Gnome 2 (MATE): sudo add-apt-r...