课程管理系统

    此次Java课程有学长为我们讲解了关于数据库的建造,对数据库的导入导出有了一定程度上的了解。

对数据库的连接和增删改查使用可查看:https://www.bilibili.com/video/av30839320/?p=20

数据库相关内容下载可查看网址:https://www.vipkes.cn/service.jsp           关注公众号可查看具体安装步骤(获取验证码获得公众号)

以下是关于此次课程的学习:

首先应该用JDBC连接数据库,使用eclipse成功连接数据库:

util.java

package ke;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class util {
	String user="sa";
	String password="zb753951";
 //liusy数据库名称        user后为数据库名称       password为密码根据实际情况更改 String url="jdbc:mysql://localhost:3306/liusy?" + "user=root&password=15075289483&useUnicode=true&characterEncoding=UTF8&serverTimezone=GMT%2B8&useSSL=false"; public Connection getConn(){ Connection conn=null; try { Class.forName("com.mysql.jdbc.Driver"); } catch (ClassNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } try { conn=DriverManager.getConnection(url, user, password); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } return conn; } public void close(ResultSet rs, Statement state, Connection conn) { if(rs!=null) { try { rs.close(); } catch(SQLException e) { e.printStackTrace(); } } if(state!=null) { try { state.close(); } catch(SQLException e) { e.printStackTrace(); } } if(conn!=null) { try { conn.close(); } catch(SQLException e) { e.printStackTrace(); } } } }

  

然后调用连接编写对数据库的增删改查:

Test.java

package ke;

import java.sql.*;
import java.util.Scanner;




public class Test {
	public static void main(String[] args) {
		System.out.println("��������ҿγ̣�");
		String a;
		Scanner scan = new Scanner(System.in);
		a = scan.next();
		find(a);
		
	}
	static Connection conn;
	static PreparedStatement ps = null;
	static ResultSet rs;
	static String sql = "select * from Ke_c";
	static util ut = new util();
	static Scanner in = new Scanner(System.in);
	static String Ke;
	static String Tea;
	static String Di;

	public static String a_Ke() {
		return Ke;
	}

	public static String a_Tea() {
		return Tea;
	}

	public static String a_Di() {
		return Di;
	}

	public static int add(String ke, String teacher, String where) {
		conn = ut.getConn();
		String sql = "insert into kecheng values(?,?,?)";
		int b = 0;
		try {
			ps = conn.prepareStatement(sql);
			ps.setString(1, ke);
			ps.setString(2, teacher);
			ps.setString(3, where);
			int a = ps.executeUpdate();
			if (a > 0) {
				b++;
				System.out.println("��ӳɹ�");

			} else {
				System.out.println("���ʧ��");
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		try {
			if (ps != null)
				ps.close();
			if (conn != null)
				conn.close();
		} catch (Exception e2) {
			e2.printStackTrace();
		}
		return b;
	}

	public static int uqdate(String ke, String teacher, String where) {
		int b = 0;
		conn = ut.getConn();
		ps = null;
		sql = "update kecheng set teacher=?,where=? where ke=?";
		try {
			ps = conn.prepareStatement(sql);
			ps.setString(1, teacher);
			ps.setString(2, where);
			ps.setString(3, ke);
			int a = ps.executeUpdate();
			if (a > 0) {
				b++;
				System.out.println("�޸ijɹ�");

			} else {
				System.out.println("�޸�ʧ��");
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		try {
			if (ps != null)
				ps.close();
			if (conn != null)
				conn.close();
		} catch (Exception e2) {
			e2.printStackTrace();
		}
		return b;

	}

	public static int delete(String Ke) {
		int b = 0;
		conn = ut.getConn();
		ps = null;
		sql = "delete from kecheng where ke=?";

		try {
			ps = conn.prepareStatement(sql);
			ps.setString(1, Ke);
			int a = ps.executeUpdate();
			if (a > 0) {
				b++;
				System.out.println("ɾ���ɹ�");

			} else {
				System.out.println("ɾ��ʧ��");
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		try {
			if (ps != null)
				ps.close();
			if (conn != null)
				conn.close();
		} catch (Exception e2) {
			e2.printStackTrace();
		}
		return b;

	}

	public static void find(String a) {
		conn = ut.getConn();
		ps = null;
		ResultSet rs = null;
		String id;
		sql = "select * from kecheng where ke=?";
		try {
			ps = conn.prepareStatement(sql);
			ps.setString(1, a);
			rs = ps.executeQuery();
			if (rs.next()) {
				Ke = rs.getString("ke");
				Tea = rs.getString("teacher");
				Di = rs.getString("where"
						+ "");

			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			try {
				if (ps != null)
					ps.close();
				if (conn != null)
					conn.close();
			} catch (Exception e2) {
				e2.printStackTrace();
			}
		}
	}
	
}

  主函数调用

<%@page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<a href = "add.jsp">增加</a></br>
<a href = "find.jsp">查询 </a></br>
<a href = "delete.jsp">删除</a></br>
<a href = "update.jsp">修改</a></br>

</body>
</html>

  增:

<%@page import="ke.Test"%>
<%@page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>增加</title>
<script type="text/javascript"">

    function validate()

    {

    var Ke=document.forms[0].Ke.value;

    var Tea=document.forms[0].Tea.value;

    var Di=document.forms[0].Di.value;

    if(Ke.length<=0){

    	alert("课程不能为空,请输入课程!");

    	return false;

    }

    else if(Tea.length<=0){

    	alert("教师不能为空,请输入教师姓名!");

    	return false;

    }

    else if(age<=0){

    	alert("地点不能为空,请输入地点!");

    	return false;

    }

 

        else if(Di.length<=0){

    	alert("专业不能为空,请输入所学专业!");

    	return false;

    }

 

    else{

    	return true;

    }

    	//document.getElementById("form").submit();

    }

    </script>
</head>
<body>
 <form action="addresult.jsp" method="post" id="form" onSubmit="return validate()" >
+++++++++++++++++-------------------------------------------------------------------------------------------------------------------------------------------------
 <input type="hidden" name="methodName" value="0"/>

<h4>  课程:<input type="text" name="Ke"title="课程不能为空"></input><br></h4>

<h4>  任课教师:<input type="radio" name="Tea" value="Wangjianmin">王建民

            <input type="radio" name="Tea" value="liulijia">刘立嘉
            <input type="radio" name="Tea" value="liudan">刘丹
            <input type="radio" name="Tea" value="wanghui">王辉
            <input type="radio" name="Tea" value="yangziguang">杨子光
            <br></h4>
<h4>  上课地点:<input type="radio" name="Di" value="yijiao">一教
            <input type="radio" name="Di" value="erjiao">二教
            <input type="radio" name="Di" value="sanjiao">三教
            <input type="radio" name="Di" value="jijiao">基教
            <br></h4>
 <input type="submit" value="完成"/>
   </form>
 
</body>
</html>






<%@page import="ke.Test"%>
<%@page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>添加结果</title>
</head>
<body>
<%
 out.println(request.getParameter("Ke"));
 if(Test.add(request.getParameter("Ke"),request.getParameter("Tea"),request.getParameter("Di"))==1)
 {
 {
out.print("<script language = 'javascript'>alert('添加成功');</script>");
response.setHeader("refresh", "0;url=Zhu.jsp");
    }
 }
 else{
 out.print("<script language = 'javascript'>alert('添加失败');</script>");
 response.setHeader("refresh", "0;url=Zhu.jsp");
//out.println("添加失败");
 }
 %>
</body>
</html>
















  

 删:

<%@page import="ke.Test"%>
<%@page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>删除界面</title>
</head>
<body>
<form action="deleteresult.jsp" method="post" id="form" onSubmit="return validate()" >

 <input type="hidden" name="methodName" value="0"/>

<h4>  课程:<input type="text" name="Ke"title="课程不能为空"></input><br></h4>

<input type="submit" value="完成"/>
   </form>
 
</body>
</html>



<%@page import="ke.Test"%>
<%@page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>删除结果</title>
</head>
<body>
<%
 
 if(Test.delete(request.getParameter("Ke"))==1)
 {
	 {
			out.print("<script language = 'javascript'>alert('添加成功');</script>");
			response.setHeader("refresh", "0;url=Zhu.jsp");
		    }
 }
 else{
	 out.print("<script language = 'javascript'>alert('添加失败');</script>");
	 response.setHeader("refresh", "0;url=Zhu.jsp");
		//out.println("添加失败");
 }
 %>
</body>
</html>

 改:

<%@page import="ke.Test"%>
<%@page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>修改界面</title>
<script type="text/javascript"">

    function validate()

    {

    var Ke=document.forms[0].Ke.value;

    var Tea=document.forms[0].Tea.value;

    var Di=document.forms[0].Di.value;

    if(Ke.length<=0){

    	alert("课程不能为空,请输入课程!");

    	return false;

    }

    else if(Tea.length<=0){

    	alert("教师不能为空,请输入教师姓名!");

    	return false;

    }

    else if(age<=0){

    	alert("地点不能为空,请输入地点!");

    	return false;

    }

 

        else if(Di.length<=0){

    	alert("专业不能为空,请输入所学专业!");

    	return false;

    }

 

    else{

    	return true;

    }

    	//document.getElementById("form").submit();

    }

    </script>
</head>
<body>
 <form action="uqdateresult.jsp" method="post" id="form" onSubmit="return validate()" >

 <input type="hidden" name="methodName" value="0"/>

<h4>  课程:<input type="text" name="Ke"title="课程不能为空"></input><br></h4>

<h4>  任课教师:<input type="radio" name="Tea" value="Wangjianmin">王建民

            <input type="radio" name="Tea" value="liulijia">刘立嘉
            <input type="radio" name="Tea" value="liudan">刘丹
            <input type="radio" name="Tea" value="wanghui">王辉
            <input type="radio" name="Tea" value="yangziguang">杨子光
            <br></h4>
<h4>  上课地点:<input type="radio" name="Di" value="yijiao">一教
            <input type="radio" name="Di" value="erjiao">二教
            <input type="radio" name="Di" value="sanjiao">三教
            <input type="radio" name="Di" value="jijiao">基教
            <br></h4>
 <input type="submit" value="完成"/>
   </form>
 
</body>
</html>



<%@page import="ke.Test"%>
<%@page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>修改结果</title>
</head>
<body>
<%
 
 if(Test.uqdate(request.getParameter("Ke"),request.getParameter("Tea"),request.getParameter("Di"))==1)
 {
	 {
			out.print("<script language = 'javascript'>alert('删除成功');</script>");
			response.setHeader("refresh", "0;url=Zhu.jsp");
		    }
 }
 else{
	 out.print("<script language = 'javascript'>alert('删除失败');</script>");
	 response.setHeader("refresh", "0;url=Zhu.jsp");
		//out.println("添加失败");
 }
 %>
</body>
</html>

  查:

 

<%@page import="ke.Test"%>
<%@page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>查找界面</title>
<script type="text/javascript"">

    function validate()

    {

    var Ke=document.forms[0].Ke.value;

    var Tea=document.forms[0].Tea.value;

    var Di=document.forms[0].Di.value;

    if(Ke.length<=0){

    	alert("课程不能为空,请输入课程!");

    	return false;

    }

    else if(Tea.length<=0){

    	alert("教师不能为空,请输入教师姓名!");

    	return false;

    }

    else if(age<=0){

    	alert("地点不能为空,请输入地点!");

    	return false;

    }

 

        else if(Di.length<=0){

    	alert("专业不能为空,请输入所学专业!");

    	return false;

    }

 

    else{

    	return true;

    }

    	//document.getElementById("form").submit();

    }

    </script>
</head>
<body>
 <form action="findresult.jsp" method="post" id="form" onSubmit="return validate()" >

 <input type="hidden" name="methodName" value="0"/>

<h4>  课程:<input type="text" name="Ke"title="课程不能为空"></input><br></h4>
 <input type="submit" value="完成"/>
   </form>
 
</body>
</html>






<%@page import="ke.Test"%>
<%@page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>查找结果</title>
</head>
<body>
<%
 
 Test.find(request.getParameter("Ke"));
out.println(Test.a_Ke());
out.println(Test.a_Tea());
out.println(Test.a_Di());
		
 %>
</body>
</html>

  

posted @ 2018-12-09 22:55  竹林清竹  阅读(621)  评论(0编辑  收藏  举报