zoukankan      html  css  js  c++  java
  • java_jdbc_spring框架查询操作简例

    1.添加commons-dbcp-1.4,commons-logging,commons-pool-1.2//线程池,commons.collections-3.2.1.jar,spring.jar

    2.利用spring的查询方法和jdbc  "DataSource数据源"

    package com.ming.www;
    
    import java.sql.ResultSet;
    import java.sql.SQLException;
    
    import org.springframework.jdbc.core.BeanPropertyRowMapper;
    import org.springframework.jdbc.core.JdbcTemplate;
    import org.springframework.jdbc.core.RowMapper;
    
    public class TestDemo1 {
    
    	public static void main(String[] args) {
    		// TODO Auto-generated method stub
    		User user = new User();
    		user = findUser("ccc");
    		System.out.println(user.getUsername());
    		System.out.println(user.getPassword());
    	}
    
    	public static User findUser(String name) {
    		JdbcTemplate jdbc = new JdbcTemplate(JdbcUtils.getDatasource());
    		String sql = "select id,username,password from t_user where username=?";
    		Object[] args = new Object[] { name };
    		Object user = jdbc.queryForObject(sql, args, new BeanPropertyRowMapper(User.class));
    		return (User) user;
    	}
    	public static User findUser1(String name) {
    		JdbcTemplate jdbc = new JdbcTemplate(JdbcUtils.getDatasource());
    		String sql = "select id,username,password from t_user where username=?";
    		Object[] args = new Object[] { name };
    		Object user = jdbc.queryForObject(sql, args, new RowMapper() {
    
    			@Override
    			public Object mapRow(ResultSet rs, int arg1) throws SQLException {
    				// TODO Auto-generated method stub
    				User user = new User();
    				user.setId(rs.getInt("id"));
    				user.setUsername(rs.getString("username"));
    				user.setPassword(rs.getString("password"));
    				return user;
    			}
    
    		});
    		return (User) user;
    	}
    }
    


     

    package cn.itcast.www;
    
    
    import java.io.InputStream;
    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.ResultSet;
    import java.sql.SQLException;
    import java.sql.Statement;
    import java.util.Properties;
    
    import javax.sql.DataSource;
    
    import org.apache.commons.dbcp.BasicDataSourceFactory;
    
    public final class JdbcUtils {
    
    	// private static String url = "jdbc:mysql://";
    	// private static String user = "";
    	// private static String password = "、";
    
    	private static DataSource myDataSource = null;
    
    	public JdbcUtils() {
    
    	}
    
    	static {
    		try {
    			Class.forName("com.mysql.jdbc.Driver");
    			// mydatasource = new MyDataSource();
    			Properties prop = new Properties();
    
    			InputStream ins = JdbcUtils.class.getClassLoader()
    					.getResourceAsStream("dbcpconfig.properties");
    			prop.load(ins);
    			myDataSource = BasicDataSourceFactory.createDataSource(prop);
    
    		} catch (Exception e) {
    			throw new ExceptionInInitializerError(e);
    		}
    	}
    
    	public static Connection getConnection() throws SQLException {
    		return myDataSource.getConnection();
    	}
    
    	public static void free(ResultSet rs, Statement st, Connection conn) {
    		try {
    			if (rs != null)
    				rs.close();
    		} catch (SQLException e) {
    			e.printStackTrace();
    		} finally {
    			try {
    				if (st != null)
    					st.close();
    			} catch (SQLException e) {
    				e.printStackTrace();
    			} finally {
    				if (conn != null)
    					try {
    						conn.close();
    						// mydatasource.free(conn);
    					} catch (Exception e) {
    						e.printStackTrace();
    					}
    			}
    		}
    	}
    }
    


     

  • 相关阅读:
    Linux 设置主机名和 IP 地址的映射关系
    Zookeeper 数据查看工具 ZooInspector 的使用
    Zookeeper 集群
    Zookeeper 监控指定节点数据变化
    Zookeeper 常用命令
    Spring整合ActiveMQ持久化到Mysql数据库
    Activemq5.15.5持久化到mysql8.0.20
    ActiveMQ支持的协议
    SIGINT SIGTERM SIGKILL区别
    lograte -日志轮询log
  • 原文地址:https://www.cnblogs.com/MarchThree/p/3720416.html
Copyright © 2011-2022 走看看