servlet学习笔记之七(连接mysql)
2010-01-23 16:29
155 查看
set names gbk 解决mysql乱码
数据库中选UTF8防止乱码
注意考贝jdbc的驱动到webappliction的lib目录下
代码
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/bbs?user=root&password=root");
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
return conn;
}
public static Statement getStatement(Connection conn) {
Statement stmt = null;
try {
if(conn != null) {
stmt = conn.createStatement();
}
} catch (SQLException e) {
e.printStackTrace();
}
return stmt;
}
public static ResultSet getResultSet(Statement stmt, String sql) {
ResultSet rs = null;
try {
if(stmt != null) {
rs = stmt.executeQuery(sql);
}
} catch (SQLException e) {
e.printStackTrace();
}
return rs;
}
public static void closeConn(Connection conn) {
try {
if(conn != null) {
conn.close();
conn = null;
}
} catch (SQLException e) {
e.printStackTrace();
}
}
public static void closeStmt(Statement stmt) {
try {
if(stmt != null) {
stmt.close();
stmt = null;
}
} catch (SQLException e) {
e.printStackTrace();
}
}
public static void closeRs(ResultSet rs) {
try {
if(rs != null) {
rs.close();
rs = null;
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
数据库中选UTF8防止乱码
注意考贝jdbc的驱动到webappliction的lib目录下
代码
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/bbs?user=root&password=root");
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
return conn;
}
public static Statement getStatement(Connection conn) {
Statement stmt = null;
try {
if(conn != null) {
stmt = conn.createStatement();
}
} catch (SQLException e) {
e.printStackTrace();
}
return stmt;
}
public static ResultSet getResultSet(Statement stmt, String sql) {
ResultSet rs = null;
try {
if(stmt != null) {
rs = stmt.executeQuery(sql);
}
} catch (SQLException e) {
e.printStackTrace();
}
return rs;
}
public static void closeConn(Connection conn) {
try {
if(conn != null) {
conn.close();
conn = null;
}
} catch (SQLException e) {
e.printStackTrace();
}
}
public static void closeStmt(Statement stmt) {
try {
if(stmt != null) {
stmt.close();
stmt = null;
}
} catch (SQLException e) {
e.printStackTrace();
}
}
public static void closeRs(ResultSet rs) {
try {
if(rs != null) {
rs.close();
rs = null;
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
相关文章推荐
- 64位CentOS6.5下Eclipse用Java连接mysql
- 关于流量升高导致TIME_WAIT增加,MySQL连接大量失败的问题
- [记录]JPA/Hibernate的MySQL长连接问题
- 关于mysql 远程连接不上的问题
- 完美解决thinkphp3的mysql连接问题
- PHP使用数据库永久连接方式(mysql_pconnect)操作MySQL的是与非
- PHP数据库连接mysql与mysqli的区别与用法
- java连接mysql
- MySQL的自然联结+外部联结(左外连接,右外连接)+内部联结
- C语言连接Mysql实例(一)
- Mysql 连接远程Linux主机无法连接 10061问题
- PHP与MySQL连接及SQL语言基本语法
- JDBC连接Mysql , sql server ,oracle 的方法
- MySql远程访问无法连接的问题!
- mysql企业备份软件mysqlbackup启动连接失败
- java学习篇:连接mySQL(一)
- SSIS 连接mysql问题
- Navicat for mysql 远程连接 mySql数据库10061错误问题
- [解决方法]Eclipse连接mysql失败:Error1682:Native table 'performance_schma'.'session_variables'
- mysql delete 多表连接删除功能