1. CRUD (Increase and deletion) Introduction: Create/retrieve/update/delete
2, JDBC Introduction 1) JDBC (Java database Connectivity,java connection) 2) is a Java API for running SQL statements that provides unified access to a variety of relational databases. Consists of a set of classes and interfaces written in the Java language. 3) JDBC provides a standard API for tool/database developers to build more advanced tools and interfaces that database developers can write database applications with pure Java APIs. 4) JDBC is also evasive indication.
3, JDBC driver classification 1) JDBC-ODBC Bridge Connection 2) Local protocol pure Java Driver 3) network protocol pure Java Driver 4) Local API
4, JDBC less than 1) JDBC is unified at the Java language level, but there are still many differences between databases. 2) Hibernate (cross-database operation) is a re-encapsulation of JDBC, which achieves a broader and more uniform and better portability of database operations.
5. JDBC-ODBC Bridge Connection operation SQL Server step: 1) Start--Control Panel--management tool--data source (ODBC)--ODBC data Source Manager--User dsn--Join--Create new data source--sql server--name--server " Local "or". " (both local)--next--"Use Windows NT Authentication"--next--"Change the default database to XXX"--complete--test data source
2) Connect the data source in the program (import java.sql.*;)
- Load driver (Increase memory for required driver)
-
- Class.forName ("Sun.jdbc.odbc.JdbcOdbcDriver")
- Get Connected (specify which data source to connect to, Username,password)
-
- Connection ct=drivermanager.getconnection ("JDBC:ODBC: Database Name", "sa", "password")
- Note: username and password are not required for Windows authentication.
- Create statement or PreparedStatement
-
- Statement is primarily used to send SQL statements to the database
- Statement sm=ct.creat Statement ();
- Run ( crud, create DATABASE, backup. Recovery )
-
- int I=sm.executeupdate ("INSERT INTO Dept values (' 50 ', ' Security Department ', ' West Wing ')"); I indicates the number of record entries successfully added
- Executeupdate can cud operation
- Close Resource
-
sm.close ();if (ct!=null)ct.close ();
6. Supplement:
- Delete a record
-
- int i= =sm.executeupdate ("Delete from dept where dept=50");
- Inquire
-
- ResultSet rs=sm.executequery ("SELECT * from dept");
While (Rs.next (0) {int Deptno=rs.getint (1);String dname=rs.getstring (2);String loc=rs.getstring (3); }
-
- rs points to the previous line of the first row of the result set and loops out.
Database SQL Server2012 Notes (vii)--JAVA program operations SQL Server