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

How to connect java to mysql underlying encapsulation

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

Share

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

This article mainly introduces how to connect java to the underlying package of mysql, which is very detailed and has certain reference value. Interested friends must finish reading it!

The details are as follows

Connect to the database

Package com.dao.db;import java.sql.Connection;import java.sql.SQLException;/** * database connection layer MYSQL * @ author Administrator * * / public class DBConnection {/ * connect to the database * @ return * / public static Connection getDBConnection () {/ / 1. Registered driver try {Class.forName ("com.mysql.jdbc.Driver");} catch (ClassNotFoundException e) {/ / TODO Auto-generated catch block e.printStackTrace ();} / / get the connection to the database try {Connection conn = java.sql.DriverManager.getConnection ("jdbc:mysql://localhost/mysql?useUnicode=true&characterEncoding=utf-8", "root", "root"); return conn;} catch (SQLException E1) {e1.printStackTrace ();} return null;}}

Data layer encapsulation

Package com.dao.db;import java.sql.Connection;import java.sql.PreparedStatement;import java.sql.ResultSet;import java.sql.ResultSetMetaData;import java.sql.SQLException;import java.sql.Types;import java.util.ArrayList;import java.util.HashMap;/** * MYSQL database underlying encapsulation * @ author Administrator * * / public class DBManager {private PreparedStatement pstmt; private Connection conn; private ResultSet rs; / * Open database * / public DBManager () {conn = DBConnection.getDBConnection () } / * * perform modify and add operation * @ param coulmn * @ param type * @ return * @ throws SQLException * / public boolean updateOrAdd (String [] coulmn, int [] type, String sql) throws SQLException {if (! setPstmtParam (coulmn, type, sql) return false; boolean flag = pstmt.executeUpdate () > zero true false; closeDB (); return flag } / * get query result set * @ param coulmn * @ param type * @ param sql * @ throws SQLException * / public DataTable getResultData (String [] coulmn, int [] type, String sql) throws SQLException {DataTable dt = new DataTable (); ArrayListlist = new ArrayList (); if (! setPstmtParam (coulmn, type, sql) return null; rs = pstmt.executeQuery (); ResultSetMetaData rsmd = rs.getMetaData (); / / take the column name of the database int numberOfColumns = rsmd.getColumnCount () While (rs.next ()) {HashMap rsTree = new HashMap (); for (int ritual 1)

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

Development

Wechat

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

12
Report