前言:本章纯代码,后期会修改。
一.数据库代码
--用户表
create table shop_user(
id number primary key,
account varchar2(30) not null,
password varchar2(30) not null
);
insert into shop_user values(1,'xxx','xxx123');
--商品表
create table shop_goods(
id number primary key,
name varchar2(30) not null,
price number default 0.0,--价格
info varchar2(200) default '三五产品' not null--简介
);
commit
注意一定要commit提交,因为有可能拿不到数据。
二.项目代码
导入美化包,jar包
User.java代码
package com.zking.pojo;
public class User {
private Integer id;
private String account;
private String password;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getAccount() {
return account;
}
public void setAccount(String account) {
this.account = account;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public User(Integer id, String account, String password) {
super();
this.id = id;
this.account = account;
this.password = password;
}
public User() {
super();
// TODO Auto-generated constructor stub
}
}
Goods.java代码
package com.zking.pojo;
public class Goods {
private Integer id;
private String name;
private Integer price;
private String info;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getPrice() {
return price;
}
public void setPrice(Integer price) {
this.price = price;
}
public String getInfo() {
return info;
}
public void setInfo(String info) {
this.info = info;
}
public Goods(Integer id, String name, Integer price, String info) {
super();
this.id = id;
this.name = name;
this.price = price;
this.info = info;
}
public Goods() {
super();
// TODO Auto-generated constructor stub
}
@Override
public String toString() {
return "Goods [id=" + id + ", name=" + name + ", price=" + price + ", info=" + info + "]";
}
}
DBHelper.java代码
package com.zking.util;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class DBHelper {
//加载驱动
static{
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
//定义链接字符串
private static final String URL="jdbc:oracle:thin:@localhost:1521:orcl";
//获得链接
public static Connection getCon() {
try {
return DriverManager.getConnection(URL,"scott","sa123");
} catch (SQLException e) {
e.printStackTrace();
}
return null;
}
//关闭资源
public static void close(Connection con,PreparedStatement ps, ResultSet rs) {
try {
if (con!=null&&!con.isClosed()) {
con.close();
}if (ps!=