JDBC: Javadatabaseconnectivity is how java connects to the database. Example of a static query: importjava. SQL. *; publicclasstest {staticStringdrivercom. mysql. jdbc. Driver; staticStringurljdbc: mysql: 10.108.27.48thunder; staticString
JDBC: Java database connectivity is how java connects to the database. An example of static query import java. SQL. *; public class test {static String driver = "com. mysql. jdbc. driver "; static String url =" jdbc: mysql: // 10.108.27.48/thunder "; static String
JDBC: Java database connectivity is how java connects to the database.
An example of static Query
Import java. SQL. *; public class test {static String driver = "com. mysql. jdbc. driver "; static String url =" jdbc: mysql: // 10.108.27.48/thunder "; static String username =" thor "; static String password =" password "; static String SQL = "SELECT * FROM user"; PreparedStatement public static void main (String [] args) {Connection conn = null; Statement stmt = null; ResultSet rs = null; try {Class. forName (Driver); System. out. println ("the driver is loaded successfully. "); conn = DriverManager. getConnection (url, username, password); System. out. println ("database connection successful. "); stmt = conn. createStatement (); System. out. println ("statement created successfully. "); rs = stmt.exe cuteQuery (SQL); System. out. println ("the result is obtained successfully. "); while (rs. next () {String name = rs. getString ("username"); System. out. println (name) ;}} catch (Exception e) {e. printStackTrace ();} finally {Try {if (rs! = Null) rs. close (); if (stmt! = Null) stmt. close (); if (conn! = Null) conn. close () ;}catch (Exception e) {e. printStackTrace ();}}}}
Install the JDBC driver that supports MySQL in eclipse
Download the JDBC-supported MySQL driver at http://dev.mysql.com/downloads/connector/j /.
Open the downloaded package (mysql-connector-java-5.1.34.zip), copy the Java package (mysql-connector-java-5.1.34-bin.jar) to a directory for loading the driver.
Add the downloaded MySQL driver package (mysql-connector-java-5.1.34-bin.jar) to the Build path of the project. Choose Project> Properties, select the Java Build Path option> Libraries tab, select J2EE 1.3 Libraries, and click Add External JARs on the right.
Messy
It consists of three parts: static queries without parameters, static queries with parameters, and metadata retrieval.
It mainly involves four classes (or interfaces): DriverManager, Connection, Statement, and ResultSet.
In addition, PreparedStatement is the derived class of Statement and SQLException is the exception class.
Class |
Description |
DriverManager |
Load various drivers and return the corresponding database connection to the caller based on different requests. |
Connection) |
Database Connection, responsible for communicating with the database, SQL Execution and transaction processing are all performed in a specific Connection environment. You can generate Statement objects used to execute SQL statements. |
Statement (Interface) |
It is used to query and update static SQL statements without parameters and return execution results. |
ResultSet (Interface) |
Used to obtain SQL query results. |
PreparedStatement (Interface) |
It is used to query and update dynamic SQL statements containing parameters. |
SQLException |
Indicates that an exception occurred during database connection establishment, shutdown, or SQL statement execution. |
Classes/interfaces
1. DriverManager
public static Connection getConnection(String url, String user, String password);
public static Connection getConnection(String url);
Establish a connection between the JDBC driver and the specified database URL.
Jdbc url format of MySQL:
jdbc:mysql//[hostname][:port]/[dbname][?param1=value1][¶m2=value2]….
Example:jdbc:mysql://localhost:3306/sample_db?user=root&password=your_password
Common parameters:
Parameters |
Meaning |
User |
User Name |
Password |
Password |
AutoReconnect |
Online failed, whether to bring online again (true/false) |
MaxReconnect |
Number of attempts to reconnect |
InitialTimeout |
Attempt to reconnect to the interval |
MaxRows |
Maximum number of lines returned |
UseUnicode |
Whether to use Unicode font encoding (true/false) |
CharacterEncoding |
What encoding (GB2312/UTF-8 /...) |
RelaxAutocommit |
Whether to submit automatically (true/false) |
CapitalizeTypeNames |
Data Definition name in uppercase |
public static Driver getDrivers(String url);
Returns the driver of the database connection specified by the url.
Ii. Connection
Statement createStatement();
Create a Statement object to send SQL statements to the database.
Statement createStatement(int resultSetType, int resultSetConcurrency);
The resultSetType parameter specifies the result set type, which has three values:
ResultSet. TYPE_FORWARD_ONLY indicates that only the record pointer can be moved forward;
ResultSet. TYPE_SCROLL_INSENSITIVE indicates that the record pointer can be moved in two directions, but not updated in time. That is, if the data in the database has been modified, it is not reflected in the ResultSet;
ResultSet. TYPE_SCROLL_SENSITIVE indicates that the record pointer can be moved in two directions, and database updates can be tracked in time to change the data in the ResultSet.
The resultSetConcurrency parameter specifies the concurrency mode of the result set. There are two values:
ResultSet. CONCUR_READ_ONLY indicates that tables in the database cannot be updated using the result set;
ResultSet. CONCUR_UPDATABLE indicates that the table in the database can be updated using the result set.
PreparedStatement prepareStatement(String sql);
Create a PreparedStatement object to send dynamic SQL statements with parameters to the database.
void close();
Disconnect and release the database and JDBC resources of the Connection object.
boolean isClosed();
Determine whether the service is disabled.
III. Statement
ResultSet executeQuery(String sql);
Execute the given SQL statement and encapsulate the result in the result set object ResultSet to return the result.
int executeUpdate(String sql);
Execute a given SQL statement, which may be an INSERT, UPDATE, or DELETE statement, or an SQL statement (such as a DDL statement) that does not repent of any content ). The return value of this statement indicates the number of affected rows (that is, the update count ).
boolean execute(String sql);
Execute the SQL statement and indicate the form of the first result (in some cases, a single SQL statement may return multiple result sets or update counts ).
ResultSet getResultSet();
int getUpdateCount();
boolean getMoreResults();
void close();