jsp連接MySQL資料庫
←手機掃碼閱讀
使用JSP插入數據的常式
<%@ page import="java.sql.*" %>
<%
//告訴編譯器使用SQL包
%>
<%
try{
Class.forName("org.gjt.mm.mysql.Driver");
//載入 mm.mysql.driver
} catch (java.lang.ClassNotFoundException e)
//如果載入時出錯,給出相應的錯誤信息
{
out.print("Class not found exception occur. Message is:");
out.print(e.getMessage());
}
try{
Connection con;
Statement stmt;
ResultSet rs;
con = DriverManager.getConnection("jdbc:mysql://localhost:3306/test?user=root;password=");
//創建資料庫聯接,這樣的做法類似於M$的ASP中的創建資料庫聯接。
stmt = con.createStatement();
stmt.executeUpdate("INSERT INTO mytable (col1,col2) VALUES ('This is a test string',32)");
//執行插入數據的操作
rs = stmt.executeQuery("SELECT * FROM mytable");
//把資料庫中所有的數據讀出來
while (rs.next())
{
String s1 = rs.getString(1);
String s2 = rs.getString(2);
out.println("col1="+s1+"--col2="+s2+"br");
//列印所顯示的數據
}
} catch (SQLException e) {
//如果SQL語句執行的過程中出錯,則顯示出相應的錯誤信息
out.print("SQL Exception occur. Message is:");
out.print(e.getMessage());
}
%>
[火星人 via ] jsp連接MySQL資料庫已經有408次圍觀
http://www.coctec.com/docs/program/show-post-72466.html