Database SQL Server2012 Notes (vii)--JAVA program operations SQL Server

Source: Internet
Author: User

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
      • if (sm!=null)
                         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

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.