Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 002d57a

Browse files
author
JesusBetaX
committed
init 3
1 parent d3d98df commit 002d57a

File tree

5 files changed

+98
-18
lines changed

5 files changed

+98
-18
lines changed

‎.gitignore‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1-
/nbproject/private/
1+
/nbproject/private/
2+
/build/

‎src/com/jx/db/DataBaseConfig.java‎

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
11
package com.jx.db;
22

3-
import java.sql.ResultSet;
4-
import java.sql.SQLException;
5-
63
/**
74
*
85
* @author Jesus
@@ -19,9 +16,9 @@ public static DataBase getDataBaseMySQL() {
1916
mysql = new DataBase();
2017
// Ejemplo con base de datos MySQL
2118
mysql.setDriverClassName("com.mysql.jdbc.Driver");
22-
mysql.setUrl("jdbc:mysql://localhost:3306/database_name");
23-
mysql.setUsername("usuario");
24-
mysql.setPassword("password");
19+
mysql.setUrl("jdbc:mysql://localhost:3306/punto_venta");
20+
mysql.setUsername(/*"usuario"*/"root");
21+
mysql.setPassword(/*"password"*/"");
2522
}
2623
return mysql;
2724
}

‎src/com/jx/db/Demo.java‎

Lines changed: 29 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
package com.jx.db;
22

3+
import java.util.LinkedHashMap;
34
import java.sql.ResultSet;
45
import java.sql.SQLException;
5-
import java.util.LinkedHashMap;
66

77
/**
88
*
@@ -16,7 +16,7 @@ public void select() throws SQLException {
1616

1717
try (ResultSet rs = db.query(sql)) {
1818
while (rs.next()) {
19-
int idProducto = rs.getInt("idProducto");
19+
int idProducto = rs.getInt("id");
2020
String codigo = rs.getString("codigo");
2121
String nombre = rs.getString("nombre");
2222

@@ -27,27 +27,45 @@ public void select() throws SQLException {
2727
}
2828
}
2929

30-
public void insert() throws SQLException {
30+
public int insert(Productop) throws SQLException {
3131
try (DataBase db = DataBaseConfig.getDataBaseMySQL()) {
3232
LinkedHashMap<String, Object> values = new LinkedHashMap<>();
33-
values.put("codigo", "PC-89273912");
34-
values.put("nombre", "PC");
35-
int idProducto = db.insert("producto", values);
33+
values.put("codigo", p.getCodigo());
34+
values.put("nombre", p.getNombre());
35+
36+
int id_insertado = db.insert("producto", values);
37+
p.setId(id_insertado);
38+
return id_insertado;
3639
}
3740
}
3841

39-
public void upadte(intidProducto) throws SQLException {
42+
public int upadte(Productop) throws SQLException {
4043
try (DataBase db = DataBaseConfig.getDataBaseMySQL()) {
4144
LinkedHashMap<String, Object> values = new LinkedHashMap<>();
42-
values.put("codigo", "PC-89273912");
43-
values.put("nombre", "PC");
44-
db.update("producto", values,
45-
"idProducto = ?", idProducto);
45+
values.put("codigo", p.getCodigo());
46+
values.put("nombre", p.getNombre());
47+
48+
returndb.update("producto", values, "id = ?", p.getId());
4649
}
4750
}
4851

4952
public static void main(String... args) throws SQLException {
5053
Demo demo = new Demo();
54+
55+
Producto p = new Producto();
56+
p.setCodigo("PC-" + System.currentTimeMillis());
57+
p.setNombre("PC");
58+
59+
demo.insert(p);
60+
System.out.println("insert:" + p);
61+
62+
63+
p.setCodigo("PC-" + System.currentTimeMillis());
64+
65+
demo.upadte(p);
66+
System.out.println("update:" + p);
67+
68+
demo.select();
5169
}
5270

5371
}

‎src/com/jx/db/Producto.java‎

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package com.jx.db;
2+
3+
import java.util.LinkedHashMap;
4+
5+
/**
6+
*
7+
* @author Jesus
8+
*/
9+
public class Producto {
10+
11+
private int id;
12+
private String codigo;
13+
private String nombre;
14+
15+
public Producto() {
16+
this(0, "", "");
17+
}
18+
19+
public Producto(int id, String codigo, String nombre) {
20+
this.id = id;
21+
this.codigo = codigo;
22+
this.nombre = nombre;
23+
}
24+
25+
public int getId() {
26+
return id;
27+
}
28+
public void setId(int id) {
29+
this.id = id;
30+
}
31+
public String getCodigo() {
32+
return codigo;
33+
}
34+
public void setCodigo(String codigo) {
35+
this.codigo = codigo;
36+
}
37+
public String getNombre() {
38+
return nombre;
39+
}
40+
public void setNombre(String nombre) {
41+
this.nombre = nombre;
42+
}
43+
44+
@Override
45+
public String toString() {
46+
LinkedHashMap<String, Object> values = new LinkedHashMap<>();
47+
values.put("id", id);
48+
values.put("codigo", codigo);
49+
values.put("nombre", nombre);
50+
return values.toString();
51+
}
52+
53+
54+
}

‎src/com/jx/db/mysql.sql‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
2+
create database if not exists punto_venta;
3+
use punto_venta;
4+
5+
create table producto(
6+
id int not null auto_increment,
7+
codigo varchar(25),
8+
nombre varchar(255),
9+
primary key(id)
10+
);

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /