Connect to the database using JDBC and connect to the database using jdbc
JDBC (Java Data Base Connectivity) database connection, we need to use JDBC when writing web applications or java applications to connect to the database. To connect to a database using JDBC, follow these steps:
1. Load the driver Class. forName (driver );
2. Create the Connection object Connection con = DriverManager. getConnection (url, username, password );
3. Create an SQL statement execution object
4. Execute SQL statements
5. Process execution results
6. Close related connection objects (the order is opposite to the declared order)
The following is an example of establishing a connection with a MySQL database. The process of other databases is similar.
Import java. SQL. Connection;
Import java. SQL. DriverManager;
Import java. SQL. ResultSet;
Import java. SQL. SQLException;
Import java. SQL. Statement;
Public class DBConnection
{
Public static void main (String [] args)
{
String driver = "com. mysql. jdbc. Driver ";
// Localhost indicates the local host, which can also be replaced by a local IP address. 3306 indicates the default port number of the MySQL database, and "user" indicates the name of the database to be connected.
String url = "jdbc: mysql: // localhost: 3306/user ";
// Enter the username and password of the database.
String username = "test ";
String password = "test ";
String SQL = "select * from user"; // write the SQL statement to be executed. Here, the information of all users is queried from the user table.
Try
{
Class. forName (driver); // load the driver. Here, the method of implicitly registering the driver is used.
}
Catch (ClassNotFoundException e)
{
E. printStackTrace ();
}
Try
{
Connection con = DriverManager. getConnection (url, username, password); // create a Connection object
Statement st = con. createStatement (); // create an SQL Execution object
ResultSet rs = st.exe cuteQuery (SQL); // execute the SQL statement and return the result set
While (rs. next () // traverses the result set and outputs it
{
System. out. println ("username:" + rs. getString (1); // obtain data by column label
System. out. println ("useradd:" + rs. getString ("useradd"); // obtain data by column name
System. out. println ("userage:" + rs. getInt ("userage "));
}
// Close related objects
If (rs! = Null)
{
Try
{
Rs. close ();
}
Catch (SQLException e)
{
E. printStackTrace ();
}
}
If (st! = Null)
{
Try
{
St. close ();
}
Catch (SQLException e)
{
E. printStackTrace ();
}
}
If (con! = Null)
{
Try
{
Con. close ();
}
Catch (SQLException e)
{
E. printStackTrace ();
}
}
}
Catch (SQLException e)
{
E. printStackTrace ();
}
}
}
How does one connect to a database using JDBC in java?
1. Registration driver
Class. forname ("com. mysql. jdbc. Driver"); // This is the Driver used to connect to the mysql database.
2. Get database connection
Java. SQL. Connection conn = java. SQL. DriverManager. getConnection (); 3. Get the expression
Java. SQL. Statement stmt = conn. createStatement ("jdbc: mysql: // localhost/test? UseUnicode = true & characterEncoding = GBK "," root "," null "); // the three parameters are the URL, user name, and password of the database connection. 4. Execute the SQL
Java. SQL. ResultSet rs1_stmt.exe cuteQuery ("select * from user"); 5. display the data in the result set
While (rs. next ()){
System. out. println (rs. getInt (1 ));
System. out. println (rs. getString ("username "));
System. out. println (rs. getString ("password "));
System. out. pringln ();
} // Execute the insert statement
// Stmt.exe cuteUpdate ("insert into user values (1, 'Chinese', '123 ')");
6. release resources
Rs. close ();
Stmt. close ();
Conn. close ();
How does one connect to a database using JDBC in java?
1. Registration driver
Class. forname ("com. mysql. jdbc. Driver"); // This is the Driver used to connect to the mysql database.
2. Get database connection
Java. SQL. Connection conn = java. SQL. DriverManager. getConnection (); 3. Get the expression
Java. SQL. Statement stmt = conn. createStatement ("jdbc: mysql: // localhost/test? UseUnicode = true & characterEncoding = GBK "," root "," null "); // the three parameters are the URL, user name, and password of the database connection. 4. Execute the SQL
Java. SQL. ResultSet rs1_stmt.exe cuteQuery ("select * from user"); 5. display the data in the result set
While (rs. next ()){
System. out. println (rs. getInt (1 ));
System. out. println (rs. getString ("username "));
System. out. println (rs. getString ("password "));
System. out. pringln ();
} // Execute the insert statement
// Stmt.exe cuteUpdate ("insert into user values (1, 'Chinese', '123 ')");
6. release resources
Rs. close ();
Stmt. close ();
Conn. close ();