簡體   English   中英

帶有MS SQL Server 2008的Java Eclipse

[英]Java Eclipse with MS SQL server 2008

我從使用MS SQL Server 2008開始學習c#,我只是想知道是否可以在jsp中實現3層?

我將擁有我的數據訪問層,在這里我的連接和方法(如(添加,刪除,查看,更新)等)以及業務邏輯和UI。

我嘗試這樣做,但是無法顯示輸出。 我嘗試了一種簡單的添加方法,然后查看添加的內容。 (尚無業務邏輯)

這是我的代碼:

DAL公共類{

public static String driver = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
static String connectionURL = "jdbc:sqlserver://localhost; database = Sample; Integratedsecurity = true";


public void addProduct(String productName){
    try {
        Class.forName(driver);
        Connection con = DriverManager.getConnection(connectionURL);

            String sql = "Insert into Products values (?)";
            PreparedStatement pst = con.prepareStatement(sql);
            pst.setString(1, productName);
            pst.closeOnCompletion();
            pst.close();

    } catch (SQLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (ClassNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

public ResultSet viewProduct(){
    ResultSet rs = null;
    try {
        Class.forName(driver);
        Connection con = DriverManager.getConnection(connectionURL);
        String sql = "Select * from Products";

        PreparedStatement pst = con.prepareStatement(sql);
        rs = pst.executeQuery();

        while(rs.next()){
            rs.getInt(2);
        }
    } catch (SQLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (ClassNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return rs;  
}

這是我在其中添加用戶產品的頁面(我只是剪切了html的其他部分)

<%!
    DAL d = new DAL();
%>

<body>
<% 
    String productName = request.getParameter("productName") != null ?   
        request.getParameter("productName") : "No product has been delivered";
    d.addProduct(productName);
%>
<h1> Product: <%= d.viewProduct() %> </h1>
</body>

錯誤顯示java.lang.ClassNotFoundException: com.microsoft.sqlserver.jdbc.SQLServerDriver但是我在classPath中擁有該庫,並且我嘗試不具有僅用於測試目的的3層,但仍然無法正常工作。 返回值為Null和classnotfoundexception。 任何幫助將不勝感激。

您的代碼中存在多個問題:

  • addProduct()調用在該語句上缺少pst.executeUpdate()方法。 因此,插入將永遠不會運行。

  • viewProduct()調用沒有關閉獲得的ResultSet ,這導致JDBC驅動程序中甚至是SQL服務器上的資源懸空。

  • 您可以使用一個連接管理所有呼叫,而無需每次運行都打開一個新的連接。 除此之外,使用后您不會關閉連接。 您應該將新的Java語法用於try-with-resources或添加一個finally塊:

      try ( Statement st = connection.prepareStatement(sql); ) { // use the statement here ... st.executeUpdate(); } catch(Exception e) { } // st will be closed here Statement st = null; try { st = connection.prepareStatement(sql); } catch(Exception e) { } finally { try { if( st != null ) st.close(); } catch (Exception ex) { throw new RuntimeException("cannot close statement", ex); } } 

通用的方法非常駭人聽聞。 如果這是基於JSP的應用程序,則應考慮使用連接池,而不是基於每個調用來處理連接。 連接池在web.xml中配置為數據源,需要根據您使用的容器(Tomcat,Jetty等)進行配置。

如果使用的是容器數據源,則將從數據源繪制連接,而不是直接從JDBC驅動程序繪制連接。 否則,您應該考慮應用程序內部的連接池,該連接池可應要求為您提供現成的連接。

話雖如此:

您將SQL驅動程序的jar文件放在哪里? 調用方式應位於Web應用程序的WEB-INF/lib文件夾中。

首先要確保的是在構建路徑中正確設置了sqlserver庫。 要訪問/更新/刪除數據庫中的條目,請嘗試以下操作:

Connection conn = null;

Statement stmt = null;

try {

    // Register JDBC driver
    Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");

    conn = DriverManager.getConnection(DB_URL, USER, PASSWORD);

    stmt = conn.createStatement();

    String sql = "Enter your update query";

    stmt.executeUpdate(sql);

    // Extract all the records to see the updated records
    sql = "Enter your select query";

    ResultSet rs = stmt.executeQuery(sql);

    while (rs.next()) {

        // Retrieve data by column name in your database
        String username = rs.getString("enter relevant column name from database");

        int logintries = rs.getInt("enter relevant column name from database");

        // Display values to console
        System.out.print("Username : " + username);

        System.out.print(", Login Tries : " + logintries);

    }

    rs.close();

    } catch (SQLException se) {

        se.printStackTrace();

    } catch (Exception e) {

        e.printStackTrace();

    }

如果ms sqlserver庫設置正確,這也應該為您工作。 希望能幫助到你...

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM