java_jdbc_spring框架查询操作简例
本文内容纲要: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();
}
}
}
}
}
本文内容总结:java_jdbc_spring框架查询操作简例
原文链接:https://www.cnblogs.com/MarchThree/p/3720416.html
以上是 java_jdbc_spring框架查询操作简例 的全部内容, 来源链接: utcz.com/z/362699.html