Postgresql JSR 檔要放在 ROOT\WEB-INF\lib 底下 (這裡用 Tomcat)

 

<%@ page contentType="text/html; charset=iso-8859-1" language="java" import="java.sql.* " %>
<%@ page import="java.io.*" %>
Hello :
<%
try {
    String driver = "org.postgresql.Driver";
    String url = "jdbc:postgresql://localhost:5432/mydb";
    String username = "postgres";
    String password = "a";
    String myDataField = null;
    String myQuery = "SELECT * FROM users";
    Connection myConnection = null;
    PreparedStatement myPreparedStatement = null;
    ResultSet myResultSet = null;
    Class.forName(driver).newInstance();
    myConnection = DriverManager.getConnection(url,username,password);
    myPreparedStatement = myConnection.prepareStatement(myQuery);
    myResultSet = myPreparedStatement.executeQuery();
    while(myResultSet.next())
        myDataField = myResultSet.getString("name");
    out.print(myDataField);
}

catch(ClassNotFoundException e){
    e.printStackTrace();
    out.print("Error" + e);
}catch (SQLException ex){
    out.print("SQLException: "+ex.getMessage());
    out.print("SQLState: " + ex.getSQLState());
    out.print("VendorError: " + ex.getErrorCode());
}
%>

 




    全站熱搜

    黃彥霖 發表在 痞客邦 留言(0) 人氣()