package com.qiuchanghong.myjdbc.ch1;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
public class Userqjdbc implements Userq
{
public void delUser(String username)
{
//定义链接对象
Connection connection=null;
PreparedStatement statement=null;
//定义SQL语句
String sql="delete fron user where username=?";
try
{
//获得链接
connection=MySQLConnect.getIntence();
//获得SQL语句对象
statement=connection.prepareStatement(sql);
//填充SQL对象参数
statement.setString(1, username);
//删除指定的记录
statement.executeUpdate();
//提交记录
connection.commit();
}
catch(SQLException a)
{
a.printStackTrace();
}
finally
{
try
{
if(connection!=null) connection.close();
if(statement!=null) statement.close();
}
catch(SQLException b)
{
System.out.println("关闭数据库出错,请与系统管理员联系!");
b.printStackTrace();
}
}
}
public int insertUser(User user)
{
//定义返回值
int value=-1;
//定义连接对象
Connection connection=null;
PreparedStatement statement=null;
//定义sql语句
String sql="insert user(username ,password,falg)values (?,?,?)";
try
{
//获得连接对象
connection=MySQLConnect.getIntence();
//获得SQL语句对象
statement=connection.prepareStatement(sql);
//填充SQL对象参数
statement.setString(1, user.getUsername());
statement.setString(2, user.getPassword());
statement.setString(3, user.getFlag());
// 往User表中添加一条记录
value = statement.executeUpdate();
// 提交
connection.commit();
// 返回结果
return value;
}
catch(SQLException e)
{
e.printStackTrace();
}
finally
{
try
{
if(statement != null) statement.close();
if(connection != null) connection.close();
}
catch(SQLException e)
{
System.out.println("关闭数据库出错,请与系统管理员联系!");
e.printStackTrace();
}
}
return value;
}
public int updateUser(User user)
{
//定义一个返回值
int value=-1;
//定义一个连接对象
Connection connection=null;
PreparedStatement statement=null;
//定义一个查寻语句
String sql="update user set username=?,password=?,flag=?";
try
{
//获得链接对象
connection=MySQLConnect.getIntence();
//获得SQL语句对象
statement=connection.prepareStatement(sql);
//填充SQL对象参数
statement.setString(1, user.getUsername());
statement.setString(2, user.getPassword());
statement.setString(3, user.getFlag());
//修改表中制定的值
value=statement.executeUpdate();
//提交
connection.commit();
return value;
}
catch(SQLException e)
{
e.printStackTrace();
}
finally
{
try
{
if(statement!=null) statement.close();
if(connection!=null) connection.close();
}
catch(SQLException f)
{
System.out.println("关闭数据库出错,请与系统管理员联系!");
f.printStackTrace();
}
}
return value;
}
public List<User> getAllUsers()
{
// 定义返回值
List<User> users = new ArrayList<User>();
String sql = "select * from user";
// 定义连接对象
Connection connection = null;
PreparedStatement statement = null;
ResultSet resultSet = null;
try
{
connection = MySQLConnect.getIntence();
statement = connection.prepareStatement(sql);
// 获得查询结果
resultSet = statement.executeQuery();
// 处理结果集
while(resultSet.next())
{
// 定义User对象
User user = new User();
// 用查询出来的记录填充User对象
user.setUsername(resultSet.getString("username"));
user.setPassword(resultSet.getString("password"));
user.setFlag(resultSet.getString("flag"));
// 将User对象放入集合中
users.add(user);
}
return users;
}
catch(SQLException e)
{
e.printStackTrace();
}
finally
{
try
{
if(resultSet != null) resultSet.close();
if(statement != null) statement.close();
if(connection != null) connection.close();
}
catch(SQLException e)
{
System.out.println("关闭数据库出错,请与系统管理员联系!");
e.printStackTrace();
}
}
return users;
}
}
MyJDBC_qiu
3星 · 超过75%的资源 需积分: 3 108 浏览量
2008-07-12
10:35:30
上传
评论
收藏 1.3MB RAR 举报
野人776
- 粉丝: 1
- 资源: 6
最新资源
- Win64OpenSSL-3-3-0.exe
- 课高分程设计-基于C++实现的民航飞行与地图简易管理系统-南京航空航天大学
- 航天器遥测数据故障检测系统python源码+文档说明+数据库(课程设计)
- 北京航空航天大学操作系统课设+ppt+实验报告
- 基于Vue+Echarts实现风力发电机中传感器的数据展示监控可视化系统+源代码+文档说明(高分课程设计)
- 基于单片机的风力发电机转速控制源码
- 基于C++实现的风力发电气动平衡监测系统+源代码+测量数据(高分课程设计)
- 毕业设计- 基于STM32F103C8T6 单片机,物联网技术的太阳能发电装置+源代码+文档说明+架构图+界面截图
- 基于 LSTM(长短期记忆)(即改进的循环神经网络)预测风力发电厂中风力涡轮机产生的功率+源代码+文档说明
- 基于stm32f103+空心杯电机+oled按键+运动算法
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈