博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
课程管理系统
阅读量:5086 次
发布时间:2019-06-13

本文共 7232 字,大约阅读时间需要 24 分钟。

    此次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"%>
Insert title here增加
查询
删除
修改

  增:

<%@page import="ke.Test"%><%@page language="java" contentType="text/html; charset=UTF-8"pageEncoding="UTF-8"%>
增加
+++++++++++++++++-------------------------------------------------------------------------------------------------------------------------------------------------

课程:

任课教师:王建民 刘立嘉 刘丹 王辉 杨子光

上课地点:一教 二教 三教 基教

<%@page import="ke.Test"%> <%@page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
添加结果 <%  out.println(request.getParameter("Ke"));  if(Test.add(request.getParameter("Ke"),request.getParameter("Tea"),request.getParameter("Di"))==1)  {
 {
out.print(""); response.setHeader("refresh", "0;url=Zhu.jsp");     }  }  else{
 out.print("");  response.setHeader("refresh", "0;url=Zhu.jsp"); //out.println("添加失败");  }  %>

  

 删:

<%@page import="ke.Test"%><%@page language="java" contentType="text/html; charset=UTF-8"pageEncoding="UTF-8"%>
删除界面

课程:

<%@page import="ke.Test"%><%@page language="java" contentType="text/html; charset=UTF-8"pageEncoding="UTF-8"%>
删除结果<% if(Test.delete(request.getParameter("Ke"))==1) { { out.print(""); response.setHeader("refresh", "0;url=Zhu.jsp"); } } else{ out.print(""); response.setHeader("refresh", "0;url=Zhu.jsp"); //out.println("添加失败"); } %>

 改:

<%@page import="ke.Test"%><%@page language="java" contentType="text/html; charset=UTF-8"pageEncoding="UTF-8"%>
修改界面

课程:

任课教师:王建民 刘立嘉 刘丹 王辉 杨子光

上课地点:一教 二教 三教 基教

<%@page import="ke.Test"%><%@page language="java" contentType="text/html; charset=UTF-8"pageEncoding="UTF-8"%>
修改结果<% if(Test.uqdate(request.getParameter("Ke"),request.getParameter("Tea"),request.getParameter("Di"))==1) { { out.print(""); response.setHeader("refresh", "0;url=Zhu.jsp"); } } else{ out.print(""); response.setHeader("refresh", "0;url=Zhu.jsp"); //out.println("添加失败"); } %>

  查:

 

<%@page import="ke.Test"%><%@page language="java" contentType="text/html; charset=UTF-8"pageEncoding="UTF-8"%>
查找界面

课程:

<%@page import="ke.Test"%><%@page language="java" contentType="text/html; charset=UTF-8"pageEncoding="UTF-8"%>
查找结果<% Test.find(request.getParameter("Ke"));out.println(Test.a_Ke());out.println(Test.a_Tea());out.println(Test.a_Di()); %>

  

转载于:https://www.cnblogs.com/NCLONG/p/10093944.html

你可能感兴趣的文章
经典入门_排序
查看>>
Redis Cluster高可用集群在线迁移操作记录【转】
查看>>
二、spring中装配bean
查看>>
VIM工具
查看>>
javascript闭包
查看>>
@Column标记持久化详细说明
查看>>
创建本地yum软件源,为本地Package安装Cloudera Manager、Cloudera Hadoop及Impala做准备...
查看>>
mysql8.0.13下载与安装图文教程
查看>>
站立会议08(冲刺2)
查看>>
url查询参数解析
查看>>
http://coolshell.cn/articles/10910.html
查看>>
[转]jsbsim基础概念
查看>>
DIV和SPAN的区别
查看>>
第一次使用cnblogs
查看>>
C#语法糖之 session操作类 asp.net
查看>>
2015 Multi-University Training Contest 3
查看>>
使用Gitblit 在windows 上部署你的Git Server
查看>>
217. Contains Duplicate
查看>>
vue2.0 关于Vue实例的生命周期
查看>>
jenkins 更换主数据目录
查看>>