JSP Connection database: JDBC + access database for beginners

Source: Internet
Author: User

This is a simple jdbc odbc bridge connection.

First, you must create a database. Open the control panel and set the data source name to TestData in the configuration system dsn.

Import java. SQL .*;
Class SqlTest
{
Public static void main (String [] args) throws Exception
{
String driver = "sun. jdbc. odbc. JdbcOdbcDriver ";
String url = "jdbc: odbc: TestData"; // the data source name is TestData.
Connection con = null;
Statement stmt = null;
ResultSet rs = null;
Try
{
Class. forName (driver );
Con = DriverManager. getConnection (url );
Stmt = con. createStatement ();
Rs1_stmt.exe cuteQuery ("select * from adm"); // The table name is adm.
While (rs. next ())
{
String s1 = rs. getString ("ID ");
System. out. println (s1 );
String s2 = rs. getString ("admin ");
System. out. println (s2 );
String s3 = rs. getString ("password ");
System. out. println (s3 );
}
} Catch (Exception e)
{
E. printStackTrace ();
}
Rs. close ();
Con. close ();
}
}

The following is the jsp implementation (basically the same ):

<% @ Page contentType = "text/html" %>
<% @ Page pageEncoding = "UTF-8" %>
<% @ Page import = "java. SQL. *" %>
<Html>
<Meta http-equiv = "Content-Type" content = "text/html; charset = UTF-8">
<Head>
<Title> JDBCTest Page </title>
</Head>
<Body>
<H1> JDBCTest Page <Table border = "1" align = "center">
<Thead>
<Tr>
<Th> </th>
<Th> </th>
<Th> </th>
<Th> </th>
<Th> </th>
</Tr>
</Thead>
<Tr>
<Td> ID </td>
<Td> User: </td>
<Td> password: </td>
<! -- Td> E-MAIL: <td -->
<Td> email password: </td>
</Tr>
<%
Class. forName ("sun. jdbc. odbc. JdbcOdbcDriver ");
String url = "jdbc: odbc: TestData ";
String username = "";
String password = "";
Connection con = DriverManager. getConnection (url, username, password );
Statement stmt = con. createStatement ();
ResultSet rs1_stmt.exe cuteQuery ("select * from adm ");
While (rs. next ())
{
%>
<Tr>
<Td> <% = rs. getString ("ID") %> </td>
<Td> <% = rs. getString ("admin") %> </td>
<Td> <% = rs. getString ("password") %> </td>
</Tr>
<%
}
Rs. close ();
Con. close ();

%>
</Table>
</Body>
</Html>

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.