JAVA版SqlHelper
//JAVA版SqlHelper
package com.test.Dao; import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.ResultSetMetaData; import java.sql.Statement; import java.util.ArrayList; import java.util.Collections; import java.util.HashMap; import java.util.List; import java.util.Map; import javax.servlet.jsp.jstl.sql.Result; import javax.servlet.jsp.jstl.sql.ResultSupport; public class SqlHelper { public String query(String sql) { String result=""; try{ Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver"); Connection conn= DriverManager.getConnection("jdbc:sqlserver://116.255.154.99;DatabaseName=test;user=sa;password=Tcqbb1989"); String sql1 = "select * from article"; Statement stmt=conn.createStatement(); ResultSet rs = stmt.executeQuery(sql1); while(rs.next()){ result+=rs.getString(1)+"===="+rs.getString(2)+"</br>"; } }catch(Exception e){ result+=e.getMessage(); } return result; } public List query2(String sql) { ResultSet result = null; try{ Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver"); Connection conn= DriverManager.getConnection("jdbc:sqlserver://116.255.154.99;DatabaseName=test;user=sa;password=Tcqbb1989"); String sql1 = "select * from article"; Statement stmt=conn.createStatement(); ResultSet rs = stmt.executeQuery(sql1); return resultSetToList(rs); }catch(Exception e){ } return null; } public int excute(String sql) { String result=""; int rows1 =0; try{ Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver"); Connection conn= DriverManager.getConnection("jdbc:sqlserver://116.255.154.99;DatabaseName=test;user=sa;password=Tcqbb1989"); Statement stmt=conn.createStatement(); rows1 = stmt.executeUpdate(sql); }catch(Exception e){ result+=e.getMessage(); } return rows1; } public List resultSetToList(ResultSet rs) throws java.sql.SQLException { if (rs == null) return Collections.EMPTY_LIST; ResultSet md = (ResultSet) rs.getMetaData(); //得到结果集(rs)的结构信息,比如字段数、字段名等 int columnCount = ((ResultSetMetaData) md).getColumnCount(); //返回此 ResultSet 对象中的列数 List list = new ArrayList(); Map rowData = new HashMap(); while (rs.next()) { rowData = new HashMap(columnCount); for (int i = 1; i <= columnCount; i++) { rowData.put(((ResultSetMetaData) md).getColumnName(i), rs.getObject(i)); } list.add(rowData); System.out.println("list:" + list.toString()); } return list; } }