package com.zh.dao;
import java.sql.Connection;
import java.sql.Date;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import com.zh.SelectingCourse.SelectingCourse;
import com.zh.TeachingCourse.TeachingCourse;
import com.zh.course.Course;
import com.zh.manager.Manager;
import com.zh.student.Student;
import com.zh.teacher.Teacher;
public class Dao {
private static String driver= "net.sourceforge.jtds.jdbc.Driver";
protected static String dbUrl = "jdbc:jtds:sqlserver://localhost:1433;"
+ "DatabaseName=网上选课系统";
protected static String dbUser = "sa";
protected static String dbPwd = "abc123@";
protected static String second = null;
private static Connection conn = null;
public Dao() {
try
{
Class.forName(driver);
try{
conn=DriverManager.getConnection(dbUrl, dbUser, dbPwd);
}catch(SQLException e){
e.printStackTrace();
}
}catch(ClassNotFoundException e){
e.printStackTrace();
}
}
/*
创建执行查询语句的方法executeQuery,其返回值ResultSet结果集,首先要初始化Dao对象,调用构造函数,从而获取数据库连接
*/
@SuppressWarnings("unused")
private static ResultSet executeQuery(String sql) {
try {
if(conn==null)
new Dao();
return conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE).executeQuery(sql);
} catch (SQLException e) {
e.printStackTrace();
return null;
} finally {
}
}
/*
创建执行更改语句的方法,它的返回值为int类型的整数,此返回值代表数据库更新的操作是否成功,返回1代表成功,不成功返回-1
*/
@SuppressWarnings("unused")
private static int executeUpdate(String sql) {
try {
if(conn==null)
new Dao();
return conn.createStatement().executeUpdate(sql);
} catch (SQLException e) {
System.out.println(e.getMessage());
//if(e.getMessage().equals("[Microsoft][SQLServer 2000 Driver for JDBC][SQLServer]DELETE 语句与 COLUMN REFERENCE 约束 'FK_TB_BORRO_REFERENCE_TB_BOOKI' 冲突。该冲突发生于数据库 'db_library',表 'tb_borrow', column 'bookISBN'。"))
return -1;
} finally {
}
}
//优化项目运行速度,在完成数据库操作后,关闭连接
public static void close() {
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}finally{
conn = null;
}
}
/*
* manager登录方法
*/
public static Manager checkm(String mno, String mpassword) {
Manager manager=new Manager();
String sql = "select * from tb_manager where mno='" + mno
+ "' and mpassword='" + mpassword+ "'";
ResultSet rs = Dao.executeQuery(sql);
try {
while (rs.next()) {
String names = rs.getString(1);
manager.setMno(rs.getString("mno"));
manager.setMname(rs.getString("mname"));
manager.setMpassword(rs.getString("mpassword"));
}
} catch (Exception e) {
e.printStackTrace();
}
Dao.close();
return manager;
}
/*
* teacher登录方法
*/
public static Teacher checkt(String tno, String tpassword) {
Teacher teacher=new Teacher();
String sql = "select * from tb_teacher where tno='" + tno
+ "' and tpassword='" + tpassword+ "'";
ResultSet rs = Dao.executeQuery(sql);
try {
while (rs.next()) {
String names = rs.getString(1);
teacher.setTno(rs.getString("tno"));
teacher.setTname(rs.getString("tname"));
teacher.setTsex(rs.getString("tsex"));
teacher.setTpassword(rs.getString("tpassword"));
}
} catch (Exception e) {
e.printStackTrace();
}
Dao.close();
return teacher;
}
/*
* student登录方法
*/
public static Student checks(String sno, String spassword) {
Student student=new Student();
String sql = "select * from tb_student where sno='" + sno
+ "' and spassword='" + spassword+ "'";
ResultSet rs = Dao.executeQuery(sql);
try {
while (rs.next()) {
String names = rs.getString(1);
student.setSno(rs.getString("sno"));
student.setSname(rs.getString("sname"));
student.setSsex(rs.getString("ssex"));
student.setSage(rs.getString("sage"));
student.setEntrancetime(rs.getString("entrancetime"));
student.setSmajor(rs.getString("smajor"));
student.setSsdept(rs.getString("ssdept"));
student.setSpassword(rs.getString("spassword"));
}
} catch (Exception e) {
e.printStackTrace();
}
Dao.close();
return student;
}
/*
* 添加InsertStudent方法
*/
public static int InsertStudent(String sno,String sname,String ssex,String sage,String entrancetime,String smajor,String ssdept,String spassword)
{
int i=0;
try{
String sql="insert into tb_student(sno,sname,ssex,sage,entrancetime,smajor,ssdept,spassword) values('"+sno+"','"+sname+"','"+ssex+"','"+sage+"','"+entrancetime+"','"+smajor+"','"+ssdept+"','"+spassword+"')";
System.out.println(sql);
i=Dao.executeUpdate(sql);
}catch(Exception e){
e.printStackTrace();
}
Dao.close();
return i;
}
/*
* 添加InsertTeacher方法
*/
public static int InsertTeacher(String tno,String tname,String tsex,String tpassword)
{
int i=0;
try{
String sql="insert into tb_teacher(tno,tname,tsex,tpassword) values('"+tno+"','"+tname+"','"+tsex+"','"+tpassword+"')";
System.out.println(sql);
i=Dao.executeUpdate(sql);
}catch(Exception e){
e.printStackTrace();
}
Dao.close();
return i;
}
/*
* 添加InsertManager方法
*/
public static int InsertManager(String mno,String mname,String mpassword)
{
int i=0;
try{
String sql="insert into tb_manager(mno,mname,mpassword) values('"+mno+"','"+mname+"','"+mpassword+"')";
System.out.println(sql);
i=Dao.executeUpdate(sql);
}catch(Exception e){
e.printStackTrace();
}
Dao.close();
return i;
}
/*
* 添加InsertCourse方法
*/
public static int InsertCourse(String cno,String cname,String ccredit)
{
int i=0;
try{
String sql="insert into tb_course(cno,cname,ccredit) values('"+cno+"','"+cname+"','"+ccredit+"')";
System.out.println(sql);
i=Dao.executeUpdate(sql);
}catch(Exception e){
e.printStackTrace();
}
Dao.close();
return i;
}
/*
* 添加InsertSelectingCourse方法
*/
public static int InsertSelectingCourse(String sno,String cno,String grade)
{
int i=0;
try{
String sql="insert into tb_selecting_course(sno,cno,grade) values('"+sno+"','"+cno+"','"+grade+"')";
System.out.println(sql);
//if(Integer.parseInt(grade)==0)
i=Dao.executeUpdate(sql);
}catch(Exception e){
e.printStackTrace();
}
Dao.close();
return i;
}
/*
* 添加InsertTeachingCourse方法
*/
public static int InsertTeachingCourse(String tno,String cno,String ctime)
{
int i=0;
try{
String sql="insert into tb_teaching_course(tno,cno,ctime) values('"+tno+"','"+cno+"','"+ctime+"')";
System.out.println(sql);
i=Dao.executeUpdate(sql);
}catch(Exception e){
e.printStackTrace();
}
Dao.close();
return i;
}
/*
* 添加selectStudent方法
*/
public static List selectStudent() {
List list=new ArrayList();
String sql = "select * from tb_student";
ResultSet rs = Dao.executeQuery(sql);
try {
while (rs.next()) {
Student student=new Student();
student.setSno(rs.getString("sno"));
student.setSname(rs.getString("sname"));
student.setSsex(rs.getString("ssex"));
student.setSage(rs.getString("sage"));
student.setEntrancetime(rs.getString("entrancetime"));
student.setSmajor(rs.getString("smajor"));
student.setSsdept(rs.getString("ssdept"));
student.setSpassword(rs.getString("spassword"));
list.add(student);
}
} catch (Exception e) {
e.printStackTrace();
}
Dao.close();
return list;
}
/*
* 添加selectCourse方法
*/
public static List selectCourse() {
List list=new ArrayList();
String sql = "select * fr
小白程序媛
- 粉丝: 1
- 资源: 5
最新资源
- 【创新无忧】基于龙格库塔优化算法RUN优化广义神经网络GRNN实现数据回归预测附matlab代码.rar
- 【创新无忧】基于龙格库塔优化算法RUN优化广义神经网络GRNN实现光伏预测附matlab代码.rar
- 【创新无忧】基于龙格库塔优化算法RUN优化相关向量机RVM实现北半球光伏数据预测附matlab代码.rar
- 【创新无忧】基于龙格库塔优化算法RUN优化极限学习机ELM实现乳腺肿瘤诊断附matlab代码.rar
- 【创新无忧】基于龙格库塔优化算法RUN优化极限学习机KELM实现故障诊断附matlab代码.rar
- 【创新无忧】基于麻雀搜索优化算法SSA优化广义神经网络GRNN实现电机故障诊断附matlab代码.rar
- 【创新无忧】基于麻雀搜索优化算法SSA优化广义神经网络GRNN实现光伏预测附matlab代码.rar
- 【创新无忧】基于龙格库塔优化算法RUN优化相关向量机RVM实现数据多输入单输出回归预测附matlab代码.rar
- 【创新无忧】基于麻雀搜索优化算法SSA优化极限学习机KELM实现故障诊断附matlab代码.rar
- 【创新无忧】基于麻雀搜索优化算法SSA优化广义神经网络GRNN实现数据回归预测附matlab代码.rar
- 【创新无忧】基于麻雀搜索优化算法SSA优化极限学习机ELM实现乳腺肿瘤诊断附matlab代码.rar
- 【创新无忧】基于麻雀搜索优化算法SSA优化相关向量机RVM实现北半球光伏数据预测附matlab代码.rar
- 【创新无忧】基于能量谷优化算法EVO优化广义神经网络GRNN实现电机故障诊断附matlab代码.rar
- 【创新无忧】基于麻雀搜索优化算法SSA优化相关向量机RVM实现数据多输入单输出回归预测附matlab代码.rar
- 【创新无忧】基于能量谷优化算法EVO优化极限学习机ELM实现乳腺肿瘤诊断附matlab代码.rar
- 【创新无忧】基于能量谷优化算法EVO优化广义神经网络GRNN实现光伏预测附matlab代码.rar
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈