package com.prosay.util;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class DbUtil {
public static void main(String[] args) throws SQLException {
DbUtil db = new DbUtil();
int i = db.doUpdate("insert into tb_cinema (c_id,c_name,c_pingfen,c_entry_time,c_loc)values(?,?,?,?,?) ", 8, "李小龙传奇",200,"2018-11-11","上海");
System.out.println(i);
ResultSet rs=db.doQuery("select * from tb_cinema");
db.showResultSet(rs);
}
private String user = "root";
private String password = "123456";
private String url = "jdbc:mysql://localhost:3306/db_star?character=utf-8";
private String database = "db_star";
private Connection conn = null;
private PreparedStatement ps = null;
private ResultSet rs = null;
private String className = "com.mysql.jdbc.Driver";
private String charset = "utf-8";
private int port = 3306;
private String serverName="localhost";
public DbUtil() {
this(null, null, null, null);
this.updateUrl();
}
public DbUtil(String className) {
this(null, null, null, className);
this.updateUrl();
}
public DbUtil(String user, String password) {
this(null, null, user, password);
this.updateUrl();
}
public DbUtil(String url, String user, String password) {
this(null, url, user, password);
this.updateUrl();
}
public DbUtil(String className, String url, String user, String password) {
if (className != null) {
this.className = className;
}
if (url != null) {
this.url = url;
}
if (user != null) {
this.user = user;
}
if (password != null) {
this.password = password;
}
init();
}
public void updateUrl() {
this.url="jdbc:mysql://"+serverName+":"+port+"/"+database+"?characterEncoding="+charset;
}
public void init() {
try {
Class.forName(className);
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void setDatabase(String database) {
this.database = database;
}
public void setPort(int port) {
this.port = port;
}
public void setCharset(String charset) {
this.charset = charset;
}
public Connection getConnection() throws SQLException {
conn = DriverManager.getConnection(url, user, password);
return conn;
}
public PreparedStatement getpreparedstatement(String sql, Object[] params) throws SQLException {
ps = getConnection().prepareStatement(sql);
if (params != null && params.length > 0) {
for (int i = 0; i < params.length; i++) {
ps.setObject(i + 1, params[i]);
}
}
return ps;
}
public ResultSet doQuery(String sql, Object... params) throws SQLException {
ResultSet rs = getpreparedstatement(sql, params).executeQuery();
return rs;
}
public void close() throws SQLException {
if (conn != null) {
conn.close();
}
}
/**
* 更新语句
* @param sql
* @param params
* @return
* @throws SQLException
*/
public int doUpdate(String sql, Object... params) throws SQLException {
PreparedStatement ps = getpreparedstatement(sql, params);
return ps.executeUpdate();
}
/**
* 展示最终结果
* @param rs
* @throws SQLException
*/
public void showResultSet(ResultSet rs) throws SQLException {
if (rs != null) {
for (int i = 1; i <= rs.getMetaData().getColumnCount(); i++) {
System.out.print(rs.getMetaData().getColumnName(i));
if (i == rs.getMetaData().getColumnCount()) {
System.out.println();
} else {
System.out.print("\t");
}
}
}
rs.beforeFirst();
while (rs.next()) {
for (int i = 1; i <= rs.getMetaData().getColumnCount(); i++) {
System.out.print(rs.getObject(i));
if (i == rs.getMetaData().getColumnCount()) {
System.out.println();
} else {
System.out.print("\t");
}
}
}
}
}