Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Database connection Learning-A simple address Book

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)06/01 Report--

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;}}

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report