歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Unix知識 >> 關於Unix >> JSP入門知識總結

JSP入門知識總結

日期:2017/3/6 15:28:17   编辑:關於Unix
JSP入門知識總結 1.傳遞表單參數: String name = new String(request.getParameter(name)); 2. 數據庫 連接: ~~MY SQL //設置數據庫的URL String url = jdbc: mysql ://localhost:3306/jspsky; try //加載驅動程序 Class.forname(org.gjt.mm.mysql.Driver) 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變量中。


Copyright © Linux教程網 All Rights Reserved