项目描述
上传时间
浏览人数
package com.domo0923.dao;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import com.domo0923.pojo.pojo;
public class Dao {
public Connection connect() {
try {
Class.forName("com.mysql.jdbc.Driver");
Connection c=DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/ddddd","root","123456");
return c;
} catch (ClassNotFoundException | SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
public boolean isHad(String zhanghao){
String sql="select * from ddddd.www where col='"+zhanghao+"';";
try {
PreparedStatement pre=connect().prepareStatement(sql);
ResultSet re=pre.executeQuery();
while(re.next()){
return true;
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return false;
}
public void add(String zhanghao,String mima,String xingbie,String yuwen,String shuxue,String yingyu,String zhonghe,String beizhu){
String sql="INSERT INTO ddddd.www (col, oi, cd, yu, shu, ying, zhonghe, beizhu) VALUES ('"+zhanghao+"', '"+mima+"', '"+xingbie+"', '"+yuwen+"', '"+shuxue+"', '"+yingyu+"', '"+zhonghe+"', '"+beizhu+"');";
try {
PreparedStatement pre=connect().prepareStatement(sql);
pre.executeUpdate();
connect().close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public List<pojo> selectAll(){
String sql="SELECT * FROM ddddd.www;";
try {
PreparedStatement pre=connect().prepareStatement(sql);
ResultSet re=pre.executeQuery();
connect().close();
List<pojo> ls=new ArrayList<>();
while(re.next()){
pojo pj=new pojo();
pj.setCol(re.getString("col"));
pj.setOi(re.getString("oi"));
pj.setCd(re.getString("cd"));
pj.setYu(re.getString("yu"));
pj.setShu(re.getString("shu"));
pj.setYing(re.getString("ying"));
pj.setZhonghe(re.getString("zhonghe"));
pj.setBeizhu(re.getString("beizhu"));
ls.add(pj);
}
return ls;
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
public static void main(String[] args) {
Dao d = new Dao();
System.out.println(d.selectAll());
}
public pojo select(String zhanghao){
String sql="select * from ddddd.www where col='"+zhanghao+"';";
try {
PreparedStatement pre=connect().prepareStatement(sql);
ResultSet re=pre.executeQuery();
pojo pj=new pojo();
while(re.next()){
pj.setCol(zhanghao);
pj.setOi(re.getString("oi"));
pj.setCd(re.getString("cd"));
pj.setYu(re.getString("yu"));
pj.setShu(re.getString("shu"));
pj.setYing(re.getString("ying"));
pj.setZhonghe(re.getString("zhonghe"));
pj.setBeizhu(re.getString("beizhu"));
}
return pj;
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
public int del(String zhanghao){
String sql="delete from ddddd.www where col='"+zhanghao+"';";
try {
PreparedStatement pre=connect().prepareStatement(sql);
int i=pre.executeUpdate();
connect().close();
return i;
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return 0;
}
public int update(String zhanghao,String mima,String xingbie,String yuwen,String shuxue,String yingyu,String zhonghe,String beizhu){
String sql="UPDATE ddddd.www SET oi='"+mima+"', cd='"+xingbie+"', yu='"+yuwen+"', shu='"+shuxue+"', ying='"+yingyu+"', zhonghe='"+zhonghe+"', beizhu='"+beizhu+"' WHERE col='"+zhanghao+"';";
try {
PreparedStatement pre=connect().prepareStatement(sql);
int i=pre.executeUpdate();
connect().close();
return i;
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return 0;
}
}