最大的網站源碼資源下載站,
從去年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)
out.println(e.getmessage());
{}
3.文件操作
~~將一個字符串寫到一個指定的文件中,如果該文件不存在,則新建一個文件,并完成寫入;如果存在,則用此字符串覆蓋原文件的所有內容
import java.io.*;
string str = "print me 雪峰!";
//定義好打印的目標文件名
//取得當前主機存放web頁面的絕對路徑
string hostdir = system.getproperty("user.dir");
//取得當前主機所采用的路徑分隔符
string filebar = system.getproperty("file.separator");
//書寫完整的目標文件存放路徑
string nameoffile=hostdir+filebar+"test.html";
try
//實例化一個文件輸出流對象
fileoutputstream afile = new fileoutputstream(nameoffile);
//將文件輸出流,創建一個打印輸出流對象
printwriter pw = new printwriter(afile);
pw.println(str);
//clean up
pw.close();
{}
catch(ioexception e)
out.println(e.getmessage());
{}
~~列出指定目錄下的文件列表
import java.io.*;
string cdur = system.getproperty("user.dir");
string filebar = system.getproperty("file.separator");
string mydir =cdur+filebar+"doc"+filebar+"jspsky";
file my = new file(mydir);
string d[] = my.list();
int i;
int l=d.length;
for(i=0;i out.print(d[i]);
{}
4.計數器
integer count = null;
synchronized (application)
count =(integer) application.getattribute("d");
if (count ==null)
count =new integer("0");
count = new integer(count.intvalue()+1);
application.setattribute("d",count);
{}
out.println(count);
// 首先定義一個整形對象,并初始化為:null,
// 取回application對像的屬性d的值,并強制轉化為整形對象,賦給count
// 判斷count是否為空,為空時,將o賦給count對象,
// 否則,通過count。intvalue()方法,實現count對象加1,并賦值給count
// 最后,將count對象保存在application對象的d變量中。
+++++++++++++++++++
下一步學習重點
文件的刪除
文件內容的修改
圖片的上傳
郵件列表
javabeans
ejb
xml
javascript
對數據庫的操作及維護
了解商業項目開發流程
實例練習
留言板
聊天室
發送郵件
新聞系統
截取網頁內容
購物車
多做練習,在實踐中不斷熟悉java包的使用!
新聞熱點
疑難解答