標籤:
在進行jsp與jdbc串連時,出現這樣一個錯誤,提示如下:
java.net.ConnectException: Connection refused: connect
後來發現是由於mysql資料庫沒開。
1 <%@ page language="java" contentType="text/html;charset=UTF-8" 2 pageEncoding="UTF-8"%> 3 <%@ page language="java" %> 4 <%@ page import="org.gjt.mm.mysql.Driver" %> 5 <%@ page import="java.sql.*" %> 6 <% 7 //驅動程式名 8 String driverName="com.mysql.jdbc.Driver"; 9 //資料庫使用者名稱 10 String userName="xiaohengdada"; 11 //密碼 12 String userPasswd="123456"; 13 //資料庫名 14 String dbName="xh"; 15 //表名 16 String tableName="account"; 17 //連接字串 18 String url="jdbc:mysql://localhost/xh"; 19 Class.forName("com.mysql.jdbc.Driver").newInstance(); 20 Connection connection=DriverManager.getConnection(url,userName,userPasswd); 21 Statement statement = connection.createStatement(); 22 String sql="SELECT * FROM "+tableName; 23 ResultSet rs = statement.executeQuery(sql); 24 //獲得資料結果集合 25 ResultSetMetaData rmeta = rs.getMetaData(); 26 //確定資料集的列數,亦欄位數 27 int numColumns=rmeta.getColumnCount(); 28 // 輸出每一個資料值 29 out.print("id"); 30 out.print("|"); 31 out.print("name"); 32 out.print("|"); 33 out.print("money");34 out.print("<br>");35 while(rs.next()) { 36 out.print(rs.getString(1));37 out.print("|");38 out.print(rs.getString(2)+" "); 39 out.print("|"); 40 out.print(rs.getString(3)); 41 out.print("<br>"); 42 } 43 out.print("<br>"); 44 out.print("資料庫操作成功,恭喜你"); 45 rs.close(); 46 statement.close(); 47 connection.close(); 48 %>
一個簡單的javaBean標籤訪問使用執行個體
1 package xh; 2 3 /** 4 * @author gacl 5 * Person類就是一個最簡單的JavaBean 6 */ 7 public class Person { 8 9 //------------------Person類封裝的私人屬性---------------------------------------10 // 姓名 String類型11 private String name;12 // 性別 String類型13 private String sex;14 // 年齡 int類型15 private int age;16 //是否已婚 boolean類型17 private boolean married;18 //---------------------------------------------------------19 20 //------------------Person類的無參數構造方法---------------------------------------21 /**22 * 無參數構造方法23 */24 public Person() {25 26 }27 //---------------------------------------------------------28 29 //------------------Person類對外提供的用於訪問私人屬性的public方法---------------------------------------30 public String getName() {31 return name;32 }33 34 public void setName(String name) {35 this.name = name;36 }37 38 public String getSex() {39 return sex;40 }41 42 public void setSex(String sex) {43 this.sex = sex;44 }45 46 public int getAge() {47 return age;48 }49 50 public void setAge(int age) {51 this.age = age;52 }53 54 public boolean isMarried() {55 return married;56 }57 58 public void setMarried(boolean married) {59 this.married = married;60 }61 //---------------------------------------------------------62 }
1 <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> 2 <%-- 3 在jsp中使用jsp:useBean標籤來執行個體化一個Java類的對象 4 <jsp:useBean id="person" class="xh.Person" scope="page"/> 5 ┝<jsp:useBean>:表示在JSP中要使用JavaBean。 6 ┝id:表示產生的執行個體化對象,凡是在標籤中看見了id,則肯定表示一個執行個體對象。 7 ┝class:此對象對應的包.類名稱 8 ┝scope:此javaBean的儲存範圍,四種範圍:page、request、session、application 9 --%>10 <jsp:useBean id="person" class="xh.Person" scope="page"/>11 <%12 //person對象在上面已經使用jsp:useBean標籤執行個體化了,因此在這裡可以直接使用person對象13 //使用setXxx方法為對象的屬性賦值14 //為person對象的name屬性賦值15 person.setName("笑哼");16 //為person對象的Sex屬性賦值17 person.setSex("男");18 //為person對象的Age屬性賦值19 person.setAge(25);20 //為person對象的married屬性賦值21 person.setMarried(false);22 %>23 <!DOCTYPE HTML>24 <html>25 <head>26 <title>javaBean標籤訪問執行個體</title>27 </head>28 29 <body>30 <%--使用getXxx()方法擷取對象的屬性值 --%>31 <h2>姓名:<%=person.getName()%></h2>32 <h2>性別:<%=person.getSex()%></h2>33 <h2>年齡:<%=person.getAge()%></h2>34 <h2>已婚:<%=person.isMarried()%></h2>35 </body>36 </html>
<jsp:useBean>標籤的執行原理:"首先在指定的域範圍內尋找指定名稱的JavaBean對象,如果存在則直接返回該JavaBean對象的引用,如果不存在則執行個體化一個新的JavaBean對象並將它以指定的名稱儲存到指定的域範圍中。
jsp學習(五)