Learning materials "Crazy java handouts"
Environment: MYSQL
Java 1.7
Java operations Database with JDBC is one of the foundations of Java programming. Mastering SQL is the foundation of JDBC programming. JDBC is the interface API developed by Sun, and each database vendor provides implementation classes (drivers) based on the interface API, which is a typical application for interface programming.
SQL can be divided into two parts: Data manipulation Language (DML) and data definition language (DDL).
SQL (Structured Query language) is the syntax for executing queries. However, the SQL language also contains syntax for updating, inserting, and deleting records.
The query and update Directives form the DML portion of SQL:
- SELECT -get data from a database table
- Update-updating data in a database table
- Delete-deletes data from the database table
- INSERT INTO-inserts data into a database table
The Data definition language (DDL) portion of SQL gives us the ability to create or delete tables. We can also define indexes (keys), specify links between tables, and impose constraints between tables.
The most important DDL statement in SQL:
- CREATE database-creating new databases
- alter database-Modify Databases
- CREATE TABLE-Creates a new table
- ALTER TABLE -Change (change) database table
- Drop Table-delete tables
- CREATE index-Creating indexes (search key)
- Drop Index-delete indexes
MySQL Syntax:
show databases;
Use database name;
Show tables;
DESC Table Name--View the structure of the data table
bin\ mysql-p Password-u user name-h hostname--default-character-set=utf8 connection to remote host MySQL service
ALTER TABLE TableName Modify; Only one column can be modified at a time
ALTER TABLE tablename Rename to newname;
ALTER TABLE tablename Change column name to new column name type;
(constraint constraint name) constraint definition;---multi-column constraint
Primary key (column name)----Multi-column PRIMARY KEY constraint, cannot customize constraint name
Column name Type Auto_increment primary key---self-growing primary key
ALTER TABLE tablename DROP INDEX constraint name;
When establishing a FOREIGN key constraint, MySQL will index the column;
Column name type references table name (primary key name);
Foreign key referencing the primary key----Self-association of its own table
On Detete cascade/on Delete set null--primary key record deleted, also removed from key
CREATE INDEX index_name on table_name (column1, column2 ...);
Create or replace view view_name as subquery with CHECK option; Modify view data is not allowed
Dml:
insert INTO; Update Delete from;
concat ("xxx", "xxx") string connection
Distinct, in, like, is null
PS: It is not recommended to use a specific database function in a Java program, because it causes the program code to be coupled to a particular database, and if the migration is to reopen the source program to modify the SQL statement.
The difference between where and having
Where is used to filter rows, having to filter groups
The WHERE clause cannot use the Group function (AVG (), COUNT (), Max (), Min (), sum ()), and having can
Cross joins, natural join, join using, join on
Left joins, right joins, full join
Sub-query
Set Operation Select union/minus/
JDBC Common classes and interfaces: Several important classes or objects under the java.sql package
DriverManager
Connection
Statement
ResultSet
Example: Remember to add Mysql-connector-java jar package
Public classJdbctest { Public Static voidMain (string[] args)throwsclassnotfoundexception, SQLException, interruptedexception {//Load DriverClass.forName ("Com.mysql.jdbc.Driver"); Try{Connection conn= Drivermanager.getconnection ("Jdbc:mysql://127.0.0.1:3306/sakila", "root", "password"); Statement stmt=conn.createstatement (); ResultSet RS= Stmt.executequery ("SELECT * from City LIMIT 5"); while(Rs.next ()) {System.out.println (Rs.getint (1) + "\ T" + rs.getstring (2) + "\ T" + rs.getint (3) + "\ T" + rs.gettimestamp (4)); Thread.Sleep (3000); } } finally{}}} output:1 A Corua (La Corua) 2006-02-15 04:45:25.02 abha 2006-02-15 04:45:25.03 Abu Dhabi 101 200 6-02-15 04:45:25.04 Acua 2006-02-15 04:45:25.05 Adana 97 2006-02-15 04:45:25.0
//performing sql,preparedstatement vs. statement Public classPreparedstatementtest {PrivateString driver; PrivateString URL; PrivateString User; PrivateString Pass; Public voidInitparam (String paramfile)throwsException {Properties props=NewProperties (); Props.load (NewFileInputStream (paramfile)); Driver= Props.getproperty ("Driver"); URL= Props.getproperty ("url"); User= Props.getproperty ("User"); Pass= Props.getproperty ("Pass"); } Public voidInsertusestatement ()throwsException {class.forname (driver); LongStart =System.currenttimemillis (); Try(Connection Conn=(Connection) drivermanager.getconnection (Url,user,pass); Statement stmt=conn.createstatement ()) { for(inti=0;i<100;i++) {stmt.executeupdate ("INSERT into student_table values (" + "null, ' name" +i+ "', 1)"); } System.out.println ("Using statement:" + (System.currenttimemillis ()-start)); } } //PreparedStatement can prevent SQL injection,? Represents a placeholder Public voidInsertuseprepare ()throwsException {class.forname (driver); Longstart=System.currenttimemillis (); Try(Connection Conn=drivermanager.getconnection (Url,user,pass); PreparedStatement pstmt=conn.preparestatement ("INSERT into student_table values (null,?, 1)")) { for(intI =0;i<100;i++) {pstmt.setstring (1, "name" +i); Pstmt.executeupdate (); } System.out.println ("Using PreparedStatement:" + (System.currenttimemillis ()-start)); } } Public Static voidMain (string[] args)throwsException {//TODO auto-generated Method StubPreparedstatementtest pt =Newpreparedstatementtest (); Pt.initparam ("E:\\workspace\\learning\\mysql.ini"); Pt.insertusestatement (); Pt.insertuseprepare (); }} Output: Using statement:3423using PreparedStatement:3176View Code
JDBC Programming Java