幾則JSP入門知識(shí)總結(jié)
2020-06-01 23:30:07
供稿:網(wǎng)友
從去年9月份,我就開始著手學(xué)JSP,以前也只有一點(diǎn)程序的意識(shí),一路上摸索過來,經(jīng)過了很多磨難,終于有一天,我就像一個(gè)旱鴨子學(xué)會(huì)游泳一樣,心里無比高興,熬了幾天夜,終于寫成了這個(gè)純JSP的文章發(fā)布程序。
相信下面的幾則小知識(shí)對(duì)向我這樣水平的菜鳥有一定的幫助!
==============================================================================
1.傳遞表單參數(shù):
String name = new String(request.getParameter("name"));
2.數(shù)據(jù)庫連接:
~~MYSQL
//設(shè)置數(shù)據(jù)庫的URL
String url = "jdbc:mysql://localhost:3306/jspsky";
try
//加載驅(qū)動(dòng)程序
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+"')";
//運(yùn)行SQL語句,并建立結(jié)果集
java.sql.ResultSet rsi = statement.executeQuery(sqlStringi);
//在屏幕上輸出庫中的內(nèi)容
while(rss.next())
{
String a_name = rss.getString(1);
out.println(a_name);
{}
//關(guān)閉連接
connection.close();
}
//捕捉異常
catch(java.sql.SQLException e)
out.println(e.getMessage());
{}
catch(ClassNotFoundException e)
out.println(e.getMessage());
{}
~~DB2
//定義數(shù)據(jù)庫的URL
String url = "jdbc:db2:portal";
try
//加載驅(qū)動(dòng)程序
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";
//執(zhí)行SQL語句
java.sql.ResultSet rs = statement.executeQuery(sqlString);
//在屏幕上顯示所連表中的內(nèi)容
while(rs.next())
{
String name = rs.getString(1);
out.println(name);
{}
//關(guān)閉連接
connection.close();
}
//捕捉異常
catch(java.sql.SQLException e)
out.println(e.getMessage());
{}
catch(ClassNotFoundException e)
out.println(e.getMessage());
{}
3.文件操作
~~將一個(gè)字符串寫到一個(gè)指定的文件中,如果該文件不存在,則新建一個(gè)文件,并完成寫入;如果存在,則用此字符串覆蓋原文件的所有內(nèi)容
import java.io.*;
String str = "print me 雪峰!";
//定義好打印的目標(biāo)文件名
//取得當(dāng)前主機(jī)存放WEB頁面的絕對(duì)路徑
String hostdir = System.getProperty("user.dir");
//取得當(dāng)前主機(jī)所采用的路徑分隔符
String fileBar = System.getProperty("file.separator");
//書寫完整的目標(biāo)文件存放路徑
String nameOfFile=hostdir+fileBar+"test.html";