【重點】Class.forName("com.MySQL.jdbc.Driver");//數據庫連接語句聲明
創新互聯致力于成都網站設計、網站建設、外貿網站建設,成都網站設計,集團網站建設等服務標準化,推過標準化降低中小企業的建站的成本,并持續提升建站的定制化服務水平進行質量交付,讓企業網站從市場競爭中脫穎而出。 選擇創新互聯,就選擇了安全、穩定、美觀的網站建設服務!
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/bbs","root","root"); //本地mysql數據庫JDBC連接語句(項目名bbs登錄名root密碼root)
conn = DriverManager.getConnection("jdbc:mysql://w.rdc.sae.sina.com.cn:3307/app_xiaoweibbs","0ww4ox1l0n","4ik4i5jkwwmxij0lyjl3hmlx3kkyyi1ii10iih33");
//新浪云mysql數據庫JDBC連接語句(云數據庫地址和端口固定“w.rdc.sae.sina.com.cn:3307”web應用名稱前面要加上app_ 然后找到web應用設置類似0ww4ox1l0n的數據庫登錄名和類似4ik4i5jkwwmxij0lyjl3hmlx3kkyyi1ii10iih33這樣的數據庫登錄密碼)
【一】下面是我自己的bbs數據庫的封裝代碼:
package com.bjsxt.bbs; //包名要記得改掉
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;
}
}
文章題目:新浪云web項目數據庫連接及封裝
分享網址:http://m.newbst.com/article8/jeidip.html
成都網站建設公司_創新互聯,為您提供手機網站建設、移動網站建設、App設計、標簽優化、Google、網站制作
聲明:本網站發布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網站立場,如需處理請聯系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經允許不得轉載,或轉載時需注明來源: 創新互聯