十年网站开发经验 + 多家企业客户 + 靠谱的建站团队
量身定制 + 运营维护+专业推广+无忧售后,网站问题一站解决
【重点】Class.forName("com.mysql.jdbc.Driver");//数据库连接语句声明
创新互联公司自2013年创立以来,是专业互联网技术服务公司,拥有项目网站设计、网站制作网站策划,项目实施与项目整合能力。我们以让每一个梦想脱颖而出为使命,1280元皮山做网站,已为上家服务,为皮山各地企业和个人服务,联系电话:028-86922220conn = 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;
}
}
另外有需要云服务器可以了解下创新互联cdcxhl.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。