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

Source: Internet
Author: User
Tags stmt

Part I: Experimental projects

Item II: Preliminary database.

Purpose: To understand the steps and methods of connecting a database to Java. and the installation and use of MySQL database.

Target:(1) The MySQL database is installed on the computer room.
Install the MySQL database successfully
(2) Create data table student (ID varchar (), name varchar (+), success int, failure int). 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 you want to access TYH3 String URL = "Jdbc:mysql://127.0.0.1:3306/tyh3";              Username String user = "root" when MySQL is configured;           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 run SQL statements Statement Statement = Conn.createstatement ();            SQL statement to run String sql = "SELECT * from student";            Result set ResultSet rs = statement.executequery (SQL);            System.out.println ("-----------------");            SYSTEM.OUT.PRINTLN ("Running results such as the following see:"); 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 guessing correctly, the data sheet, success+1. When the error is being pushed. 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, completed two projects, and the implementation results, code written to the blog.
2. In the Homework blog, answer the following four questions:
(1) brief description of Java. What are the basic steps to connecting a database? What are their corresponding core classes and code? 1 Loading Database Drivers
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)   Description in Java, What is the core code for random numbers between [1,53]?
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?
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.