此次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"%>增加
删:
<%@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"%>删除界面
改:
<%@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"%>修改界面
查:
<%@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"%>查找界面