Get the App
SLTechnology News&Howtos  ›  Database  › 

Database connection Learning-A simple address Book

Shulou Source: shulou.com Published: 2022-06-01 15:59:41 10月29日 Update

In order to do the graduation project, I studied Java, and then I had to connect to the database. In order to connect to the database, I did a simple project, address book (now only added functions), and successfully connected to the database.

First of all, take a look at my WEB home page:

It's relatively simple, then fill in the contact page.

The code of my database connection is thrown out first, after all, this is the focus of my address book study.

Package s2.jsp.zhangxiao.daoxiao import java.sql.PreparedStatement;import java.sql.Connection;import java.sql.ResultSet;import java.sql.DriverManager;import java.sql.SQLException;public class AddressBase {/ / define the user name of the database private final String DBNAME = "root"; / / define the password of the database private final String DBPASS = "0" / / define the driver information of the database private final String DRIVER = "com.mysql.jdbc.Driver"; / / define the address to access the database private final String URL = "jdbc:mysql://localhost:3306/addressdb"; public Connection getConnection () {Connection con=null; try {Class.forName (DRIVER); con=DriverManager.getConnection (URL, DBNAME, DBPASS) } catch (ClassNotFoundException e) {System.out.println ("drive exception"); e.printStackTrace ();} catch (SQLException e) {System.out.println ("database exception"); e.printStackTrace ();} return con } public void closeAll (Connection con,PreparedStatement past,ResultSet rs) {try {rs.close ();} catch (SQLException e) {/ / TODO Auto-generated catch block e.printStackTrace ();} try {past.close () } catch (SQLException e) {/ / TODO Auto-generated catch block e.printStackTrace ();} try {con.close ();} catch (SQLException e) {/ / TODO Auto-generated catch block e.printStackTrace () }} public int update (String sql,String [] getValues) {int iTuno; Connection con=null; PreparedStatement past=null; ResultSet rs=null; con=getConnection (); try {past=con.prepareStatement (sql); if (getValuesinvalid null) {for (int j = 0; j < getValues.length) I=past.executeUpdate +) {past.setString (jacks 1, getValues [j]);}} i=past.executeUpdate ();} catch (SQLException e) {/ / TODO Auto-generated catch block e.printStackTrace ();} return I;}}

Tags: Data database learning communication address book driver success code information function only address password graduation project user name contact person this is key page Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Technology NVidia MariaDB Xiaomi Shulou Tech Info