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 and Encapsulation of Sina Cloud web Project

2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

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

[key] Class.forName ("com.mysql.jdbc.Driver"); / / Database connection statement declaration

Conn = DriverManager.getConnection ("jdbc:mysql://localhost:3306/bbs", "root", "root"); / / Local mysql database JDBC connection statement (project name bbs login name root password root)

Conn = DriverManager.getConnection ("jdbc:mysql://w.rdc.sae.sina.com.cn:3307/app_xiaoweibbs", "0ww4ox1l0n", "4ik4i5jkwwmxij0lyjl3hmlx3kkyyi1ii10iih33")

/ / Sina Cloud mysql database JDBC connection statement (the cloud database address and port are fixed "w.rdc.sae.sina.com.cn:3307" web application name is preceded by app_, and then find the web application setting database login name similar to 0ww4ox1l0n and database login password like 4ik4i5jkwwmxij0lyjl3hmlx3kkyyi1ii10iih33)

[1] here is the encapsulation code of my own bbs database:

Remember to change the package com.bjsxt.bbs; / / package name

Import com.mysql.jdbc.Statement

Import java.sql.*

Public class DB {

Public static Connection getConn () {

Connection conn = null

Try {

Class.forName ("com.mysql.jdbc.Driver")

/ / conn = DriverManager.getConnection ("jdbc:mysql://localhost:3306/bbs", "root", "root")

Conn = DriverManager.getConnection ("jdbc:mysql://w.rdc.sae.sina.com.cn:3307/app_xiaoweibbs", "0ww4ox1l0n", "4ik4i5jkwwmxij0lyjl3hmlx3kkyyi1ii10iih33")

} catch (ClassNotFoundException e) {

E.printStackTrace ()

} catch (SQLException e) {

E.printStackTrace ()

E.printStackTrace ()

}

Return conn

}

Public static Statement createStmt (Connection conn) {

Statement stmt = null

Try {

Stmt = (Statement) conn.createStatement ()

} catch (SQLException e) {

E.printStackTrace ()

}

Return stmt

}

Public static ResultSet ExceruQuery (Statement stmt,String sql) {

ResultSet rs = null

Try {

Rs = stmt.executeQuery (sql)

} catch (SQLException e) {

E.printStackTrace ()

}

Return rs

}

Public static int executeUpdate (Connection conn,String sql) {

Int ret = 0

Statement stmt = null

Try {

Stmt = (Statement) conn.createStatement ()

Ret = stmt.executeUpdate (sql)

} catch (SQLException e) {

E.printStackTrace ()

} finally {

Close (stmt)

}

Return ret

}

Public static void close (Connection conn) {

If (conn! = null) {

Try {

Conn.close ()

} catch (SQLException e) {

/ / TODO Auto-generated catch block

E.printStackTrace ()

}

}

Conn = null

}

Public static void close (Statement stmt) {

If (stmt! = null) {

Try {

Stmt.close ()

} catch (SQLException e) {

/ / TODO Auto-generated catch block

E.printStackTrace ()

}

}

Stmt = null

}

Public static void close (ResultSet rs) {

If (rs! = null) {

Try {

Rs.close ()

} catch (SQLException e) {

/ / TODO Auto-generated catch block

E.printStackTrace ()

}

}

Rs = null

}

Public static PreparedStatement PreparedStmt (Connection conn,String sql) {

PreparedStatement pstmt = null

Try {

Pstmt = conn.prepareStatement (sql)

} catch (SQLException e) {

/ / TODO Auto-generated catch block

E.printStackTrace ()

}

Return pstmt

}

Public static PreparedStatement PreparedStmt (Connection conn,String sql,int autoGeneratedKeys) {

PreparedStatement pstmt = null

Try {

Pstmt = conn.prepareStatement (sql,autoGeneratedKeys)

} catch (SQLException e) {

/ / TODO Auto-generated catch block

E.printStackTrace ()

}

Return pstmt

}

Public static void close (PreparedStatement pstmt) {

If (pstmt! = null) {

Try {

Pstmt.close ()

} catch (SQLException e) {

/ / TODO Auto-generated catch block

E.printStackTrace ()

}

}

Pstmt = null

}

}

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