mysql 连接
mysql 连接
package com.cn.dao;
import java.sql.DriverManager;
public class BaseDao {
private java.sql.Connection conn;
private java.sql.Statement stat;
private java.sql.ResultSet rs;
public BaseDao(){
}
public void openConnection() {
try{
Class.forName("com.mysql.jdbc.Driver");
conn=DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/test", "root", "wp");
}catch(Exception e){
e.printStackTrace();
}
}
public int exectue(String sql){
int a=0;
try{
stat=conn.createStatement();
a=stat.executeUpdate(sql);
}catch (Exception e) {
e.printStackTrace();
}
return a;
}
public java.sql.ResultSet getRs(String sql){
try{
stat=conn.createStatement();
rs=stat.executeQuery(sql);
}catch (Exception e) {
e.printStackTrace();
}
return rs;
}
public void close(){
try{
if(rs!=null){
rs.close();
}
stat.close();
conn.close();
}catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
}
}
package com.cn.dao;
import java.sql.SQLException;
import java.util.ArrayList;
import com.cn.bean.UserBean;
import com.cn.sys.PageUtil;
public class Userdao {
public boolean addUser(String username,String pwd,String tel){
String sql="insert into users value('"+username+"','"+pwd+"','"+tel+"')";
BaseDao dao=new BaseDao();
dao.openConnection();
int a=dao.exectue(sql);
dao.close();
if(a!=0){
return true;
}else{
return false;
}
}
public boolean updateUser(String username,String pwd,String tel){
String sql="update users set pwd='"+pwd+"',tel='"+tel+"' where username='"+username+"'";
BaseDao dao=new BaseDao();
dao.openConnection();
int a=dao.exectue(sql);
dao.close();
if(a!=0){
return true;
}
else{
return false;
}
}
public ArrayList<UserBean> getArry(PageUtil page){
String sql="select * from users";
BaseDao dao=new BaseDao();
int index=page.getPageIndex();
int c=page.getItemCountPerPage();
int count=(index-1)*c+1;
dao.openConnection();
java.sql.ResultSet rs=dao.getRs(sql);
ArrayList<UserBean> arry=new ArrayList<UserBean>();
try{
rs.absolute(count);
for(int i=0;i<c;i++){
UserBean u=new UserBean();
u.setUsername(rs.getString("username"));
u.setPwd(rs.getString("pwd"));
u.setTel(rs.getString("tel"));
arry.add(u);
if(!rs.next()){
break;
}
}
}catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}finally{
try {
rs.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
dao.close();
}
return arry;
}
public UserBean getBean(String username){
String sql="select * from users where username='"+username+"'";
BaseDao dao=new BaseDao();
dao.openConnection();
java.sql.ResultSet rs=dao.getRs(sql);
UserBean u=new UserBean();
try{
while(rs.next()){
u.setUsername(rs.getString("username"));
u.setPwd(rs.getString("pwd"));
u.setTel(rs.getString("tel"));
}
}catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}finally{
try {
rs.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
dao.close();
}
return u;
}
public int getCount(){
String sql="select count(*) as c from users";
BaseDao dao=new BaseDao();
dao.openConnection();
int a=0;
java.sql.ResultSet rs=dao.getRs(sql);
try{
while(rs.next()){
a=rs.getInt("c");
}
}catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}finally{
try {
rs.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
dao.close();
}
return a;
}
}