Java+mysql如何写增删改查?

 如何在UserDaoImpl里面写增删改查?

package com.houtai.dao;

import java.util.List;

import com.houtai.entity.User;

public interface UserDao {
	public int addUser(User u);
	public int delUserById(int id);
	public int updateUser(User u);
	User getUserById(int id);
	public User login(String username,String password);
	List<User> getUserList();
}
package com.houtai.entity;

public class User {
	private Integer id;
	private String username;
	private String password;
	private Integer phone;
	public Integer getId() {
		return id;
	}
	public void setId(Integer id) {
		this.id = id;
	}
	public String getUsername() {
		return username;
	}
	public void setUsername(String username) {
		this.username = username;
	}
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
	public Integer getPhone() {
		return phone;
	}
	public void setPhone(Integer phone) {
		this.phone = phone;
	}
	@Override
	public String toString() {
		return "User [id=" + id + ", username=" + username + ", password=" + password + ", phone=" + phone + "]";
	}
	public User(Integer id, String username, String password, Integer phone) {
		super();
		this.id = id;
		this.username = username;
		this.password = password;
		this.phone = phone;
	}
	public User() {
		super();
		// TODO Auto-generated constructor stub
	}
	
}
package com.houtai.util;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class BaseDao {
	public static String driver="com.mysql.jdbc.Driver";
	public static String url="jdbc:mysql://localhost:3306/houtai?useUnicode=true&characterEncoding=utf8&useSSL=false&serverTimezone=UTC";
	public static String uname="root";
	public static String upwd="root";
	
	protected Connection conn=null;
	protected PreparedStatement ps =null;
	protected ResultSet rs=null;
	//静态属性在类加载阶段执行
	static {
		try {
			Class.forName(driver);
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
	}
	//连接数据库 
	public Connection getConnection() {
		try {
			if(conn==null||conn.isClosed()) {
				conn = DriverManager.getConnection(url,uname,upwd);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return conn;
	}
	//连接数据库 
	public ResultSet excuteQuery(String sql,Object... objs) {
		conn=this.getConnection();
		try {
			//创建预准备传输对象
			ps=conn.prepareStatement(sql);
			if (objs!=null&&objs.length>0) {
				for (int i = 0; i < objs.length; i++) {
					ps.setObject((i+1), objs[i]);
				}
			}
			//返回结果集
			rs=ps.executeQuery();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return rs;
	}
	//增删改 1/0
	public int excuteUpdate(String sql,Object... objs) {
		conn=this.getConnection();
		int result=0;
		try {
			//创建预准备传输对象
			ps=conn.prepareStatement(sql);
			if (objs!=null&&objs.length>0) {
				for (int i = 0; i < objs.length; i++) {
					ps.setObject((i+1), objs[i]);
				}
			}
			//创建预准备传输对象
			result=ps.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return result;
	}
	//关闭资源
	public void close() {
		try {
			if(rs!=null&&!rs.isClosed()) {
				rs.close();
			}
		} catch (Exception e) {
			// TODO: handle exception
		}
		try {
			if(ps!=null&&!ps.isClosed()) {
				ps.close();
			}
		} catch (Exception e) {
			// TODO: handle exception
		}
		try {
			if(conn!=null&&!conn.isClosed()) {
				conn.close();
			}
		} catch (Exception e) {
			// TODO: handle exception
		}
	}
}

 

package com.houtai.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import com.houtai.dao.UserDao;
import com.houtai.entity.User;
import com.houtai.util.BaseDao;

public class UserDaoImpl extends BaseDao implements UserDao{

	@Override
	public int addUser(User u) {
		String sql = "inset into user (username,password,phone) values (?,?,?)";
		return 0;
	}

	@Override
	public int delUserById(int id) {
		String sql = "delete from user where id= " +id;
		return 0;
	}
	
	@Override
	public int updateUser(User u) {
		String sql = "update user set username=?,password=?,phone=? where id=?";
		
		return 0;
	}
	
	@Override
	public User getUserById(int id) {
		String sql = "select * from user where id" +id;
		ResultSet rs = this.excuteQuery(sql);
		User user=null;
		try {
			while(rs.next()) {
				int id1 = rs.getInt("id");
				String username =rs.getString("username");
				String password =rs.getString("password");
				user.setId(id);
				user.setUsername(username);
				user.setPassword(password);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return user;
	}

	@Override
	public User login(String username, String password) {
		String sql = "select * from user where username=? and password=?";
		ResultSet rs = this.excuteQuery(sql,username,password);
		User user =null;
		try {
			while (rs.next()) {
				int id= rs.getInt("id");
				user =new User();
				user.setId(id);
				user.setPassword(password);
				user.setUsername(username);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return user;
	}

	@Override
	public List<User> getUserList() {
		String sql = "select * from user";
		ResultSet rs = this.excuteQuery(sql);
		List<User> list = new ArrayList<User>();
		try {
			while (rs.next()) {
				int id = rs.getInt("id");
				String username =rs.getString("username");
				String password =rs.getString("password");
				int phone = rs.getInt("phone");
				User user = new User();
				user.setId(id);
				user.setUsername(username);
				user.setPassword(password);
				user.setPhone(phone);
				list.add(user);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return list;
	}

	
}

你写的这种,适合做test或者是main方法执行的那种,不是web基于容器启动的。