druid link database

Used jar package

commons-beanutils-1.8.0.jar
commons-logging-1.1.3.jar
druid-1.0.9.jar
mysql-connector-java-5.1.18-bin.jar
spring-beans-3.2.5.RELEASE.jar
spring-context-3.2.5.RELEASE.jar
spring-core-3.2.5.RELEASE.jar
spring-expression-3.2.5.RELEASE.jar
spring-jdbc-3.2.5.RELEASE.jar
spring-tx-3.2.5.RELEASE.jar

The profile connection mysql
configuration file # druid.properties
driverClassName = com.mysql.jdbc.Driver
URL = JDBC: mysql: //127.0.0.1: 3306 / DB1
username =
password =
# Number of initialization connection
initialSize. 5 =
# Maximum Connections number of
maxActive = 10
# maximum timeout
maxWait = 3000

tool to connect database
com.alibaba.druid.pool.DruidDataSourceFactory Import; 

Import the javax.sql.DataSource;
Import java.io.IOException;
Import a java.io.InputStream;
Import the java.sql.Connection;
Import java.sql.SQLException;
Import Classes in java.util .properties;

public class JDBCUtil {
// definition data source
Private the dataSource static DS;
static {

the try {
// definition of a Properties object
Properties Pro = new new Properties ();
// get the address of the druid, bytecode obtained
InputStream is = JDBCUtil . .class.getClassLoader () the getResourceAsStream ( "druid.properties");
pro.load (IS);
DS = DruidDataSourceFactory.createDataSource (Pro);
The catch} (IOException E) {
e.printStackTrace ();
} the catch (Exception E) {
e.printStackTrace ();
}

}
public static getDataSource the DataSource () {
return DS;
}
public static Connection the getConnection () throws SQLException {
return DS .getConnection ();
}
}

database operation method CRUD
public class UserDaoImp implements UserDao{
//声明JDBCTemplate
private JdbcTemplate template=new JdbcTemplate(JDBCUtil.getDataSource());
// if the username and password when the user logs on, etc.
@Override
public User userLogin(User user) {
User user1 = null;
try {
String sql = "select * from user where username=? and password=?";
user1 = template.queryForObject(sql,
new BeanPropertyRowMapper<User>(User.class),
user.getUsername(),
user.getPassword());
} catch (Exception e) {
e.printStackTrace();
return null;
}
return user1;
}
// all the data look-up table
@Override
public List<User> users() {
String sql="select * from user";
List<User> maps = template.query(sql, new BeanPropertyRowMapper<User>(User.class));
return maps;
}
//delete data
@Override
public void delUser(int id) {
String sql="delete from user where id=?";
template.update(sql,id);
}
//添加数据
public void addUser(User user) {
String sql="insert into user(username,password,name,deptid,orgid,createdate) values('nc123456','nc123456',?,?,?,?)";
template.update(sql,user.getName(),user.getDeptid(),user.getOrgid(),new Date());
}
//change the data
@Override
public void updateUser(User user) {
String sql="update user set deptid=?,orgid=? where id=?";
template.update(sql,user.getDeptid(),user.getOrgid(),user.getId());
}

}

JSP
<table class="table table-hover table-condensed table-bordered" align="center">

<tr style="background-color: #b2dba1;text: center" >
<th><label class="checkbox-inline">
<input type="checkbox" name="uid" id="firstcb">
</label></th>
<th>序号</th>
<th>姓名</th>
<th>组织序号</th>
<th>部门序号</th>
<th>日期</th>
<th>操作</th>
</tr>
<c:forEach items="${users}" varStatus="list" var="lis">
<tr>
<td><label class="checkbox-inline">
<input type="checkbox" name="uid" value="${lis.id}">
</label></td>
<td>${list.count}</td>
<td>${lis.name}</td>
<td>${lis.orgid}</td>
<td>${lis.deptid}</td>
<td>${lis.createdate}</td>
<td><a class="btn btn-default" href="javascript:update(${lis.id});" role="button">修改</a>
<a class="btn btn-default" href="javascript:delecte(${lis.id});" role="button">删除</a></td>
</tr>
</c:forEach>
</table>
 
 

Guess you like

Origin www.cnblogs.com/fpflog/p/12047591.html