ZUCC 数据库原理——上机部分扼要
/ * @Author: Matchalater Presents * @Date: 2022/06/12 00:44 */
讯享网
1.配置环境
1.1建立数据库表
打开 Navicat for MySQL 新建数据库 使用utf-8字符集
然后将网站所给的建表格语句通过查询依次建立在本地。
1.2 建立Java Project
依照题意创建相应的Project
在导航栏的Project——properties——Java Build Path —— Classpath —— Add External JARS
导入对应的JAR包
然后开始修改DBUtil.java
我们需注意到:
讯享网public class DBUtil {
private static final String jdbcUrl = "jdbc:mysql://localhost:3306/booklib?useUnicode=true&characterEncoding=UTF-8"; //localhost:3306/ 后面的部分应当是我们建立的数据库的数据库名 private static final String dbUser = "root"; private static final String dbPwd = "12345"; static {
try {
Class.forName("com.mysql.jdbc.Driver"); } catch (ClassNotFoundException e) {
// TODO Auto-generated catch block e.printStackTrace(); } } //接下来我们需要攥写下面的getConncetion() 方法 //我们需要记住: //java.sql.DriverManager的getConnction方法 //然后依次填入数据库路径,用户名,密码三项 public static Connection getConnection() throws java.sql.SQLException {
return java.sql.DriverManager.getConnection(jdbcUrl, dbUser, dbPwd); } } package cn.edu.zucc.test; import java.sql.Connection; public class DBUtil {
//注意此处的dbname,须修改为你所建立的数据库名 private static final String jdbcUrl="jdbc:mysql://127.0.0.1:3306/dbname"; private static final String dbUser="root"; private static final String dbPwd=""; static{
try {
Class.forName("com.mysql.jdbc.Driver"); } catch (ClassNotFoundException e) {
e.printStackTrace(); } } //接下来我们需要攥写下面的getConncetion() 方法 //我们需要记住: //java.sql.DriverManager的getConnction方法 //然后依次填入数据库路径,用户名,密码三项 //public static Connection getConnection() throws java.sql.SQLException {
//return java.sql.DriverManager.getConnection(jdbcUrl, dbUser, dbPwd); // } public static Connection getConnection() throws java.sql.SQLException{
//请补全代码 return null; } }
1.3 调用主类Test查看是否配置成功
2.编写方法
2.1使用SQL语句的标准版模板
//建立连接类 Connection conn = null; try {
//载入连接 conn = DBUtil.getConnection(); //编写SQL语句 String sql = ""; //使用PreparedStatement载入SQL查询语句 java.sql.PreparedStatement pst = conn.prepareStatement(sql); //index start from 1 //对SQL语句中的"?"进行更改 pst.setString(1, readerId); //将查询结果载入rs之中,使用rs.next()读入数据 java.sql.ResultSet rs = pst.executeQuery(); //如果结果有多条,使用while(rs.next())逐个读入 //如果结果只有一条,直接使用if(rs.next())即可 while (rs.next()) {
//.... } rs.close(); pst.close(); //将结果做处理后输出 return result; } catch (SQLException e) {
e.printStackTrace(); throw new DbException(e); } finally {
if (conn != null) try {
conn.close(); } catch (SQLException e) {
// TODO Auto-generated catch block e.printStackTrace(); } }
2.2 相关的输出类
HashMap的简单使用
讯享网// Initialize Map</*Type1*/,/*Type2*/> /*MapName*/ = new HashMap<>(); //example: Map<String,int> TestMap = new HashMap<>(); // Put TestMap.put("str",int_type_element); // Get int return_int_elemet = TestMap.get("str"); // Traversal //*这个很有可能考 // Type1 Traversal Through 1st Type for(String x : TestMap.keySet()){
// x in this loop is the every value of type1 in this map } //Type2 Traversal Through 2rd Type for(int y : TestMap.value()){
// y in this loop is the every value of type2 in this map }
List的简单使用
// Initialize List<ele_type> result = new ArrayList<ele_type>(); //example: List<String> TestList = new ArrayList<String>(); //我们假设题目所需要输出的List类中的元素类型为 BeanBook,也可以这样设置List List<BeanBook> TestList = new ArrayList<BeanBook>(); //add //此处设置element为所给的正确的数据类型 TestList.add(element)
2.3 多条SQL查询语句
讯享网//方法1 //begining from the try catch conn.setAutoCommit(false); //in the end conn.commit(); //方法2 //每执行完一次查询语句后,有: pst.close(); //在finally语句中 if(conn!=null){
conn.close(); }
2.4 部分的类型转换 这个也很有可能会考*
//String <-> Date 的类型转换 //String to util.Date SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); String dateStr = "2019-01-03 10:59:27"; Date date = sdf.parse(dateStr); //util.Date to String SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); java.util.Date date=new java.util.Date(); String str=sdf.format(date); //util.Date to java.sql.Date 的转换 精确到秒 //使用timestamp; //System.currentTimeMillis() java.sql.Timestamp(long_type) pst.setTimestamp(x, new java.sql.Timestamp(System.currentTimeMillis()));
3.常用的SQL语句模板
讯享网//增 insert into <表名> values(?,?,?,?) //删 delete from <表名> where <限制条件> //查 select <项名> from <表名> where <限制条件> group by <分组依据的项> having <条件表达式> order by <列名> /DESC/ASC //改 update <表名> set <需要修改的项名> where <限制条件>

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容,请联系我们,一经查实,本站将立刻删除。
如需转载请保留出处:https://51itzy.com/kjqy/43919.html