微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

新浪云web项目数据库连接及 封装

【重点】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;

    }

}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐