|
<html>
<head>
<title> my first JSP-database sample </title>
<meta name="author" content="Administrator"/>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<link rel="stylesheet" type="text/css" href="jdbc2.css" title="style1"/>
</head>
<!-- localhost:8080/jsp1.jsp -->
<body>
<h3>my first JSP-database sample</h3>
<%@ page import = "java.sql.* " %>
<%@ page import = "java.lang.reflect.* " %>
<%
String dbdrv = "com.mysql.jdbc.Driver";
String database = "Amacon";
String user="root";
String passwd="";
String dbUrl1 ="jdbc:mysql://localhost:3306/";
Connection cn = null;
Statement st = null;
ResultSet rs = null;
try {
Class.forName( dbdrv );
String url = dbUrl1+database;
cn = DriverManager.getConnection(url , user, passwd);
st = cn.createStatement();
rs = st.executeQuery( "select * from customer" );
// Get meta data:
ResultSetMetaData rsmd = rs.getMetaData();
int anzCols = rsmd.getColumnCount();
out.println("anzCols: "+anzCols+"<br />");
for(int i=1; i<=anzCols; i++ ) {
out.print( rsmd.getColumnName( i ) );
out.print( " " );
} // for
out.println(" ");
// loop over all tupels
while( rs.next() ) {
for(int i=1; i<=anzCols; i++ ) { // Attention: first column with 1 instead of 0
out.print( rs.getString(i) );
out.println(", ");
} // for
out.println( "<br />" );
} // while
} catch( Exception ex ) {
out.println( ex+"<br />" );
} // try
finally {
try { if( null != rs ) rs.close(); } catch( Exception ex ) {}
try { if( null != st ) st.close(); } catch( Exception ex ) {}
try { if( null != cn ) cn.close(); } catch( Exception ex ) {}
}
%>
</body>
</html>
|
|