Connect to an msql statement using JDBC
1. // load the drive and call Java. lang. the forname method of the class to pass the name of the JDBC driver to be loaded. during runtime, the Class Loader locates and loads the JDBC driver class from the classpath environment variable. after the JDBC Driver Class is loaded. an instance of the driver class needs to be registered. class. forname ("com. microsoft. JDBC. sqlserver. sqlserverdriver "); 2. // call Java. SQL. drivermanager. the getconnection static method obtains the database connection and returns a java. SQL. connection object connection conn = drivermanager. getconnection ("JDBC: Microsoft: sqlserver: // localhost: 1433;" +
"Databasename = login", "sa", "123456 ");
3. // create a statement call the createstatement () method of the connection object to create a statement object. Statement stmt = conn. createstatement (); 4. // define an SQL statement
String SQL = "SQL statement"; 5. // use the statement object to execute the SQL statement resultset rs1_stmt.exe cutequery (SQL );
java.sql
Package content
java.sql
The package contains APIs for the following purposes:
- Pass
DriverManager
The utility establishes a connection to the database.
DriverManager
Class: establish a connection with the driver
SQLPermission
Class: When the code runs in Security Manager (such as applet ),DriverManager
Set a record stream
Driver
Interface: provides APIs for registering and connecting drivers based on the JDBC technology ("JDBC driver ").DriverManager
Class usage
DriverPropertyInfo
Class: Provides the attributes of the JDBC driver, not for general users.
- Send SQL statements to the database
Statement
: Used to send basic SQL statements
PreparedStatement
: Used to send prepared statements or basic SQL statements (derived fromStatement
)
CallableStatement
: Used to call database stored procedures (derived fromPreparedStatement
)
Connection
Interface: provides methods to create statements and manage connections and their attributes.
Savepoint
: Provides storage points in transactions.
- Search and update query results
- Standard ing between classes and interfaces of SQL to Java programming languages
Array
Interface: SQLARRAY
Ing relationship
Blob
Interface: SQLBLOB
Ing relationship
Clob
Interface: SQLCLOB
Ing relationship
Date
Class: SQLDATE
Ing relationship
Ref
Interface: SQLREF
Ing relationship
Struct
Interface: SQLSTRUCT
Ing relationship
Time
Class: SQLTIME
Ing relationship
Timestamp
Class: SQLTIMESTAMP
Ing relationship
Types
Class: Provides constants for the SQL type.
- Custom ing SQL user-defined type (UDT) to classes in Java programming language
SQLData
Interface: Specifies the ing between a UDT instance and an instance of this type.
SQLInput
Interface: provides a method to read the UDT attribute from the stream.
SQLOutput
Interface: provides a method for writing the UDT attribute back.
- Metadata
DatabaseMetaData
Interface: Provide information about the database
ResultSetMetaData
Interface: provides relatedResultSet
Object column information
ParameterMetaData
Interface: provides relatedPreparedStatement
Command Parameter Information
- Exception
SQLException
: Throws are thrown by most methods when data access fails, and by other methods for other reasons.
SQLWarning
: Throw to indicate a warning
DataTruncation
: Throws to indicate that the data may have been truncated.
BatchUpdateException
: It is thrown to indicate that not all commands in batch update are successfully executed.