Operating code for database part knowledge using Java

Source: Internet
Author: User
Tags mysql functions readline
This week, the first temporary end of Java advanced knowledge, into the database learning:

Java Advanced section:
1. Multithreading: Thread concurrency (multiple thread operations share variables);
Lock mechanism, keywords have synchronize (concurrent thread safety, lockable objects and methods), wait,notify
(pessimistic) deadlock, wait,notify,notifyall;
2. Network programming: UDP data Broadcast (data sender only need to send a copy to the switch, the switch is responsible for this information to make n copies sent to all machines)
HTTP protocol (hypertext Transfer Protocol)
JSON data format, Syntax rules: JSON object {"Property name": "Property Value"}
JSON array ["Element 1", "Element 2" ...]
Json Plugin:-Json-lib
-Gson
-Jackson
-Fastjson-alibaba

Database section: (using mysql5.5 database, and Navicat graphical tools to manipulate it)
3. System and MySQL common commands
System commands (run as Administrator)

: #启动服务
net start MySQL

#停止服务
net stop MySQL


#进入mysql命令行
Mysql-uroot-p Password

#退出mysql命令行
Exit


#修改密码
mysqladmin-uroot-p123456 Password Password
#备份数据库实例 Mysqldump-uroot-proot mydb > D:/mydb.sql
#备份表 mysqldump-uroot-proot mydb tbuser > D:/tbuser.sql

MySQL Common commands

:--Show DB instance
show databases;

--Create a DB instance
Create DATABASE MyDB;

--Using a DB instance
User mydb;

--Show all database tables in the instance
Show tables;

SQL statements

Auto_increment: Set column increment, available for primary and non-null unique columns (NOT NULL unique)


Unsigned: Set unsigned columns (column values are not allowed to be negative)


Zerofill: Set 0 Fill column (the value is 0 before the length of the column data is defined)

4.SQL statement:
DDL (Database definition language: Used to build databases, database objects, and define their columns): Create, DESC (view table structure), ALTER, drop
DML (Database Manipulation language: additions and deletions): SELECT, INSERT, delete, update;
DCL (Database Control Language: control permissions) revork,grant;
5. (Integrity condition) constraint: 1. PRIMARY KEY constraint
2. FOREIGN KEY constraints
3. Not a NULL constraint
4. Unique constraints
5. Check the constraints (MySQL is not supported temporarily)

6. Data types, operators
7. Query (FOCUS): SELECT query column 1, query column 2,...
From target table


"WHERE Query Condition"

"GROUP By Column name"


"Having a query condition"

"ORDER by column name asc| DESC "


"LIMIT [offset row,] record line Count"

Single table query: Fuzzy query ("%", "_"), aggregate function
Multi-table query: Equivalent connection, outer connection
The use of MySQL functions.

Import java.io.serializable;/** * Work details class * @author Niuxuyuan */public class jobs implements serializable{/** * */private s          Tatic final Long Serialversionuid = 1l;private String id;  Idprivate String experience;        Working experience private String city;    Work place private String industry;      Industry private String Detail;     Job Details private String company;     Company Private String JobName; Jobs public jobs (string ID, string experience, String city, string industry, string detail, string company,string jobname) {super (); this.id = Id;this.experience = experience;this.city = City;this.industry = Industry;this.detail = detail; This.company = Company;this.jobname = JobName;} @Overridepublic String toString () {return "Jobs [id=" + ID + ", experience=" + Experience + ", city=" + City + ", industry = "+ industry+", detail= "+ Detail +", company= "+ company +", jobname= "+ JobName +"] ";} Public String toString (int i) {return experience+city+industry+detail+company+jobname;} Public Jobs () {//TODO auto-Generated constructor stub}public String getId () {return ID;} public void SetId (String id) {this.id = ID;} Public String getexperience () {return experience;} public void Setexperience (String experience) {this.experience = experience;} Public String getcity () {return city;} public void Setcity (String city) {this.city = city;} Public String Getindustry () {return industry;} public void Setindustry (String industry) {this.industry = industry;} Public String Getdetail () {return detail;} public void Setdetail (String detail) {this.detail = detail;} Public String Getcompany () {return company;} public void Setcompany (String company) {this.company = company;} Public String Getjobname () {return jobname;} public void Setjobname (String jobname) {this.jobname = JobName;}}
Import Java.io.bufferedreader;import Java.io.file;import Java.io.fileinputstream;import Java.io.filenotfoundexception;import Java.io.fileoutputstream;import Java.io.ioexception;import Java.io.inputstreamreader;import Java.io.objectinputstream;import Java.io.objectoutputstream;import Java.util.arraylist;import Java.util.list;public class Addjobs {static list<jobs> List = new arraylist<> (); File File = new file ("Jobs");/** * Input data * @throws ioexception */public void input () throws IOException {Bufferedreade     R br = new BufferedReader (new InputStreamReader (system.in));     System.out.println ("Fill in id/experience/education/city/industry/detail/company/jobname format:");     String msg = ""; while (!) (     msg = Br.readline ()). Equalsignorecase ("quit")) {Add (msg); } br.close ();} /** * Convert data into a jobs object into the list collection * @param msg */private void Add (String msg) {string[] s = msg.split ("/"); Jobs job = new jobs (S[0], s[1], s[2], s[3], s[4], s[5], s[6]); List.add (job);} private void Checkfile () tHrows FileNotFoundException, IOException, classnotfoundexception {if (File.length () >0) {ObjectInputStream ois = new ObjectInputStream (new FileInputStream (file)); list<jobs> temp = (list<jobs>) ois.readobject (); if (temp!=null) {list.clear (); for (Jobs t:temp) {List.add (t );}} Ois.close ();}}  public static void Main (string[] args) throws FileNotFoundException, IOException, classnotfoundexception {addjobs AJ = new Addjobs (); if (!aj.file.exists ()) {aj.file.createNewFile ();} Aj.checkfile (); Aj.input () ObjectOutputStream oos = new ObjectOutputStream (new FileOutputStream (Aj.file)); O Os.writeobject (list); Oos.close ();}}
Import Java.io.bufferedreader;import Java.io.file;import Java.io.fileinputstream;import Java.io.filenotfoundexception;import Java.io.ioexception;import Java.io.inputstreamreader;import Java.io.objectinputstream;import Java.util.arraylist;import Java.util.list;public class Query {static List<Jobs > list = new arraylist<> (); File File = new file ("Jobs");/** * View file, import data into List collection * @throws filenotfoundexception * @throws IOException * @throws Cl Assnotfoundexception */private void Checkfile () throws FileNotFoundException, IOException, classnotfoundexception {if ( File.length () >0) {ObjectInputStream ois = new ObjectInputStream (new FileInputStream (file)); list<jobs> temp = (list<jobs>) ois.readobject (); if (temp!=null) {list.clear (); for (Jobs t:temp) {List.add (t );}} Ois.close ();}} public void Check () throws IOException {BufferedReader br = new BufferedReader (new InputStreamReader (system.in)); System.out.println ("Please enter some letters of Experience/education/city/industry/detail/company/jobname"); String msg = Br.readline (); String[] s = msg.split ("/"); String regex = ""; for (string str:s) {regex + = "[\\s\\s]*" + str + "[\\s\\s]*";} list<jobs> temp = new arraylist<> (); for (Jobs j:list) {msg = j.tostring (1); if (Msg.matches (regex)) {Temp.add ( j);}} SYSTEM.OUT.PRINTLN ("result"); for (jobs jobs:temp) {System.out.println (jobs);}} public static void Main (string[] args) throws FileNotFoundException, ClassNotFoundException, ioexception {Query q = new Qu Ery (); Q.checkfile (); Q.check ();}}

Related articles:

Java connection MySQL database and simple operation code

Database Basics

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.