java操作Access数据库(带参数插入及查询)

最近使用了Acess数据库:

1、建好数据库

2、配好数据源

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.sql.*;
import java.util.Properties;
/**
 * 
 * @author Administrator
 * @param  连接Acess数据库
 *
 */
public class AcessL {
	
	/**
	 * @param  返回连接方法
	 * @return Connection
	 */
	public static Connection getPre(){
		Connection conn=null;
		Properties p=new Properties();
		
		try {
			p.load(new FileInputStream("H:/YLZX/JWC/user.properties"));//读取文件中的数据,也可以直接写
			Class.forName(p.getProperty("driver"));
			conn=DriverManager.getConnection(p.getProperty("url"),p.getProperty("user"), p.getProperty("psw"));
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return conn;
		
	}
	/**
	 * @param 带参数的插入方法
	 * @param UserID
	 * @param UserPsw
	 * @param Type
	 */
	public void insert(String UserID,String UserPsw,String Type)
	{
		Connection conn=getPre();
		try {
			//Statement smt=conn.createStatement();
			String sql="INSERT into User(UserID,UserPsw,type) values(?,?,?);";//sql语句中末尾的分号可有可无
		    PreparedStatement ppst=conn.prepareStatement(sql);
		    ppst.setString(1, UserID);//对sql语句中的每个?按顺序赋值,赋值的方法取决于数据库的字段属性,我的字段属性都是字符串类型
		    ppst.setString(2, UserPsw);
		    ppst.setString(3, Type);
			
		    ppst.executeUpdate();
			System.out.println(sql);
			//smt.close();
			ppst.close();
			conn.close();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
	/**
	 * @param 带参数的查询方法
	 * @param UserID
	 * @param UserPsw
	 * @param Type
	 * @return
	 */
	public boolean Select(String UserID,String UserPsw,String Type){
		boolean bool=false;
		Connection conn=getPre();
		PreparedStatement ppst=null;
		ResultSet rs=null;
		String s=" ";
		String sql="select UserPsw from User  where  UserID=? and type=?";
		
		try {
		    ppst=conn.prepareStatement(sql);
			ppst.setString(1, UserID);
			ppst.setString(2, Type);
			rs=ppst.executeQuery();
			while(rs.next())
			{
				s=rs.getString(1);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		try {
			rs.close();
			ppst.close();
			conn.close();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		if(s.equals(UserPsw))
		{
			bool=true;
		}
		else{
			bool=false;
		}
		
		
		
		
		return bool;
		
	}
	public static void main(String args[])
	{
		AcessL acessl=new  AcessL();
	//System.out.println(acessl.getPre());
		//acessl.insert("LB90aaaa","9090","1");
		System.out.println(acessl.Select("L", "89", "0"));
	
	}


}

  • 1
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值