Database connection and Encapsulation of Sina Cloud web Project
[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
}
}