几则几则JSP入门知识总结入门知识总结
从去年9月份,我就开始着手学JSP,以前也只有一点程序的意识,一路上摸索过来,经过了很多磨难,终于有一天,我就
像一个旱鸭子学会游泳一样,心里无比高兴,熬了几天夜,终于写成了这个纯JSP的文章发布程序。
相信下面的几则小知识对向我这样水平的菜鸟有一定的帮助!
==============================================================================
1.传递表单参数:
String name = new String(request.getParameter(“name”));
2.数据库连接:
~~MYSQL
//设置数据库的URL
String url = “jdbc:mysql://localhost:3306/jspsky”;
try
//加载驱动程序
Class.forname(“org.gjt.mm.mysql.Driver”).newInstance();
//建立连接
java.sql.Connection connection = java.sql.DriverManager.getConnection(url);
java.sql.Statement statement = connection.createStatement();
//SQL语句
String sqlStringi =”insert into
commu(name,tel,mobile,oicq,email)values(‘”+name+”‘,‘”+tel+”‘,‘”+mobile+”‘,‘”+oicq+”‘,‘”+email+”‘)”;
//运行SQL语句,并建立结果集
java.sql.ResultSet rsi = statement.executeQuery(sqlStringi);
//在屏幕上输出库中的内容
while(rss.next())
{
String a_name = rss.getString(1);
out.println(a_name);
{}
//关闭连接
connection.close();
}
//捕捉异常
catch(java.sql.SQLException e)
out.println(e.getMessage());
{}
catch(ClassNotFoundException e)
out.println(e.getMessage());
{}
~~DB2
//定义数据库的URL
String url = “jdbc:db2:portal”;
try
//加载驱动程序
Class.forName(“COM.ibm.db2.jdbc.app.DB2Driver”);
//建立连接,
java.sql.Connection connection = java.sql.DriverManager.getConnection(url,”user”,”password”);
java.sql.Statement statement = connection.createStatement();
//SQL语句
String sqlString = “select * from client”;
//执行SQL语句
java.sql.ResultSet rs = statement.executeQuery(sqlString);
//在屏幕上显示所连表中的内容
while(rs.next())
{
String name = rs.getString(1);
out.println(name);
{}
//关闭连接
connection.close();
}
//捕捉异常
catch(java.sql.SQLException e)
out.println(e.getMessage());
{}
catch(ClassNotFoundException e)
评论0