java操作数据库sqlserver(三)

第三步:实现接口方法的实现类

package com.qilu.jdbcDemo.dao.IuserImp;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.List;
import java.util.ArrayList;
import java.sql.*;

import javax.swing.JOptionPane;

import com.qilu.jdbcDemo.dao.IUserDao;
import com.qilu.jdbcDemo.entity.User;


/*
 *这是一个实体类,实现了IUserDao接口中的所有方法 
 */
public class UserDaoImp implements IUserDao {

	//插入一条数据 --实现方法
	@Override
	public int insertOneUser(User user) {
		
		int i = 0;
		//连接数据库
		try {
			Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		try {
			Connection conn = DriverManager.getConnection(
					"jdbc:sqlserver://localhost:1433;databaseName=JDBCDemo","sa","100862");
			Statement stmt = conn.createStatement();
			//插入语句
			i = stmt.executeUpdate("insert into TestStudent values ("+user.getId()+",'"+user.getName()+"','"+user.getSex()+"',"+user.getAge()+")");
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return i;
		
	}

	//删除一条数据
	@Override
	public int deleteOneUser(User user) {
		
		int i = 0;
		//连接数据库
		try {
			Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		try {
			Connection conn = DriverManager.getConnection(
					"jdbc:sqlserver://localhost:1433;databaseName=JDBCDemo","sa","100862");
			Statement stmt = conn.createStatement();
			//插入语句
			i = stmt.executeUpdate("delete from TestStudent where id = " + user.getId());
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return i;

	}

	//更新一条数据
	@Override
	public int updateOneUser(User user) {
		
		int i = 0;
		//连接数据库
		try {
			Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		try {
			Connection conn = DriverManager.getConnection(
					"jdbc:sqlserver://localhost:1433;databaseName=JDBCDemo","sa","100862");
			//更新语句
			String sql = "update TestStudent set name = ?,sex = ?,age = ?  where id = ?";
			PreparedStatement stmt = conn.prepareStatement(sql);
			//设置参数
			stmt.setString(1, user.getName());
			stmt.setString(2, user.getSex());
			stmt.setInt(3,user.getAge());
			stmt.setInt(4, user.getId());
			i = stmt.executeUpdate();
 		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return i;
	}

	//通过userId查找一条数据
	@Override
	public User findOneUser(int userId) {
	
		User user = new User();
		//连接数据库
		try {
			Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		try {
			Connection conn = DriverManager.getConnection(
					"jdbc:sqlserver://localhost:1433;databaseName=JDBCDemo","sa","100862");
			Statement stmt = conn.createStatement();
			//得到查询的结果
			ResultSet rs = stmt.executeQuery("select * from TestStudent where id = "+userId );
			
			while(rs.next()){

				user.setId(rs.getInt("id"));
				user.setName(rs.getString("name"));
				user.setSex(rs.getString("sex"));
				user.setAge(rs.getInt("age"));
			}
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}		
		return user;
	}

	//通过userName查找一条数据
	@Override
	public User frindOneUser(String userName) {

		User user = new User();
		//连接数据库
		try {
			Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		try {
			Connection conn = DriverManager.getConnection(
					"jdbc:sqlserver://localhost:1433;databaseName=JDBCDemo","sa","100862");
			Statement stmt = conn.createStatement();
			//得到查询的结果
			ResultSet rs = stmt.executeQuery("select * from TestStudent where name = '"+userName+"'" );
			
			while(rs.next()){

				user.setId(rs.getInt("id"));
				user.setName(rs.getString("name"));
				user.setSex(rs.getString("sex"));
				user.setAge(rs.getInt("age"));
			}
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}		
		return user;
	}

	//查找出所有的数据
	@Override
	public List<User> findAllUser() {
		
		//建立一个集合,放所有数据
		List<User> userList = new ArrayList<User>();
		//连接数据库
		try {
			Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		try {
			Connection conn = DriverManager.getConnection(
					"jdbc:sqlserver://localhost:1433;databaseName=JDBCDemo","sa","100862");
			Statement stmt = conn.createStatement();
			//得到查询的结果
			ResultSet rs = stmt.executeQuery("select * from TestStudent");
			
			while(rs.next()){
				//每循环一次产生一个user对象
				User user = new User();
				user.setId(rs.getInt("id"));
				user.setName(rs.getString("name"));
				user.setSex(rs.getString("sex"));
				user.setAge(rs.getInt("age"));
				//把user放到userList集合中去
				userList.add(user);
			}
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	
		return userList;
	}

	//验证数据是否存在
	@Override
	public boolean validate(User user) {
		
		boolean b = false;
		
		//连接数据库
		try {
			Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		try {
			Connection conn = DriverManager.getConnection(
					"jdbc:sqlserver://localhost:1433;databaseName=JDBCDemo","sa","100862");
			Statement stmt = conn.createStatement();
			//得到查询的结果
			ResultSet rs = stmt.executeQuery("select * from TestStudent where id = '"+user.getId()+"'  and name = '"+user.getName()+"'" );
			
			if(rs.next()){
				
				b = true;
			}
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}		
		
		return b;
	}

}


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

蟹道人

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值