Java Programming Week 16th Friday: Database connection and use of random numbers

Source: Internet
Author: User

Part I: Experimental projects

Item II: Preliminary database. Objective: To understand the steps and methods of Java connection database and the installation and use of MySQL database. Target:(1) The MySQL database is installed on the computer room.
successfully installed MySQL database
(2) Create data table student (ID varchar (), name varchar (), success int, failure int) and import the corresponding data into the file. The default value for success and failure is 1.
Create a student table
Connect Database Code
Import java.sql.*;p Ublic class Jdbctest {public static void main (string[] args) {//driver name String Drive           r = "Com.mysql.jdbc.Driver";           The URL points to the database name to access TYH3 String URL = "Jdbc:mysql://127.0.0.1:3306/tyh3";              MySQL configuration username String user = "root";           MySQL configuration when the password String password = "123456";            try {//Load driver Class.forName (driver);            Continuous database Connection conn = drivermanager.getconnection (URL, user, password);            if (!conn.isclosed ()) System.out.println ("succeeded connecting to the database!");            Statement used to execute SQL statements Statement Statement = Conn.createstatement ();            SQL statement to execute String sql = "SELECT * from student";            Result set ResultSet rs = statement.executequery (SQL);            System.out.println ("-----------------");            System.out.println ("Execution results are as follows:"); System.out.prinTLN ("-----------------");            System.out.println ("School number" + "\ T" + "name");            System.out.println ("-----------------");            String name = NULL;                 while (Rs.next ()) {//select sname This column of data name = Rs.getstring ("sname");             First, use the iso-8859-1 character set to decode name into a sequence of bytes and store the result in a new byte array.             Then use the GB2312 character set to decode the specified byte array name = new String (name.getbytes ("iso-8859-1"), "GB2312");            Output result System.out.println (rs.getstring ("sno") + "\ T" + name);            } rs.close ();           Conn.close ();             } catch (ClassNotFoundException e) {System.out.println ("Sorry,can ' t find the driver!");           E.printstacktrace ();           } catch (SQLException e) {e.printstacktrace ();           } catch (Exception e) {e.printstacktrace (); } } }


(3) when the guess is correct, the data table, success+1; guess error, failure+1. Related Courseware: The 10th Chapter: Database (Network Disk Download). Please practice the database operations, familiar with the Java database additions and deletions to change the operation.

Part II: Job Blog Requirements1. In the job blog, use this week and 17 weeks Monday morning time, complete two projects, and the results of the operation, the code to write to the blog.
2. In the job blog, answer the following four questions:
(1) What are the basic steps to connect a database in Java? What are the corresponding core classes and codes? 1 Loading the database driver
class.forname ("Com.jdbc.mysql.Driver");
2. Connect to the database
Connection con = drivermanager.getconnection (Url,user,password);
3. Create a statement object
Statement stmt = Con.createstatement ();
4. Query results displayed with ResultSet
String sql = "";
ResultSet rs = (ResultSet) stmt.executequery (SQL);

(2) Brief description of MySQL, what is the SQL statement that creates a data table? command: Create databases < database name >
(3) What is the core code for generating random numbers between [1,53] in Java?
public class Random () {public  static void Main (String [] args) {    int ran = (int) (Math.random () *53 + 1);     System.out.println (RAN);   }}

(4) What are the classes that read and write text files in Java? What are the core codes, respectively?
it should be .inputstream\outputstream
fileinpustream\fileoutputstream it

Java Programming Week 16th Friday: Database connection and use of random numbers

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.