JDBC实现数据库的几种基本操作(查询,分页查询,根据关键字进行查询以及插入数据)
2014-08-23 20:27
1266 查看
public class CreateTable {
/**
* 向表中插入数据
*
* @param sql
* @param user
*/
public void insertData(String sql, User user) {
DriverMangement ds = DriverMangement.getInstance();
Connection connection = null;
PreparedStatement ps = null;
try {
connection = ds.getConnection();
ps = connection.prepareStatement(sql);
ps.setInt(1, user.getId());
ps.setString(2, user.getName());
ps.setString(3, user.getPassword());
ps.setString(4, user.getSex());
ps.setString(5, user.getTelphone());
ps.setString(6, user.getPassword());
ps.setString(7, user.getEmail());
ps.executeUpdate();
ps.close();
connection.close();
} catch (Exception e) {
}
}
/**
* 查询所有用户的信息
*
* @return
* @throws SQLException
*/
public List<User> searchInfo() throws SQLException {
DriverMangement ds = DriverMangement.getInstance();
Connection connection = null;
PreparedStatement ps = null;
String sql = "select * from user";
ResultSetToObject handler = new ResultSetToObject() {
/**
* 把ResultSet里面的值转换成某个具体的对象
*/
@Override
public Object convertToObject(ResultSet rs) throws SQLException {
User user = new User();
int id = rs.getInt("id");
String name = rs.getString("name");
String password = rs.getString("password");
String sex = rs.getString("telphone");
String telphone = rs.getString("name");
String photo = rs.getString("photo");
String email = rs.getString("email");
user.setId(id);
user.setName(name);
user.setPassword(password);
user.setPhoto(photo);
user.setEmail(email);
user.setSex(sex);
user.setTelphone(telphone);
return user;
}
};
List data = new Vector();
connection = ds.getConnection();
ps = connection.prepareStatement(sql);
Statement st = (Statement) connection.createStatement();
ResultSet resultSet = ps.executeQuery();
while (resultSet.next()) {
Object val = handler.convertToObject(resultSet);
data.add(val);
}
ps.close();
connection.close();
System.out.println("所有学生的信息:" + data);
return data;
}
/**
* 根据姓名和密码进行查询
*
* @param name
* @param password
* @return
*/
public List<User> SearchData(String name, String password) {
DriverMangement ds = DriverMangement.getInstance();
Connection connection = null;
PreparedStatement ps = null;
String sql = "select * from user where name=? and password=?";
ResultSetToObject handler = new ResultSetToObject() {
@Override
public Object convertToObject(ResultSet rs) throws SQLException {
User user = new User();
int id = rs.getInt("id");
String name = rs.getString("name");
String password = rs.getString("password");
String sex = rs.getString("telphone");
String telphone = rs.getString("name");
String photo = rs.getString("photo");
String email = rs.getString("email");
user.setId(id);
user.setName(name);
user.setPassword(password);
user.setPhoto(photo);
user.setEmail(email);
user.setSex(sex);
user.setTelphone(telphone);
return user;
}
};
List<User> data = new Vector<User>();
try {
connection = ds.getConnection();
ps = connection.prepareStatement(sql);
ps.setString(1, name);
ps.setString(2, password);
ResultSet resultSet = ps.executeQuery();
while (resultSet.next()) {
Object val = new Object();
val = handler.convertToObject(resultSet);
data.add((User) val);
}
ps.close();
connection.close();
} catch (Exception e) {
}
System.out.println(data);
return data;
}
/**
* 判断数据库中该用户名是否存在
*
* @param username
* @return
*/
public static boolean userExsist(String username) {
DriverMangement ds = DriverMangement.getInstance();
String sql = "select * from user where name=?";
Connection connection = null;
PreparedStatement ps = null;
Boolean bool = true;
try {
connection = ds.getConnection();
ps = connection.prepareStatement(sql);
ps.setString(1, username);
ResultSet rs = ps.executeQuery();
if (rs.next()) {
User user = new User();
user.setId(rs.getInt("id"));
user.setEmail(rs.getString("email"));
user.setName(rs.getString("name"));
user.setPassword(rs.getString("password"));
user.setPhoto(rs.getString("photo"));
user.setSex(rs.getString("sex"));
user.setTelphone(rs.getString("telphone"));
System.out.println(user);
return true;
}
rs.close();
ps.close();
connection.close();
} catch (Exception e) {
}
return false;
}
/**
* 分页显示信息
* @param sql
* @param pagesize
* @param page
* @return
* @throws SQLException
*/
public CachedRowSet showByPage(String sql,int pagesize,int page) throws SQLException
{
DriverMangement ds=DriverMangement.getInstance();
Connection connection=null;
PreparedStatement ps=null;
CachedRowSet cRowSet=null;
try {
connection=ds.getConnection();
ps=connection.prepareStatement(sql);
ResultSet rSet=ps.executeQuery();
RowSetFactory rFactory=RowSetProvider.newFactory();
cRowSet=rFactory.createCachedRowSet();
cRowSet.setPageSize(pagesize);
cRowSet.populate(rSet,(page-1)*pagesize+1);
rSet.close();
ps.close();
} catch (Exception e) {
// TODO: handle exception
}
finally
{
if(connection!=null)
{
connection.close();
}
}
return cRowSet;
}
public static User getUser() {
User user = new User();
user.setId(005);
user.setName("zhansgan");
user.setPassword("123456");
user.setSex("女");
user.setTelphone("18444441111");
user.setPhoto("1.jpeg");
user.setEmail("ahnu12345@163.com");
return user;
}
public static void main(String[] args) throws SQLException {
CreateTable table = new CreateTable();
User user = getUser();
String sql = "insert into user(id,name,password,sex,telphone,photo,email) values(?,?,?,?,?,?,?)";
String name = "zhansgan";
String pass = "123456";
String sql1="select * from user";
// boolean bool = table.userExsist("zhansgan");
// if (bool) {
// System.out.println("用户名已存在");
// } else {
// System.out.println("对不起,你所查找的用户不存在");
// }
// table.insertData(sql, user);
// table.SearchData(name, pass);
// table.searchInfo();
CachedRowSet cachedRowSet=table.showByPage(sql1, 4, 2);
while(cachedRowSet.next())
{
System.out.println(cachedRowSet.getInt("id")+"\t"
+cachedRowSet.getString("name")+"\t"
+cachedRowSet.getString("password")+"\t"
+cachedRowSet.getString("sex")+"\t"
+cachedRowSet.getString("email")+"\t"
+cachedRowSet.getString("telphone")+"\t"
+cachedRowSet.getString("photo"));
}
}
}
/**
* 向表中插入数据
*
* @param sql
* @param user
*/
public void insertData(String sql, User user) {
DriverMangement ds = DriverMangement.getInstance();
Connection connection = null;
PreparedStatement ps = null;
try {
connection = ds.getConnection();
ps = connection.prepareStatement(sql);
ps.setInt(1, user.getId());
ps.setString(2, user.getName());
ps.setString(3, user.getPassword());
ps.setString(4, user.getSex());
ps.setString(5, user.getTelphone());
ps.setString(6, user.getPassword());
ps.setString(7, user.getEmail());
ps.executeUpdate();
ps.close();
connection.close();
} catch (Exception e) {
}
}
/**
* 查询所有用户的信息
*
* @return
* @throws SQLException
*/
public List<User> searchInfo() throws SQLException {
DriverMangement ds = DriverMangement.getInstance();
Connection connection = null;
PreparedStatement ps = null;
String sql = "select * from user";
ResultSetToObject handler = new ResultSetToObject() {
/**
* 把ResultSet里面的值转换成某个具体的对象
*/
@Override
public Object convertToObject(ResultSet rs) throws SQLException {
User user = new User();
int id = rs.getInt("id");
String name = rs.getString("name");
String password = rs.getString("password");
String sex = rs.getString("telphone");
String telphone = rs.getString("name");
String photo = rs.getString("photo");
String email = rs.getString("email");
user.setId(id);
user.setName(name);
user.setPassword(password);
user.setPhoto(photo);
user.setEmail(email);
user.setSex(sex);
user.setTelphone(telphone);
return user;
}
};
List data = new Vector();
connection = ds.getConnection();
ps = connection.prepareStatement(sql);
Statement st = (Statement) connection.createStatement();
ResultSet resultSet = ps.executeQuery();
while (resultSet.next()) {
Object val = handler.convertToObject(resultSet);
data.add(val);
}
ps.close();
connection.close();
System.out.println("所有学生的信息:" + data);
return data;
}
/**
* 根据姓名和密码进行查询
*
* @param name
* @param password
* @return
*/
public List<User> SearchData(String name, String password) {
DriverMangement ds = DriverMangement.getInstance();
Connection connection = null;
PreparedStatement ps = null;
String sql = "select * from user where name=? and password=?";
ResultSetToObject handler = new ResultSetToObject() {
@Override
public Object convertToObject(ResultSet rs) throws SQLException {
User user = new User();
int id = rs.getInt("id");
String name = rs.getString("name");
String password = rs.getString("password");
String sex = rs.getString("telphone");
String telphone = rs.getString("name");
String photo = rs.getString("photo");
String email = rs.getString("email");
user.setId(id);
user.setName(name);
user.setPassword(password);
user.setPhoto(photo);
user.setEmail(email);
user.setSex(sex);
user.setTelphone(telphone);
return user;
}
};
List<User> data = new Vector<User>();
try {
connection = ds.getConnection();
ps = connection.prepareStatement(sql);
ps.setString(1, name);
ps.setString(2, password);
ResultSet resultSet = ps.executeQuery();
while (resultSet.next()) {
Object val = new Object();
val = handler.convertToObject(resultSet);
data.add((User) val);
}
ps.close();
connection.close();
} catch (Exception e) {
}
System.out.println(data);
return data;
}
/**
* 判断数据库中该用户名是否存在
*
* @param username
* @return
*/
public static boolean userExsist(String username) {
DriverMangement ds = DriverMangement.getInstance();
String sql = "select * from user where name=?";
Connection connection = null;
PreparedStatement ps = null;
Boolean bool = true;
try {
connection = ds.getConnection();
ps = connection.prepareStatement(sql);
ps.setString(1, username);
ResultSet rs = ps.executeQuery();
if (rs.next()) {
User user = new User();
user.setId(rs.getInt("id"));
user.setEmail(rs.getString("email"));
user.setName(rs.getString("name"));
user.setPassword(rs.getString("password"));
user.setPhoto(rs.getString("photo"));
user.setSex(rs.getString("sex"));
user.setTelphone(rs.getString("telphone"));
System.out.println(user);
return true;
}
rs.close();
ps.close();
connection.close();
} catch (Exception e) {
}
return false;
}
/**
* 分页显示信息
* @param sql
* @param pagesize
* @param page
* @return
* @throws SQLException
*/
public CachedRowSet showByPage(String sql,int pagesize,int page) throws SQLException
{
DriverMangement ds=DriverMangement.getInstance();
Connection connection=null;
PreparedStatement ps=null;
CachedRowSet cRowSet=null;
try {
connection=ds.getConnection();
ps=connection.prepareStatement(sql);
ResultSet rSet=ps.executeQuery();
RowSetFactory rFactory=RowSetProvider.newFactory();
cRowSet=rFactory.createCachedRowSet();
cRowSet.setPageSize(pagesize);
cRowSet.populate(rSet,(page-1)*pagesize+1);
rSet.close();
ps.close();
} catch (Exception e) {
// TODO: handle exception
}
finally
{
if(connection!=null)
{
connection.close();
}
}
return cRowSet;
}
public static User getUser() {
User user = new User();
user.setId(005);
user.setName("zhansgan");
user.setPassword("123456");
user.setSex("女");
user.setTelphone("18444441111");
user.setPhoto("1.jpeg");
user.setEmail("ahnu12345@163.com");
return user;
}
public static void main(String[] args) throws SQLException {
CreateTable table = new CreateTable();
User user = getUser();
String sql = "insert into user(id,name,password,sex,telphone,photo,email) values(?,?,?,?,?,?,?)";
String name = "zhansgan";
String pass = "123456";
String sql1="select * from user";
// boolean bool = table.userExsist("zhansgan");
// if (bool) {
// System.out.println("用户名已存在");
// } else {
// System.out.println("对不起,你所查找的用户不存在");
// }
// table.insertData(sql, user);
// table.SearchData(name, pass);
// table.searchInfo();
CachedRowSet cachedRowSet=table.showByPage(sql1, 4, 2);
while(cachedRowSet.next())
{
System.out.println(cachedRowSet.getInt("id")+"\t"
+cachedRowSet.getString("name")+"\t"
+cachedRowSet.getString("password")+"\t"
+cachedRowSet.getString("sex")+"\t"
+cachedRowSet.getString("email")+"\t"
+cachedRowSet.getString("telphone")+"\t"
+cachedRowSet.getString("photo"));
}
}
}
相关文章推荐
- JAVA对数据库进行操作,实现数据库中数据的插入,查询,更改,删除操作
- JDBC 数据库连接 创建表格、插入、查询、删除、修改数据 基本操作
- JAVA对数据库进行操作,实现数据库中数据的插入,查询,更改,删除操作
- JDBC实现增删改查、模糊查询、分页查询、子查询以及体现单例设计模式连接数据库
- JDBC连接Oracle数据库,以及基本的插入查询操作
- JAVA+JDBC对数据库的数据进行增加、修改、删除、查询等操作
- 只插入来代替删改实现数据库表数据的版本控制及操作历史查询
- (二)不同数据库间的交叉数据查询插入等操作实现
- 根据从redis缓存的数据查询出来,在从数据库中取出所有的数据,俩个数据进行比较,去掉重复,剩下库中新插入的数据,取出新数据,然后把redis中的缓存数据清空把从数据库中查出来的所有数据放到redis缓存中
- Spring与SpringMVC整合,以及实现数据库数据的增删查改和分页操作
- php从PostgreSQL 数据库检索数据,实现分页显示以及根据条件查找数据
- JDBC操作Vertica数据库,用PreparedStatements对象实现批量插入数据
- 前台交互,根据id、关键字查询、插入新数据到数据库
- 根据ajax发送数据,从servelt中读取xml数据并显示,实现查询、删除和分页
- 【NOSQL】非关系型数据库MongoDB ( 通过samus驱动实现基本数据操作 )
- 如何实现快速地从超大数据库中提取数据并进行相关的操作
- Java操作Hbase进行建表、删表以及对数据进行增删改查,条件查询
- 天气预报,MYSQL之JDBC查询模块实现(代码)和简易插入模块实现(代码)以及PREPAREDSTATEMENT插入模块实现(代码)
- 3.如何优化操作大数据量数据库(实现小数据量和海量数据的分页显示存储过程)
- 数据查询分页的几种实现方式