持久化歷史實物記錄(繩結(jié)、泥板、竹片、絹布、紙張)電子文件數(shù)據(jù)庫管理系統(tǒng)關(guān)系型數(shù)據(jù)庫Relation in Co" />
時間:2023-10-09 10:36:01 | 來源:網(wǎng)站運營
時間:2023-10-09 10:36:01 來源:網(wǎng)站運營
Web開發(fā)基礎(chǔ)-07-數(shù)據(jù)設(shè)計:知識:數(shù)據(jù)被記錄(record)、存儲(storage)、回憶(recall)。
mysql -u root -p
ALTER USER 'root'@'localhost' IDENTIFIED BY '[your new password]';
//1.加載驅(qū)動程序Class.forName("com.mysql.jdbc.Driver");//2. 獲得數(shù)據(jù)庫連接Connection conn = DriverManager.getConnection(URL, USER, PASSWORD);//3.操作數(shù)據(jù)庫,實現(xiàn)增刪改查Statement stmt = conn.createStatement();ResultSet rs = stmt.executeQuery("SELECT user_name, age FROM imooc_goddess");//如果有數(shù)據(jù),rs.next()返回truewhile(rs.next()){ System.out.println(rs.getString("user_name")+" 年齡:"+rs.getInt("age"));}
public interface PetDao { /** * 查詢所有寵物 */ List<Pet> findAllPets() throws Exception;}
DAO 實現(xiàn)類:public class PetDaoImpl extends BaseDao implements PetDao { /** * 查詢所有寵物 */ public List<Pet> findAllPets() throws Exception { Connection conn=BaseDao.getConnection(); String sql="select * from pet"; PreparedStatement stmt= conn.prepareStatement(sql); ResultSet rs= stmt.executeQuery(); List<Pet> petList=new ArrayList<Pet>(); while(rs.next()) { Pet pet=new Pet( rs.getInt("id"), rs.getInt("owner_id"), rs.getInt("store_id"), rs.getString("name"), rs.getString("type_name"), rs.getInt("health"), rs.getInt("love"), rs.getDate("birthday") ); petList.add(pet); } BaseDao.closeAll(conn, stmt, rs); return petList; }}
寵物實體類(里面get/set方法就不列出了)public class Pet { private Integer id; private Integer ownerId; //主人ID private Integer storeId; //商店ID private String name; //姓名 private String typeName; //類型 private int health; //健康值 private int love; //愛心值 private Date birthday; //生日}
連接數(shù)據(jù)庫public class BaseDao { private static String driver="com.mysql.jdbc.Driver"; private static String url="jdbc:mysql://127.0.0.1:3306/epet"; private static String user="root"; private static String password="root"; static { try { Class.forName(driver); } catch (ClassNotFoundException e) { e.printStackTrace(); } } public static Connection getConnection() throws SQLException { return DriverManager.getConnection(url, user, password); } public static void closeAll(Connection conn,Statement stmt,ResultSet rs) throws SQLException { if(rs!=null) { rs.close(); } if(stmt!=null) { stmt.close(); } if(conn!=null) { conn.close(); } } public int executeSQL(String preparedSql, Object[] param) throws ClassNotFoundException { Connection conn = null; PreparedStatement pstmt = null; /* 處理SQL,執(zhí)行SQL */ try { conn = getConnection(); // 得到數(shù)據(jù)庫連接 pstmt = conn.prepareStatement(preparedSql); // 得到PreparedStatement對象 if (param != null) { for (int i = 0; i < param.length; i++) { pstmt.setObject(i + 1, param[i]); // 為預(yù)編譯sql設(shè)置參數(shù) } } ResultSet num = pstmt.executeQuery(); // 執(zhí)行SQL語句 } catch (SQLException e) { e.printStackTrace(); // 處理SQLException異常 } finally { try { BaseDao.closeAll(conn, pstmt, null); } catch (SQLException e) { e.printStackTrace(); } } return 0; }}
@Mapper@Repositorypublic interface AdminMapper { int addManager(User user); List<User> getAllManagers();}public class User { //省略getter、setter private Integer id; private String email; private String password; private String userName; private String phoneNumber; private double credit; private UserType userType;}<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"><mapper namespace="com.example.hotel.data.admin.AdminMapper"> <insert id="addManager" parameterType="com.example.hotel.po.User" useGeneratedKeys="true" keyProperty="id"> insert into User(email,password,usertype) values(#{email},#{password},#{userType}) </insert> <select id="getAllManagers" resultMap="User"> select * from User where usertype='HotelManager' </select> <resultMap id="User" type="com.example.hotel.po.User"> <id column="id" property="id"></id> <result column="email" property="email"></result> <result column="password" property="password"></result> <result column="username" property="userNzame"></result> <result column="phonenumber" property="phoneNumber"></result> <result column="credit" property="credit"></result> <result column="usertype" property="userType"></result> </resultMap></mapper>
參考鏈接關(guān)鍵詞:數(shù)據(jù),設(shè)計,基礎(chǔ)
微信公眾號
版權(quán)所有? 億企邦 1997-2025 保留一切法律許可權(quán)利。