⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 studentdao.java

📁 学生管理系统
💻 JAVA
字号:
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了。

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -