第16周周五:数据库连接与随机数的使用 问题

来源:互联网 发布:矢量图编辑软件 编辑:程序博客网 时间:2024/06/05 09:51

(1)简述Java中,连接数据库有哪几个基本步骤? 其相应的核心类与代码分别是什么?

1.安装JDK.然后安装MySQL 2.配置两个文件 3.创建数据库 4.创建表 5.插入数据 

6.驱动程序名

String driver = "com.mysql.jdbc.Driver";

// URL指向要访问的数据库名scutcs

String url = "jdbc:mysql://127.0.0.1:3306/scutcs";

// MySQL配置时的用户名

String user = "root";

// Java连接MySQL配置时的密码

String password = "root";

try {

// 加载驱动程序

Class.forName(driver);

// 连续数据库

Connection conn = DriverManager.getConnection(url, user, password);

if(!conn.isClosed())

System.out.println("Succeeded connecting to the Database!");

// statement用来执行SQL语句

Statement statement = conn.createStatement();

// 要执行的SQL语句

String sql = "select * from student";

结果集

  1. ResultSet rs = statement.executeQuery(sql);  
  2. System.out.println("-----------------");  
  3. System.out.println("执行结果如下所示:");  
  4. System.out.println("-----------------");  
  5. System.out.println(" 学号" + "\t" + " 姓名");  
  6. System.out.println("-----------------");  
  7. String name = null;  
  8. while(rs.next()) {  

选择sname这列数据

name = rs.getString("sname");

// 首先使用ISO-8859-1字符集将name解码为字节序列并将结果存储新的字节数组中。

// 然后使用GB2312字符集解码指定的字节数组

name = new String(name.getBytes("ISO-8859-1"),"GB2312");

// 输出结果

  1. System.out.println(rs.getString("sno") + "\t" + name);  
  2. }  
  3. rs.close();  
  4. conn.close();   
  5. } catch(ClassNotFoundException e) {   
  6. System.out.println("Sorry,can`t find the Driver!");   
  7. e.printStackTrace();   
  8. } catch(SQLException e) {   
  9. e.printStackTrace();   
  10. } catch(Exception e) {   
  11. e.printStackTrace();   
  12. }   
  13. }   
  14. }  
 

(2)简述MySQL中,创建一个数据表的SQL语句是什么?

  1. CREATE TABLE STUDENT  
  2. (  
  3. SNO CHAR(7) NOT NULL,  
  4. SNAME VARCHAR(8) NOT NULL,  
  5. SEX CHAR(2) NOT NULL,  
  6. BDATE DATE NOT NULL,  
  7. HEIGHT DEC(5,2) DEFAULT 000.00,  
  8. PRIMARY KEY(SNO)  
  9. );  

(3) 简述Java中,生成[1,53]之间的随机数的核心代码分别是什么?

import java.util.Random;

public class RandomTest {
    public static void main(String[] args) {
        int max=53;
        int min=1;
        Random random = new Random();

        int s = random.nextInt(max)%(max-min+1) + min;
        System.out.println(s);
    }
}


(4)简述Java中,读写文本文件的类分别是什么?核心代码分别是什么? 

1、按字节读取文件内容  2、按字符读取文件内容  3、按行读取文件内容  4、随机读取文件内容 
public final class AccessTextFile {

    /**
     * 1. 演示将流中的文本读入一个 StringBuffer 中
     * @throws IOException
     */
    public void readToBuffer(StringBuffer buffer, InputStream is)
        throws IOException {
        String line;        // 用来保存每行读取的内容
        BufferedReader reader = new BufferedReader(new InputStreamReader(is));
        line = reader.readLine();       // 读取第一行
        while (line != null) {          // 如果 line 为空说明读完了
            buffer.append(line);        // 将读到的内容添加到 buffer 中
            buffer.append("\n");        // 添加换行符
            line = reader.readLine();   // 读取下一行
        }
    }

    /**
     * 2. 演示将 StringBuffer 中的内容读出到流中
     */
    public void writeFromBuffer(StringBuffer buffer, OutputStream os) {
        // 用 PrintStream 可以方便的把内容输出到输出流中
        // 其对象的用法和 System.out 一样
        // (System.out 本身就是 PrintStream 对象)
        PrintStream ps = new PrintStream(os);   
        ps.print(buffer.toString());
    }

    /**
     * 3*. 从输入流中拷贝内容到输入流中
     * @throws IOException
     */
    public void copyStream(InputStream is, OutputStream os) throws IOException {
        // 这个读过过程可以参阅 readToBuffer 中的注释
        String line;
        BufferedReader reader = new BufferedReader(new InputStreamReader(is));
        PrintWriter writer = new PrintWriter(new OutputStreamWriter(os));
        line = reader.readLine();
        while (line != null) {
            writer.println(line);
            line = reader.readLine();
        }
        writer.flush();     // 最后确定要把输出流中的东西都写出去了
                            // 这里不关闭 writer 是因为 os 是从外面传进来的
                            // 既然不是从这里打开的,也就不从这里关闭
                            // 如果关闭的 writer,封装在里面的 os 也就被关了
    }

    /**
     * 3. 调用 copyStream(InputStream, OutputStream) 方法拷贝文本文件
     */
    public void copyTextFile(String inFilename, String outFilename)
        throws IOException {
        // 先根据输入/输出文件生成相应的输入/输出流
        InputStream is = new FileInputStream(inFilename);
        OutputStream os = new FileOutputStream(outFilename);
        copyStream(is, os);     // 用 copyStream 拷贝内容
        is.close(); // is 是在这里打开的,所以需要关闭
        os.close(); // os 是在这里打开的,所以需要关闭
    }

0 0
原创粉丝点击