jdbc呼叫遠端mysql 資料庫
public static ResultSet getResultSet(String sql,List<String> params) {
try {
Class.forName("com.mysql.jdbc.Driver");
} catch (ClassNotFoundException e) {
throw new BizException("載入驅動出錯");
}
// String url = "jdbc:mysql://192.168.8.200:3306/chh_v2?useUnicode=true&characterEncoding=UTF-8&useSSL=false";
// String user = "chh_adm";
// String pwd = "chh2017";
String url =CallCenterConfig.getUrl();
String user = CallCenterConfig.getUsername();
String pwd = CallCenterConfig.getPassword();
Connection conn;
try {
conn = DriverManager.getConnection(url, user, pwd);
} catch (SQLException e) {
throw new BizException("連線資料庫出錯");
}
PreparedStatement ps;
try {
ps = conn.prepareStatement(sql);
// ps.setString(0, params.get(0));
} catch (SQLException e) {
throw new BizException("建立處理物件出錯");
}
ResultSet rs = null;
try {
rs = ps.executeQuery();
} catch (SQLException e) {
e.printStackTrace();
}
return rs;
}
//資料列表 使用PreparedStatement,建立PreparedStatement物件,但是這種相對安全,必須通過正確的賬號密碼操作。
@GetMapping("/center/list")
public ResponseEntity<RestResult<Object>> query() {
List<Map<String, Object>> list = new ArrayList<Map<String,Object>>();
ResultSet rs = getResultSet("SELECT " +
" Ano, " +
" BegTime, " +
" EndTime, " +
" IsCallOk, " +
" '主話單' AS type, " +
" IsRouteAgent, " +
" RoutedDN " +
"FROM " +
" t_sheetrecord " +
"LIMIT 10",null);
ResultSetMapper<CallRecordInfo> resultSetMapper = new ResultSetMapper<CallRecordInfo>();
List<CallRecordInfo> pojoList = resultSetMapper.mapRersultSetToObject(rs, CallRecordInfo.class);
try {
ResultSetMetaData md = rs.getMetaData(); //獲得結果集結構資訊,元資料
int columnCount = md.getColumnCount(); //獲得列數
while (rs.next()) {
Map<String,Object> rowData = new HashMap<String,Object>();
for (int i = 1; i <= columnCount; i++) {
rowData.put(md.getColumnName(i), rs.getObject(i));
list.add(rowData);
}
}
}catch (SQLException e) {
throw new BizException("獲取資料失敗");
}
System.out.println(list.size());
Pageable page = getPage();
return success(PageUtil.listToPage(null, page.getPageNumber(), page.getPageSize()));
}
·····················
package com.chehaha.callcenter;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import javax.persistence.Column;
import javax.persistence.Entity;
import org.apache.commons.beanutils.BeanUtils;
public class ResultSetMapper<T> {
@SuppressWarnings("unchecked")
public List<T> mapRersultSetToObject(ResultSet rs, Class outputClass) {
List<T> outputList = null;
try {
// make sure resultset is not null
if (rs != null) {
// check if outputClass has 'Entity' annotation
if (outputClass.isAnnotationPresent(Entity.class)) {
// get the resultset metadata
ResultSetMetaData rsmd = rs.getMetaData();
// get all the attributes of outputClass
Field[] fields = outputClass.getDeclaredFields();
while (rs.next()) {
T bean = (T) outputClass.newInstance();
for (int _iterator = 0; _iterator < rsmd.getColumnCount(); _iterator++) {
// getting the SQL column name
String columnName = rsmd.getColumnName(_iterator + 1);
// reading the value of the SQL column
Object columnValue = rs.getObject(_iterator + 1);
// iterating over outputClass attributes to check if
// any attribute has 'Column' annotation with
// matching 'name' value
for (Field field : fields) {
if (field.isAnnotationPresent(Column.class)) {
Column column = field.getAnnotation(Column.class);
if (column.name().equalsIgnoreCase(columnName) && columnValue != null) {
BeanUtils.setProperty(bean, field.getName(), columnValue);
break;
}
}
}
}
if (outputList == null) {
outputList = new ArrayList<T>();
}
outputList.add(bean);
}
} else {
// throw some error
}
} else {
return null;
}
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
} catch (InstantiationException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
return outputList;
}
}
··································
package com.chehaha.callcenter.entity;
import java.io.Serializable;
import java.util.Date;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
@Entity
public class CallRecordInfo {
@Id
@GeneratedValue(strategy= GenerationType.AUTO)
private Integer id;
@Column(name="Ano")
private String number;
@Column(name="BegTime")
private Date begTime;
@Column(name="EndTime")
private Date endTime;
@Column(name="IsCallOk")
private Boolean isCallOk;
@Column(name="type")
private String type;
@Column(name="IsRouteAgent")
private Boolean isRouteAgent;
@Column(name="RoutedDN")
private String routedDN;
public String getNumber() {
return number;
}
public void setNumber(String number) {
this.number = number;
}
public Date getBegTime() {
return begTime;
}
public void setBegTime(Date begTime) {
this.begTime = begTime;
}
public Date getEndTime() {
return endTime;
}
public void setEndTime(Date endTime) {
this.endTime = endTime;
}
public Boolean getIsCallOk() {
return isCallOk;
}
public void setIsCallOk(Boolean isCallOk) {
this.isCallOk = isCallOk;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public Boolean getIsRouteAgent() {
return isRouteAgent;
}
public void setIsRouteAgent(Boolean isRouteAgent) {
this.isRouteAgent = isRouteAgent;
}
public String getRoutedDN() {
return routedDN;
}
public void setRoutedDN(String routedDN) {
this.routedDN = routedDN;
}
}