Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- DBItem.java
- package beans;
- import java.sql.*;
- import java.util.*;
- import java.util.logging.Level;
- import java.util.logging.Logger;
- /**
- *
- * @author elcer
- */
- public class DBItem {
- private String driver = "com.mysql.jdbc.Driver";
- private String url = "jdbc:mysql://localhost:3306/items";
- private Connection conn = null;
- public DBItem() { //el constructor crea y abre la conexion
- try {
- Class.forName(driver);
- conn = DriverManager.getConnection(url, "usuario", "password");
- if (conn != null)
- System.out.println("Conexion efectuada con exito");
- } catch (ClassNotFoundException | SQLException ex) {
- Logger.getLogger
- (DBItem.class.getName()).log(Level.SEVERE, null, ex);
- }
- }
- public LinkedList<Item> getItems(String tipo) {
- LinkedList<Item> itemsTipo = new LinkedList<>();
- if (conn != null) {
- try (Statement st = conn.createStatement()) {
- String sqls = "SELECT * from items WHERE tipo='" + tipo + "'";
- try (ResultSet rs = st.executeQuery(sqls)) {
- while (rs.next()) {
- String nombre = rs.getString("nombre");
- Item item = new Item(nombre, tipo);
- itemTipo.add(item);
- }
- }
- } catch (SQLException ex) {
- Logger.getLogger(DBItem.class.getName()).log(Level.SEVERE, null, ex);
- }
- }
- return itemsTipo;
- }
- public void closeDB() {
- try {
- conn.close();
- } catch (SQLException ex) {
- Logger.getLogger(DBItem.class.getName()).log(Level.SEVERE, null, ex);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement