JSP and Postgres, how to fill a table? -
i have database written in postgresql. want database columnms , rows shown in browser. therefore using jsp. in browser 1 can see columnm names. there no information saved in. see screenshot further. problem not know how fill database via jsp using html codes filling table. can me please. screen of current database. can see, there no information. table empty.
this jsp code:
<%@ page contenttype="text/html; charset=iso-8859-1" language="java" import="java.sql.* "%> <%@ page import="java.io.*"%> <html> <head> <title>films example: jsp</title> </head> <body bgcolor="white"> <div> <h1>movies</h1> <table border=1> <tr> <td>imdbid</td> <td>name</td> <td>year</td> <td>rating</td> <td>votes</td> <td>runtime</td> <td>actors</td> <td>genres</td> <td>directors</td> </tr> <% try { string driver = "org.postgresql.driver"; string url = "jdbc:postgresql://localhost:5432/movie"; string username = "postgres"; string password = "mypassword"; string mydatafield = null; string myquery = "select * movie"; 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(); out.println("<table border=1>"); while (myresultset.next()) { string imdbid = myresultset.getstring("imdbid"); string name = myresultset.getstring("name"); int year = myresultset.getint("year"); double rating = myresultset.getdouble("rating"); int votes = myresultset.getint("votes"); int runtime = myresultset.getint("runtime"); string directors = myresultset.getstring("directors"); out.println("<tr><td>" + imdbid + "</td><td>" + name + "</td><td>" + year + "</td><td>" + rating + "</td><td>" + votes + "</td> <td>" + runtime + "</td> <td>" + directors + "</td> </tr>"); } out.println("</table>"); myconnection.close(); } catch (classnotfoundexception e) { e.printstacktrace(); } catch (sqlexception ex) { out.print("sqlexception: " + ex.getmessage()); out.print("sqlstate: " + ex.getsqlstate()); out.print("vendorerror: " + ex.geterrorcode()); } %> </table> </div>
first close scriptlet thag before out.println("<table>"); , after write <table> , inside using expression tag wirte code. print table.
can write seperate java class file database connection , return list object through class jsp in form of session or request/response object. best way instead of writing whole code in jsp page. try , iterate object using foreach loop.
Comments
Post a Comment