package conn;
import java.sql.*;
import java.io.*;
public class MySqlDBConn ...{
private Connection conn = null;
private Statement stmt = null;
private PreparedStatement prepstmt = null;
// 驱动程序名
private String dbdriver="com.mysql.jdbc.Driver";
// 数据库用户名
private String userName="root";
// 密码
private String userPasswd="";
// 数据库名
private String dbName="report";
// 联结字符串
private String dburl="jdbc:mysql://localhost/"+dbName+"?user="+userName+"&password="+userPasswd;
/** *//**
* 构造数据库的连接和访问类
*/
public MySqlDBConn() throws Exception ...{
Class.forName(dbdriver).newInstance();
conn = DriverManager.getConnection(dburl);
stmt = conn.createStatement();
}
public MySqlDBConn(String sql) throws Exception ...{
Class.forName(dbdriver).newInstance();
conn = DriverManager.getConnection(dburl);
this.prepareStatement(sql);
}
/** *//**
* 返回连接
* @return Connection 连接
*/
public Connection getConnection() ...{
if(conn!=null)...{
return conn;
}
else return null;
}
/** *//**
* PreparedStatement
* @return sql 预设SQL语句
*/
public void prepareStatement(String sql) throws SQLException ...{
prepstmt = conn.prepareStatement(sql);
}
/** *//**
* 设置对应值
* @param index 参数索引
* @param value 对应值
*/
public void setString(int index,String value) throws SQLException ...{
prepstmt.setString(index,value);
}
public void setInt(int index,int value) throws SQLException ...{
prepstmt.setInt(index,value);
}
public void setBoolean(int index,boolean value) throws SQLException ...{
prepstmt.setBoolean(index,value);
}
public void setDate(int index,Date value) throws SQLException ...{
prepstmt.setDate(index,value);
}
public void setLong(int index,long value) throws SQLException ...{
prepstmt.setLong(index,value);
}
public void setFloat(int index,float value) throws SQLException ...{
prepstmt.setFloat(index,value);
}
// File file = new File("test/data.txt");
// int fileLength = file.length();
// InputStream fin = new java.io.FileInputStream(file);
// mysql.setBinaryStream(5,fin,fileLength);
public void setBinaryStream(int index,InputStream in,int length) throws SQLException ...{
prepstmt.setBinaryStream(index,in,length);
}
public void clearParameters()
throws SQLException
...{
prepstmt.clearParameters();
}
/** *//**
* 返回预设状态
*/
public PreparedStatement getPreparedStatement() ...{
return prepstmt;
}
/** *//**
* 返回状态
* @return Statement 状态
*/
public Statement getStatement() ...{
return stmt;
}
/** *//**
* 执行SQL语句返回字段集
* @param sql SQL语句
* @return ResultSet 字段集
*/
public ResultSet executeQuery(String sql) throws SQLException ...{
if (stmt != null) ...{
return stmt.executeQuery(sql);
}
else return null;
}
public ResultSet executeQuery() throws SQLException ...{
if (prepstmt != null) ...{
return prepstmt.executeQuery();
}
else return null;
}
/** *//**
* 执行SQL语句
* @param sql SQL语句
*/
public void executeUpdate(String sql) throws SQLException ...{
if (stmt != null)
stmt.executeUpdate(sql);
}
public void executeUpdate() throws SQLException ...{
if (prepstmt != null)
prepstmt.executeUpdate();
}
/** *//**
* 关闭连接
*/
public void close() throws Exception ...{
if (stmt != null) ...{
stmt.close();
stmt = null;
}
if (prepstmt != null) ...{
prepstmt.close();
prepstmt = null;
}
conn.close();
conn = null;
}
}
相关文章
- Python基础:面向对象基础 (一) 类及其属性和魔法方法
- Java多线程Callable和Future类详解
- 【转】MySQL USE NAMES 'UTF8'
- iOS开发之类似Android中Toast效果制作
- Fine-tuning Convolutional Neural Networks for Biomedical Image Analysis: Actively and Incrementally如何使用尽可能少的标注数据来训练一个效果有潜力的分类器
- 多线程进阶之并发工具类:CountDownLatch、CyclicBarrier
- Java:SpringBoot整合MyBatis-Plus实现MySQL数据库的增删改查
- Java 语言基础 (初识Java语言, 变量和数据类型, 运算符, 流程控制语句, 数组)
- JS判断手机当前的系统类型
- tableLayout 实现类似gridview的效果 带分割线