数据库开发实训(Java篇)代码

子昊
2021-08-23 / 0 评论 / 279 阅读 / 正在检测是否收录...

关卡一

让我们愉快的(复制粘贴)开始吧!

 //请在此处补全创建数据库实例的SQL语句,不要改动其他代码
        /******* Begin ******/
         String sqlScript = "create database " + databaseName;  
        
        /******* End ******/

运行截屏:

关卡二

让我们愉快的(复制粘贴)开始吧!

//请在此处补全创建表的SQL语句,不要改动其他代码
          /******* Begin ******/
          String sqlScript =  "create table " + tableName + "(\n";
              for(int k=0; k<tableInfo.length; k++){
        if(tableInfo[k][0].equals(""))
            break;
        sqlScript = sqlScript + tableInfo[k][0] + " " + tableInfo[k][4] + ",\n";
        }
        sqlScript = sqlScript.substring(0, sqlScript.length()-2);
        sqlScript = sqlScript + ")";
          /******* End ******/

运行截屏:

关卡三

让我们愉快的(复制粘贴)开始吧!

//请在此处补全查询数据的SQL语句,不要改动其他代码
          /******* Begin ******/
           String sqlScript = "select distinct(publisher) from " + tableName + " where title = \'" + bookName + "\'";         
          /******* End ******/

运行截屏:

关卡四

让我们愉快的(复制粘贴)开始吧!

//请在此处补全向指定表插入数据的SQL语句,不要改动其他代码
          /******* Begin ******/
         String sqlScript = "Insert into book(id,title,author,publisher,publishYear)" + " values" + "(" + id +"," +"\'" + title+ "\',\'" + author + "\',\'" + publisher + "\',\'" + year + "\')";
       /******* End ******/

运行截屏:

关卡五

让我们愉快的(复制粘贴)开始吧!

public int deleteRecordByAuthor(Connection connection, String tableName,String author){ 
    // 请在此处添加数据库删除记录的实现代码
    Statement stmt = null;
    int updatedNum = 0;
    // 删除数据表中数据的SQL语句
    String sqlScript = "delete from " + tableName + " where author = \'" + author +"\'";
    try {
        stmt = connection.createStatement();
        updatedNum = stmt.executeUpdate(sqlScript);
    } catch (SQLException e) {
        e.printStackTrace();
    }
    return updatedNum;
}

运行截屏:

关卡六

让我们愉快的(复制粘贴)开始吧!

public int updatePublisherByTitle(Connection connection,String tableName, String title, String publisher){
    //请在此处实现数据更新功能
    Statement stmt = null;
    int updatedNum=0;
    String sqlScript = "update " + tableName + " set publisher=\'" + publisher + "\'where title=\'" + title + "\'";
    try {
        stmt = connection.createStatement();
        updatedNum = stmt.executeUpdate(sqlScript);
    } catch (SQLException e) {
        e.printStackTrace();
    }
    return updatedNum;
}

运行截屏:

关卡七

让我们愉快的(复制粘贴)开始吧!

//请在此处补全转换表的SQL语句,不要改动其他代码
        /******* Begin ******/    
         ResultSet result = null;
  Statement stmt = null;
  String sqlScript = "select * from " +    tableName + " order by id desc";
  try {
      stmt = connection.createStatement();
      result = stmt.executeQuery(sqlScript);
   } catch (SQLException e) {
     e.printStackTrace();
   }
 String sqlInsert = "insert into transformedBook (id,column_name,value) values (";
     while (result.next()) {
         String sql = "";
         String id = result.getString("id");
         String title=result.getString("title");
       if(title != null)
       {
         sql = sqlInsert + id + ",'" + "title" + "','" +  title + "');";
         InsertRecord(connection, sql);
       }
      String author =     result.getString("author");
      if(author != null)
      {
        sql = sqlInsert + id + ",'" + "author" + "','" +  author + "');";
       InsertRecord(connection, sql);
      }
     String publisher =  result.getString("publisher");
     if(publisher != null)
     {
       sql = sqlInsert + id + ",'" + "publisher" + "','" +  publisher + "');";
       InsertRecord(connection, sql);
     }
     String publishYear = result.getString("publishYear");
     if(publishYear != null)
     {
       sql = sqlInsert + id + ",'" + "publishYear" + "','" +  publishYear + "');";
       InsertRecord(connection, sql);
     }
     }
        /******* End ******/     

运行截屏:

{message type="success"}大功告成!{/message}

5

评论 (0)

取消