重庆分公司,新征程启航
为企业提供网站建设、域名注册、服务器等服务
package common;import java.io.Serializable;
创新互联是一家专业提供乐至企业网站建设,专注与网站设计制作、网站设计、H5场景定制、小程序制作等业务。10年已为乐至众多企业、政府机构等服务。创新互联专业网站设计公司优惠进行中。
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
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;/**
* 数据库工具类
* pCopyright: Copyright (c) 2010/p
*
* pCompany: /p
*
* @version 1.0
*/
public class DbUtils implements Serializable {
public DbUtils() {
} /**
* 数据库连接
* @return Connection
*/
public static Connection getConn() {
Connection conn = null; try {
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver"); conn = DriverManager.getConnection(
"jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=addressList",
"sa", "");
System.out.println("----连接数据库成功----"); } catch (ClassNotFoundException e) {
System.out.println("----加载数据库驱动失败----");
e.printStackTrace();
} catch (SQLException e) {
System.out.println("----连接数据库失败----");
e.printStackTrace();
}
return conn;
} /**
* 查询操作
* @param strSql String
* @param values List
* @return List
*/
public static List queryObj(String strSql, List values) { Connection conn = null;
PreparedStatement ps = null;
ResultSet rs = null; Result result = null; try { conn = getConn();
ps = conn.prepareStatement(strSql); if (values != null) {
for (int i = 0; i values.size(); i++) {
ps.setObject((i + 1), values.get(i));
}
} rs = ps.executeQuery(); result = ResultSupport.toResult(rs); } catch (SQLException ex) {
System.out.println("----查询失败----");
ex.printStackTrace();
} finally {
try {
if (rs != null) {
rs.close();
}
if (ps != null) {
ps.close();
}
if (conn != null) {
conn.close();
}
} catch (SQLException ex) {
ex.printStackTrace();
}
} return toList(result); } /**
* 修改操作
* @param strSql String
* @param values List
* @return boolean
*/
public static boolean editObj(String strSql, List values) { boolean flag = false; Connection conn = null;
PreparedStatement ps = null; try { conn = getConn();
ps = conn.prepareStatement(strSql); if (values != null) {
for (int i = 0; i values.size(); i++) {
Object obj = values.get(i);
if(obj == null) {
obj = "";
}
ps.setObject((i + 1), obj);
}
} if (ps.executeUpdate() 0) {
flag = true;
} } catch (SQLException ex) {
System.out.println("----修改失败----");
ex.printStackTrace();
} finally {
try {
if (ps != null) {
ps.close();
}
if (conn != null) {
conn.close();
}
} catch (SQLException ex) {
ex.printStackTrace();
}
} return flag; }
/**
* Result 转换成 List
* @param result Result
* @return List
*/
@SuppressWarnings("unchecked")
private static List toList(Result result) {
List list = new ArrayList(); // 列名
String[] column = result.getColumnNames(); for (int i = 0; i result.getRowCount(); i++) { Map map = new HashMap(); for (int j = 0; j column.length; j++) {
map.put(column[j], result.getRows()[i].get(column[j]));
} list.add(map);
} return list;
}
}
1.增加\x0d\x0aString s1="insert into tableNames (id,name,password) values(myseq.nextval,?,?);"\x0d\x0aClass.forName(driver);\x0d\x0aConnection conn = DriverManager.getConnection(url,dbUser,dbPwd);\x0d\x0aPreparedStatement prepStmt = conn.prepareStatement(s1);\x0d\x0aprepStmt.setString(1,name);\x0d\x0aprepStmt.setString(2,password);\x0d\x0aResultSet rs=stmt.executeUpdate();\x0d\x0a2、删除\x0d\x0aString s2="delete from tbNames where name=?";\x0d\x0aClass.forName(driver);\x0d\x0aConnection conn = DriverManager.getConnection(url,dbUser,dbPwd);\x0d\x0aPreparedStatement prepStmt = conn.prepareStatement(s2);\x0d\x0aprepStmt.setString(1,name);\x0d\x0aResultSet rs=stmt.executeUpdate();\x0d\x0a3、修改\x0d\x0aString s3=“update tbNames set name=? where id=?”;\x0d\x0aClass.forName(driver);\x0d\x0aConnection conn = DriverManager.getConnection(url,dbUser,dbPwd);\x0d\x0aPreparedStatement prepStmt = conn.prepareStatement(s3);\x0d\x0aprepStmt.setString(1,name);\x0d\x0aprepStmt.setString(2,id);\x0d\x0aResultSet rs=stmt.executeUpdate();\x0d\x0a4、查询\x0d\x0aString s4="select id,name,password from tbNames";\x0d\x0aClass.forName(driver);\x0d\x0aConnection conn = DriverManager.getConnection(url,dbUser,dbPwd);\x0d\x0aStatement stmt=conn.createStatement();\x0d\x0aResultSet rs = stmt.executeQuery(s4);\x0d\x0awhile(rs.next){\x0d\x0aint id=rs.getInt(1);\x0d\x0aString name = rs.getString(2);\x0d\x0aString pwd=rs.getString(3);\x0d\x0aSystem.out.println(id+name+pwd); } \x0d\x0a\x0d\x0a以上四步必须都得关闭连接;!!!\x0d\x0ars.close();\x0d\x0astmt.close();\x0d\x0aconn.close();
File fs =new File("filename","absolute_path");//实现添加功能
fs.delete();//删除效果
修改就是写入吧?一般用BufferedWriter类,可以接收任意参数.
如果只输入字符,可以用FileWriter类.最快的是RandomAccessFile,还可以具体定位输入呢,一般用来简单加密.
这几个类具体的实现你可以去查,专门用来文件输出的( File Out )
结合的框架可以完成用ssh,ssi,spring MVC 等等框架都可以完成,最简单的写个basedao然后用servelt完成