import java.sql.*;
import java.util.ArrayList;
import java.util.List;
import dto.Student;
public class StudentDAO implements IStudentDAO{
public static void main(String[] args) {
// Student s=new Student("aa",12,13.4f);
// StudentDAO dao=new StudentDAO();
// dao.intsertStudent(s);
// StudentDAO dao=new StudentDAO();
// List <Student>list=dao.getAllStudent();
// dao.print(list);
// StudentDAO dao=new StudentDAO();
// dao.updateStudent();
StudentDAO dao=new StudentDAO();
dao.deleteStudent();
}
public void print(List <Student> list)
{
for(Student s:list)
System.out.println(s.getName()+","+s.getAge()+","+s.getHeight());
}
public Connection getConnection()
{
Connection con=null;
try {
Class.forName("net.sourceforge.jtds.jdbc.Driver");
con=DriverManager.getConnection("jdbc:jtds:sqlserver://localhost:1433/lwy","sa","lwy");
} catch (Exception e) {
e.printStackTrace();
}
return con;
}
public boolean deleteStudent()
{
boolean flag=false;
Connection con=null;
PreparedStatement pstmt=null;
String sql="delete from student where id=14";
try {
con=this.getConnection();
pstmt=con.prepareStatement(sql, ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
int count=pstmt.executeUpdate();
if(count>0)
flag=true;
else
flag=false;
} catch (Exception e) {
e.printStackTrace();
flag=false;
}
finally
{
try {
if(pstmt!=null)
pstmt.close();
if(con!=null)
con.close();
} catch (Exception e) {
// TODO: handle exception
}
}
return flag;
}
public boolean updateStudent()
{
boolean flag=false;
Connection con=null;
PreparedStatement pstmt=null;
String sql="update student set age=12 where id=13";
try {
con=this.getConnection();
pstmt=con.prepareStatement(sql, ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
int count=pstmt.executeUpdate();
if(count>0)
flag=true;
else
flag=false;
} catch (Exception e) {
e.printStackTrace();
flag=false;
}
finally
{
try {
if(pstmt!=null)
pstmt.close();
if(con!=null)
con.close();
} catch (Exception e) {
// TODO: handle exception
}
}
return flag;
}
@Override
public boolean intsertStudent(Student s) {
Connection con=null;
PreparedStatement pstmt=null;
String sql="Insert into Student (name,age,height) values(?,?,?)";
boolean flag=false;
try {
con=this.getConnection();
pstmt=con.prepareStatement(sql, ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
pstmt.setString(1, s.getName());
pstmt.setInt(2, s.getAge());
pstmt.setFloat(3, s.getHeight());
int count=pstmt.executeUpdate();
if(count>0)
flag=true;
else
flag=false;
} catch (Exception e) {
e.printStackTrace();
flag=false;
}
finally
{
try {
if(pstmt!=null)
pstmt.close();
if(con!=null)
con.close();
} catch (Exception e) {
// TODO: handle exception
}
}
return flag;
}
@Override
public List <Student> getAllStudent() {
Connection con=null;
PreparedStatement pstmt=null;
ResultSet rs=null;
String sql="select * from student";
List <Student> list=new ArrayList <Student>();
try {
con=this.getConnection();
pstmt=con.prepareStatement(sql, ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
rs=pstmt.executeQuery();
while(rs.next())
{
Student s=new Student(rs.getString("name"),rs.getInt("age"),rs.getFloat("height"));
list.add(s);
}
} catch (Exception e) {
e.printStackTrace();
}
finally
{
try {
if(rs!=null);
rs.close();
if(pstmt!=null)
pstmt.close();
if(con!=null)
con.close();
} catch (Exception e) {
// TODO: handle exception
}
}
return list;
}
}
//简单的增删改查全实现了。你自己加个窗体就ok了。
StudentDAO.rar_StudentDao
版权申诉
42 浏览量
2022-09-19
14:22:36
上传
评论
收藏 1KB RAR 举报
JaniceLu
- 粉丝: 80
- 资源: 1万+
最新资源
- MySQL是一种广泛使用的开源关系型数据库管理系统
- MySQL是一种广泛使用的开源关系型数据库管理系统
- MySQL是一种广泛使用的开源关系型数据库管理系统
- 012c3c44c465a099108e0d8570b86a70.zip
- 基于Java和JavaWeb的网上商城项目设计源码 - myshopping
- 基于Vue和JavaScript的书城项目设计源码 - Demo12.18
- wp2787778-map-wallpaper.jpg
- 基于Javascript的杜王町打工人仓库管理系统设计源码 - 杜王町打工人的仓库
- 基于C#的报销材料合并工具设计源码 - 报账材料合并
- 基于Java的驾校一点通后端服务设计源码 - jiaxiaoServer
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
评论0