20165320 第八周课下补做

知识点总结:

一、加载JDBC-数据库驱动。

        try{ Class. forName("com.mysql.jdbc.Driver");
            }
        catch(Exception e){}

二、连接数据库

        Connection con;
        String uri =
        "jdbc:mysql://192.168.100.1:3309/name?user=root&password=&useSSL=true";
        try{
            con = DriverManager.getConnection(uri);
        }
        catch(SQLException e){
            System.out.println(e);
        }

三、常规操作

向数据库发送SQL查询语句

try {Statement sql=con.createStatement();
}
catch(SQLException e) {}

处理查询结果,返回一个ResultSet对象

ResultSet rs = sql.executeQuery{"SELCET * FROM student");

关闭链接

con.close();

四、条件与排序查询

一般格式

select 字段 from 表名 where 条件

字段和固定值比较

select name from mess where name = ''

字段在某个区间范围

select * from mess where height>1.60 and height<=1.8

使用某些特殊的日期函数

select * from time_list where second(shijian)=56

用操作符like进行模式匹配

select * from mess where name like '%林%'

排序:用order by子语句对记录进行排序

selct * from mess order by height

运行结果截图

1.

2.

3.

教材代码例子分析

11.1:查询students数据库中mess表的全部记录,总共三条记录

R1001 张三 2000-12-12 178

R1002 李四 1999-10-09 168

R5320 申启 1998-02-03 180

11.2:随机查询students数据库中mess表中的两条记录,首先将光标移到最后一行,然后再获取最后一行的行号,以便获取表中的记录数目

随机数方法:

public static int[] getRandomNumber(int max,int amount) 

返回1~max之间的amount个不同的随机数

11.3:查询表中姓张,身高大于1.65,出生在2000之前,月份在7月之后的学生,并按出生日期排序

三个条件:

1.year(birthday)<=2000 and month(birthday)>7;

2.name Like '张_%';

3.height >1.65 ;

11.4:向mess表中插入如下两条记录:

R11,将三,2000-10-23

R10,李武,1989-7-22

11.5:使用预处理语句向mess表中添加记录并查询了姓张的记录

PreparedStatement preSql; //预处理语句对象

preSql = con.prepareStatement(sqlStr);//得到预处理对象

11.6:将数据库名以及SQL语句传递给Query类的对象,用表格显示所查到的记录

11.7:使用事务处理,将mess表中number字段是R1001的height的值减少n,并将减少的n增加到字段是R1002的height上

con.commit(); //开始事务处理,如果发生异常直接执行catch块

11.8:使用Derby数据库管理系统创建了名字是students的数据库,并在数据库中建立了成绩表:

张三 90  李斯 88  刘二 67

sta.execute(SQL); //创建表

教材习题:

1. 将select后的where条件改成order by birthday,其它的条件都删掉。

以下代码省略了GetDBCconnecttion类

importjava.sql.*;
public class BianCheng1{
    public static void main(String args[]) {
    Connection con;
    Statement sql;
    ResultSet rs;
    con = GetDBConnection.connectDB("students","root","");
    if(con == null )return;
    String sqlStr="select * frommess orderby birthday";
    try {
    sql=con.createStatement();
       rs = sql.executeQuery(sqlStr);
        while(rs.next()){
        String number=rs.getString(1);
         String name=rs.getString(2);
         Date date=rs.getDate(3);
        floatheight=rs.getFloat(4);
         System.out.printf("%s\t",number);
         System.out.printf("%s\t",name);
         System.out.printf("%s\t",date);
        System.out.printf("%.2f\n",height);
    }
    con.close();

    }
    catch(SQLException e) {
    System.out.println(e);
    }
 }

}

2.这个题目不会做,百度了一下代码.

同样省略了Query

import javax.swing.;
public class BianCheng2 {
public static void main(String args[]) {
String [] tableHead;
String [][] content;
JTable table
JFrame win= new JFrame();
Query findRecord = new Query();
findRecord.setDatabaseName(args[0]);
findRecord.setSQL("select from "+args[1]);
content = findRecord.getRecord();
tableHead=findRecord.getColumnName();
table = new JTable(content,tableHead);
win.add(new JScrollPane(table));
win.setBounds(12,100,400,200);
win.setVisible(true);
win.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
posted @ 2018-04-22 22:24  Gst丶Paul  阅读(193)  评论(0编辑  收藏  举报