package dao;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import javax.servlet.ServletContext;
public class CommDAO
{
Connection conn = null;
public CommDAO()
{
conn = this.getConn();
}
private static Properties config = null;
static {
try {
config = new Properties();
// InputStream in = config.getClass().getResourceAsStream("dbconnection.properties");
InputStream in = CommDAO.class.getClassLoader().getResourceAsStream("dbconnection.properties");
config.load(in);
in.close();
} catch (Exception e) {
e.printStackTrace();
}
}
public Connection getConn()
{
try
{
String d = new Date().toLocaleString().substring(0,9);
Class.forName("com.mysql.jdbc.Driver");
String dburl = (String)config.get("dburl");
conn = DriverManager.getConnection(dburl);
}
catch(Exception e)
{
e.printStackTrace();
}
return conn;
}
public int getInt(String sql)
{
int i = 0;
try {
Statement st = conn.createStatement();
ResultSet rs = st.executeQuery(sql);
if(rs.next())
{
i = rs.getInt(1);
}
st.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return i;
}
public double getDouble(String sql)
{
double i = 0;
try {
Statement st = conn.createStatement();
ResultSet rs = st.executeQuery(sql);
if(rs.next())
{
i = rs.getDouble(1);
}
st.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return i;
}
public void commOper(String sql)
{
try {
Statement st = conn.createStatement();
st.execute(sql);
st.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void commOperSqls(ArrayList<String> sql)
{
try {
conn.setAutoCommit(false);
for(int i=0;i<sql.size();i++)
{
Statement st = conn.createStatement();
st.execute(sql.get(i));
st.close();
}
conn.commit();
} catch (SQLException e) {
try {
conn.rollback();
} catch (SQLException e1) {
e1.printStackTrace();
}
e.printStackTrace();
}finally{
try {
conn.setAutoCommit(true);
} catch (SQLException e) {
e.printStackTrace();
}
}
}
public List<HashMap> select(String sql)
{
List<HashMap> list = new ArrayList();
try {
Statement st = conn.createStatement();
ResultSet rs = st.executeQuery(sql);
ResultSetMetaData rsmd = rs.getMetaData();
while(rs.next())
{
HashMap map = new HashMap();
int i = rsmd.getColumnCount();
for(int j=1;j<=i;j++)
{
if(!rsmd.getColumnName(j).equals("ID"))
{
map.put(rsmd.getColumnName(j), rs.getString(j)==null?"":rs.getString(j));
}
else
{
map.put("id", rs.getString(j));
}
}
list.add(map);
}
rs.close();
st.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
System.out.println("--"+sql);
e.printStackTrace();
}
return list;
}
public void close()
{
try {
conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* 执行一条查询sql,以 List<hashmap> 的形式返回查询的记录,记录条数,和从第几条开始,由参数决定,主要用于翻页
* pageno 页码 rowsize 每页的条数
*/
public List select(String sql, int pageno, int rowsize) {
List<HashMap> list=new ArrayList<HashMap>();
List<HashMap> mlist=new ArrayList<HashMap>();
try{
list=this.select(sql);
int min = (pageno-1)*rowsize;
int max = pageno*rowsize;
for(int i=0;i<list.size();i++)
{
if(!(i<min||i>(max-1)))
{
mlist.add(list.get(i));
}
}
}catch(RuntimeException re){
re.printStackTrace();
throw re;
}
return mlist;
}
public static void main(String[] args) {
System.out.println(new CommDAO());
}
}
java.sql.SQLException: The url cannot be null
at java.sql.DriverManager.getConnection(DriverManager.java:649)
at java.sql.DriverManager.getConnection(DriverManager.java:270)
at dao.CommDAO.getConn(CommDAO.java:66)
at dao.CommDAO.<init>(CommDAO.java:31)
at org.apache.jsp.welcome_jsp._jspService(welcome_jsp.java:124)
at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:731)
at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:439)
at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:395)
at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:339)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:731)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at util.SetChar.doFilter(SetChar.java:26)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:218)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:122)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:505)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:169)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:958)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:452)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1087)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:637)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:316)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Thread.java:745)
六月 12, 2017 2:06:37 下午 org.apache.catalina.core.ApplicationContext log
信息: java.lang.NullPointerException
botao555
初梦_
产品经理不是经理