📄 customeraddsondao.java
字号:
package com.mole.struts.dao;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.sql.DataSource;
import com.mole.struts.bean.CustomerBlogArticleTypeBean;
import com.mole.struts.bean.CustomerInfoBean;
import com.mole.struts.bean.CustomerSons;
public class CustomerAddSonDAO {
private Connection conn;
public CustomerAddSonDAO() {
}
public Connection getConn() {
if (conn == null) {
try {
Context ctx = new InitialContext();
if (ctx == null)
throw new Exception("Failed to initial context!");
DataSource ds = (DataSource) ctx
.lookup("java:comp/env/jdbc/crmdata");
conn = ds.getConnection();
} catch (Exception e) {
e.printStackTrace();
}
}
return conn;
}
public String getCustomerStyle(String ID) throws Exception {
ResultSet rs = null;
PreparedStatement ps = null;
CustomerInfoBean bean = new CustomerInfoBean();
String sql = "SELECT [BlogStyle] FROM [Customer] WHERE [ID]=?";
try {
conn.setAutoCommit(true);
ps = conn.prepareStatement(sql);
ps.setObject(1, ID);
rs = ps.executeQuery();
if (rs.next()) {
return rs.getString(1);
}
return null;
} finally {
if (ps != null)
ps.close();
}
}
public ArrayList commonQuery(String sql) {
Connection conn = getConn();
Statement ps = null;
ResultSet rs = null;
ArrayList list = new ArrayList();
try {
conn.setAutoCommit(true);
ps = conn.createStatement();
rs = ps.executeQuery(sql);
while (rs.next()) {
CustomerBlogArticleTypeBean temp = new CustomerBlogArticleTypeBean();
temp.setId(rs.getString(1));
temp.setCustomerId(rs.getString(2));
temp.setName(rs.getString(3));
list.add(temp);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
rs.close();
ps.close();
return list;
} catch (SQLException e) {
e.printStackTrace();
}
}
return list;
}
public ArrayList executeQuery(String sql) {
Connection conn = getConn();
Statement ps = null;
ResultSet rs = null;
ArrayList<CustomerSons> list = new ArrayList();
try {
conn.setAutoCommit(true);
ps = conn.createStatement();
rs = ps.executeQuery(sql);
while (rs.next())// the arguments are
// SonId,Content,IssueTime,TopicId,WriterName,ContentSize
{
CustomerSons tempSon = new CustomerSons();
tempSon.setSonId(String.valueOf(rs.getInt(1)));
tempSon.setContent(rs.getString(2));
tempSon.setIssueTime(rs.getDate(3).toString());
tempSon.setTopicId(rs.getString(4));
tempSon.setWriterName(rs.getString(5));
tempSon.setContentSize(rs.getString(6));
list.add(tempSon);
}
return list;
} catch (Exception e) {
System.out.println(e.toString());
} finally {
try {
conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return null;
}
public void executeUpdate(String sql) {
Connection conn = getConn();
PreparedStatement ps = null;
try {
conn.setAutoCommit(true);
ps = conn.prepareStatement(sql);
ps.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
} finally {
try {
ps.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
public void executeUpdate(String[] sql) {
Connection conn = getConn();
Statement ps = null;
try {
conn.setAutoCommit(false);
ps = conn.createStatement();
for (int i = 0; i < sql.length; i++) {
ps.executeUpdate(sql[i]);
}
conn.commit();
} catch (SQLException e) {
e.printStackTrace();
} finally {
try {
ps.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
/*
* public ArrayList<Object[]> executeQuery(String sql) { Connection
* conn=getConn(); PreparedStatement ps = null; ResultSet rs = null;
* ArrayList<Object[]> al = new ArrayList<Object[]>(); Object[] ret = null;
* int num = 0; try { conn.setAutoCommit(true); ps =
* conn.prepareStatement(sql); rs = ps.executeQuery(); num =
* rs.getMetaData().getColumnCount(); while (rs.next()) { ret = new
* Object[num]; for (int i = 0; i < num; i++) ret[i] = rs.getObject(i + 1);
* al.add(ret); } } catch (Exception e) { e.printStackTrace(); } finally {
* try { rs.close(); ps.close(); } catch (SQLException e) {
* e.printStackTrace(); } } return al; }
*/
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -