package
javabean;
import java.io.UnsupportedEncodingException;
import java.sql. * ;
import java.util. * ;
import javabean.Goods;
import javabean.Orders;
public class ConnectionDB {
Connection con = null ;
Statement st = null ;
Orders ord = null ;
PreparedStatement pst = null ;
public ConnectionDB(){}
public Vector listGoods( int page){ // 显示出数据库中所有商品
jdbcConnection();
String sql = " select * from goods limit " + page + " ,5 " ;
Vector v = new Vector();
try {
ResultSet rs = st.executeQuery(sql);
Goods goods = null ;
while (rs.next()){
goods = new Goods();
goods.setId(rs.getInt( " id " ));
goods.setName(rs.getString( " name " ));
goods.setType(rs.getString( " type " ));
goods.setPrice(rs.getDouble( " price " ));
v.addElement(goods);
}
return v;
} catch (SQLException e) {
e.printStackTrace();
return v;
}
}
public String chString(String str){ // 解决显示在页面上的中文乱码
try {
if (str.equals( "" )){
str = "" ;
}
else str = new String(str.getBytes( " ISO8859-1 " ), " GB2312 " );
} catch (Exception e){
e.printStackTrace();
e.getMessage();
}
return str;
}
public Vector selectRecord(String str){ // 查找商品
jdbcConnection();
Vector v = new Vector();
String sql = " select id,name,type,price from goods where name like '% " + str + " %' " ;
try {
ResultSet rs = st.executeQuery(sql);
Goods goods = null ;
while (rs.next()){
goods = new Goods();
goods.setId(rs.getInt( " id " ));
goods.setName(rs.getString( " name " ));
goods.setType(rs.getString( " type " ));
goods.setPrice(rs.getDouble( " price " ));
v.addElement(goods);
}
return v;
} catch (SQLException e) {
e.printStackTrace();
return v;
}
}
public boolean isExist(String name,String password) // 用户登陆
{
jdbcConnection();
ResultSet rs = null ;
try {
pst = con.prepareStatement( " select * from register where name=? and password=? " );
pst.setString( 1 ,name);
pst.setString( 2 ,password);
rs = pst.executeQuery();
if (rs.next()){
return true ;
}
} catch (Exception e) {
System.out.println( " 您所输入的用户名或密码不存在 " );
System.out.println(e.getMessage());
}
return false ;
}
public boolean zhuCu(String name,String password,String sex,String degree) // 用户注册
{
jdbcConnection();
try {
String sq = " select name from register where name=' " + name + " ' " ;
ResultSet rs = st.executeQuery(sq);
if (rs.next()){
System.out.println( " 用户名已存在! " );
return false ;
}
String sql = " insert into register(name,password,sex,degree) values(?,?,?,?) " ;
pst = con.prepareStatement(sql);
pst.setString( 1 ,name);
pst.setString( 2 ,password);
pst.setString( 3 ,sex);
pst.setString( 4 ,degree);
pst.executeUpdate();
pst = con.prepareStatement( " insert into login(name,password) values(?,?) " );
pst.setString( 1 ,name);
pst.setString( 2 ,password);
pst.executeUpdate();
System.out.println( " 插入成功 " );
return true ;
} catch (Exception e) {
System.out.println( " 您所输入的类型与数据库不符 " );
System.out.println(e.getMessage());
}
return false ;
}
public void jdbcConnection(){ // 连接数据库
try {
Class.forName( " org.gjt.mm.mysql.Driver " );
con = DriverManager.getConnection( " jdbc:mysql://127.0.0.1:3309/shopping " , " root " , " root " );
st = con.createStatement();
System.out.println( " 数据库已连接 " );
} catch (Exception e) {
System.out.println(e.getMessage());
e.printStackTrace();
}
}
public static void main(String[] args){
ConnectionDB con = new ConnectionDB();
con.zhuCu( " 方法 " , " admin " , " fmale " , " dgree " );
}
}
import java.io.UnsupportedEncodingException;
import java.sql. * ;
import java.util. * ;
import javabean.Goods;
import javabean.Orders;
public class ConnectionDB {
Connection con = null ;
Statement st = null ;
Orders ord = null ;
PreparedStatement pst = null ;
public ConnectionDB(){}
public Vector listGoods( int page){ // 显示出数据库中所有商品
jdbcConnection();
String sql = " select * from goods limit " + page + " ,5 " ;
Vector v = new Vector();
try {
ResultSet rs = st.executeQuery(sql);
Goods goods = null ;
while (rs.next()){
goods = new Goods();
goods.setId(rs.getInt( " id " ));
goods.setName(rs.getString( " name " ));
goods.setType(rs.getString( " type " ));
goods.setPrice(rs.getDouble( " price " ));
v.addElement(goods);
}
return v;
} catch (SQLException e) {
e.printStackTrace();
return v;
}
}
public String chString(String str){ // 解决显示在页面上的中文乱码
try {
if (str.equals( "" )){
str = "" ;
}
else str = new String(str.getBytes( " ISO8859-1 " ), " GB2312 " );
} catch (Exception e){
e.printStackTrace();
e.getMessage();
}
return str;
}
public Vector selectRecord(String str){ // 查找商品
jdbcConnection();
Vector v = new Vector();
String sql = " select id,name,type,price from goods where name like '% " + str + " %' " ;
try {
ResultSet rs = st.executeQuery(sql);
Goods goods = null ;
while (rs.next()){
goods = new Goods();
goods.setId(rs.getInt( " id " ));
goods.setName(rs.getString( " name " ));
goods.setType(rs.getString( " type " ));
goods.setPrice(rs.getDouble( " price " ));
v.addElement(goods);
}
return v;
} catch (SQLException e) {
e.printStackTrace();
return v;
}
}
public boolean isExist(String name,String password) // 用户登陆
{
jdbcConnection();
ResultSet rs = null ;
try {
pst = con.prepareStatement( " select * from register where name=? and password=? " );
pst.setString( 1 ,name);
pst.setString( 2 ,password);
rs = pst.executeQuery();
if (rs.next()){
return true ;
}
} catch (Exception e) {
System.out.println( " 您所输入的用户名或密码不存在 " );
System.out.println(e.getMessage());
}
return false ;
}
public boolean zhuCu(String name,String password,String sex,String degree) // 用户注册
{
jdbcConnection();
try {
String sq = " select name from register where name=' " + name + " ' " ;
ResultSet rs = st.executeQuery(sq);
if (rs.next()){
System.out.println( " 用户名已存在! " );
return false ;
}
String sql = " insert into register(name,password,sex,degree) values(?,?,?,?) " ;
pst = con.prepareStatement(sql);
pst.setString( 1 ,name);
pst.setString( 2 ,password);
pst.setString( 3 ,sex);
pst.setString( 4 ,degree);
pst.executeUpdate();
pst = con.prepareStatement( " insert into login(name,password) values(?,?) " );
pst.setString( 1 ,name);
pst.setString( 2 ,password);
pst.executeUpdate();
System.out.println( " 插入成功 " );
return true ;
} catch (Exception e) {
System.out.println( " 您所输入的类型与数据库不符 " );
System.out.println(e.getMessage());
}
return false ;
}
public void jdbcConnection(){ // 连接数据库
try {
Class.forName( " org.gjt.mm.mysql.Driver " );
con = DriverManager.getConnection( " jdbc:mysql://127.0.0.1:3309/shopping " , " root " , " root " );
st = con.createStatement();
System.out.println( " 数据库已连接 " );
} catch (Exception e) {
System.out.println(e.getMessage());
e.printStackTrace();
}
}
public static void main(String[] args){
ConnectionDB con = new ConnectionDB();
con.zhuCu( " 方法 " , " admin " , " fmale " , " dgree " );
}
}