雅文科技网上书店备忘

代码志 ,2009-11-04 01:1 ,ming

软件工程的大作业是一个网上书店项目,网上书店刚好又可以作为电子商务的作业,如果用JSP+SQLSERVER实现的话,又成了网络数据库编程的作业哦,呵呵,组员一致决定“3 in 1”。无独有偶,我们组员对JSP都不熟悉,仅有一人有过JSP项目经验,但是为了完成这次作业,自己变找了本JAVA的书,花了一个星期来看,然后就开始Coding啦。

虽然以前没有选修JAVA,但学习起来并没有觉得有多大的困难,除了myEclipse几个版本跟Windows7的兼容性问题之外,让人头痛的问题暂时不多,不过有些东西还有记录一下,以防忘记。

雅文网上购书系统

1.JavaBean,Servlet,Script乱码
在写JavaBean时传值过程中常常遇到中文乱码的问题,需要设置一下编码。

request.setCharacterEncoding("UTF-8");

response.setCharacterEncoding("UTF-8");

然后对字符串重新编码:

字符串=new String(字符串.getBytes("ISO-8859-1"),"UTF-8");

2.时间转化为字符串,插入时间时,可以将时间传为String 输入。

String dateStr=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()); 

3.图片按钮

 

4. ResultSet.Next()相当于一个指针,读取一行中某个属性值之后,该属性值之前的值变无法getString得到,如果由于排版而搞乱顺序时,最好先用变量存储起来。

5. 数据库批处理Bean

import java.sql.*;
public class QueryBean
{
public String query_statement;/*定义SQL语句*/
public String param[];
public ResultSet result=null;
public Connection conn;
//设置查询参数
public void setParam(String[] param)
{
	this.param=param;
    }
//设置SQL查询语句
public void setQuerystatement(String query_statement)
{
	this.query_statement=query_statement;

}
//设置连接参数
public void setConnection(String driverName,String jdbcURL,String username,String passwd) throws Exception
{
	Connection conn1;
	Class.forName(driverName);
	conn1=DriverManager.getConnection(jdbcURL,username,passwd);
	conn1.setAutoCommit(false);
	this.conn=conn1;
}
/*获取查询结果*/
public ResultSet getResult()
{
	try
	{
	  PreparedStatement select_stm=conn.prepareStatement(query_statement,java.sql.ResultSet.TYPE_SCROLL_INSENSITIVE,java.sql.ResultSet.CONCUR_READ_ONLY);
		if(param!=null)
			for(int i=0;i

6. 简单的数据操作Bean(JDBC方式)

import java.sql.*;

public class SqlQuery {
	private String dblink;
	private String mySQL;
 	private Connection connect;
        private Statement st;
	private ResultSet rset;
	private String Results;//返回值

	public void setDblink(String dblink) {
		this.dblink = dblink;//String dblink="jdbc:odbc:yawentech";
	}

	public void setMySQL(String mySQL) {
		this.mySQL = mySQL;
	}
	public String getResults() {
		String temp="";
		try{
			//加载JDBC-ODBC驱动程序
			Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
	     }catch(ClassNotFoundException ce){
					System.out.println(ce);
					}
	     try{
	 		//连接数据库
	 	    connect=DriverManager.getConnection(dblink);
	 		//生成statement
	 		st=connect.createStatement();
	 	    //执行SQL
	 	    rset=st.executeQuery(mySQL);
		    while(rset.next()){
			String a=rset.getString("属性名");;
			Results=a;
				}
			}catch(SQLException se){
				//System.out.println(se);
				return "Null_1";
			}finally{
				try{
					if(rset!=null) rset.close();
					if(st!=null) st.close();
					if(connect!=null) connect.close();
				}catch(SQLException se){return "Null_2";}//System.out.println(se);}
		   }
			return Results;
	}
}

7. Servlet弹出窗口,并跳转到其他页面,若要后退的话:history.back();

response.setCharacterEncoding("UTF-8");
PrintWriter out = response.getWriter();
out.print(" < script language=' javascript' > alert('修改成功!');location.href='提示信息';< / script > ");

8. SQL Server 外键级联删除/更新问题,创建table时没有给外键设置级联关系,就得用SQL Server Management Studio Express设置了。否则当外键有值时,删除/更新就会出错。这样子挺麻烦的,教训我们数据库设计一定要完善。

CatId int NOT NULL FOREIGN KEY REFERENCES ProductCategory_table(CatId ) on delete cascade;/*级联删除*/
CatId int NOT NULL FOREIGN KEY REFERENCES ProductCategory_table(CatId ) on delete cascade on update cascade;/*级联删除、更新*/

设置外键级联删除

设置外键级联删除关系

已经有4个人留下评论。

  1. Mulch 说:

    Hey mate, thanks 4 sharing but this page isn’t vewable in Internet Explorer it is doubled up.

  2. Hey mate, greetings from Hawii !

  3. Ming 说:

    hi,thank you! I am using a plugin named wp-syntax to make my codes look nice,actually it doesn’t function well.And I’m going to try other plugins after Spring Festival.I am too busy to work on this and reply you last month.Thanks again.

  4. Lee Pyotr 说:

    Great article. There’s a lot of good data here, though I did want to let you know something – I am running Redhat with the current beta of Firefox, and the layout of your blog is kind of bizarre for me. I can read the articles, but the navigation doesn’t function so well.

踩博就要像铲除敌人一样毫不留情!