学习实例.文章管理.目标与经验总结

源文件下载 Web.rar

目标:实现文章添加,删除,修改功能。

经验总结:

1、接收POST参数,使用HTTPServlet,在接收参数前,使用 request.setCharacterEncoding("GBK"); 指明文字编码,避免中文乱码。

2、类中的init不会在类初始化时执行,应添加如下类构造代码,将数据库初始化放在此程序中。

public Article() {
super();
Conn = new DBUtil();
}

3、据实践证明,微软发布的JDBC支持包无法使用,故选用jtds连接数据库

Class.forName("net.sourceforge.jtds.jdbc.Driver").newInstance(); 
Connstr = "jdbc:jtds:sqlserver://localhost/Web";
Conn = DriverManager.getConnection(Connstr,"java_user","java_user");
Conn.setAutoCommit(true);
stmt = Conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_UPDATABLE);

4、选择查询是返回一个结果集,更新,删除查询则返回一个被影响的行数。为此,我将Execute写成了两参数不同的函数。这种处理方法称为重载

//查询返回结果集
public ResultSet Execute(String sql) throws Exception{
ResultSet rsC = null;
try{
rsC = stmt.executeQuery(sql);
}catch(Exception e){
System.out.println("DBUtil->Execute:" + e.getMessage());
}
return rsC;
}
//执行SQL语句返回影响行数
public int Execute(String sql,int iR) throws Exception{
iR = stmt.executeUpdate(sql);
return iR;
}

5、在任何时候,要想从ResultSet中取得一行记录,都必须使用while(rs.next()),否则就会像我一样,为寻找这条记录花上一个下午的时间。

6、不得不承认,这样显示出来的时间更亲切一些

Date date = new Date();
String s = String.format("%tF",date) + " " + String.format("%tT",date);

out.println(s);

7、验证一个字符串是否纯数字,如今有了更好的办法。

if(!ID.matches("[0-9]+")){
out.print("参数错误!");
}
jtds"Network error IOException Connection refused: connect"问题解决 ,需要打SP4补丁

Why do I get java.sql.SQLException: "Network error IOException: Connection refused: connect" when trying to get a connection? 

The "Connection refused" exception is thrown by jTDS when it is unable to connect to the server. There may be a number of reasons why this could happen: 

The server name is misspelled or the port number is incorrect.
SQL Server is not configured to use TCP/IP. Either enable TCP/IP from SQL Server's Network Utility app or have jTDS connect via named pipes (see the URL format for information on how to do this).
There is a firewall blocking port 1433 on the server. 
To check whether TCP/IP is enabled and the port is not blocked you can use "telnet <server_host> 1433". Until telnet doesn't connect, jTDS won't either. If you can't figure out why, ask your network administrator for help.
原文地址:https://www.cnblogs.com/blackice/p/2618263.html