以access为例:
两种方法:
1.直接使用access的jdbc驱动;
2.使用access的odbc驱动,再使用java的jdbc/odbc桥接器package mypackage;import java.sql.*;/** A JDBC example that connects to the MicroSoft Access sample
* Northwind database, issues a simple SQL query to the
* employee table, and prints the results.
* <P>
* Taken from Core Servlets and JavaServer Pages 2nd Edition
* from Prentice Hall and Sun Microsystems Press,
* http://www.coreservlets.com/.
* © 2003 Marty Hall and Larry Brown.
* May be freely used or adapted.
*/public class NorthwindTest {
public static void main(String[] args) {
String driver = "sun.jdbc.odbc.JdbcOdbcDriver";
String url = "jdbc:odbc:Northwind";
String username = ""; // No username/password required
String password = ""; // for desktop access to MS Access.
showEmployeeTable(driver, url, username, password);
} /** Query the employee table and print the first and
* last names.
*/
public static void showEmployeeTable(String driver,
String url,
String username,
String password) {
try {
// Load database driver if it's not already loaded.
Class.forName(driver);
// Establish network connection to database.
Connection connection =
DriverManager.getConnection(url, username, password);
System.out.println("Employees\n" + "==========");
// Create a statement for executing queries.
Statement statement = connection.createStatement();
String query =
"SELECT firstname, lastname FROM employees";
// Send query to database and store results.
ResultSet resultSet = statement.executeQuery(query);
// Print results.
while(resultSet.next()) {
System.out.print(resultSet.getString("firstname") + " ");
System.out.println(resultSet.getString("lastname"));
}
connection.close();
} catch(ClassNotFoundException cnfe) {
System.err.println("Error loading driver: " + cnfe);
} catch(SQLException sqle) {
System.err.println("Error with connection: " + sqle);
}
}
}
两种方法:
1.直接使用access的jdbc驱动;
2.使用access的odbc驱动,再使用java的jdbc/odbc桥接器package mypackage;import java.sql.*;/** A JDBC example that connects to the MicroSoft Access sample
* Northwind database, issues a simple SQL query to the
* employee table, and prints the results.
* <P>
* Taken from Core Servlets and JavaServer Pages 2nd Edition
* from Prentice Hall and Sun Microsystems Press,
* http://www.coreservlets.com/.
* © 2003 Marty Hall and Larry Brown.
* May be freely used or adapted.
*/public class NorthwindTest {
public static void main(String[] args) {
String driver = "sun.jdbc.odbc.JdbcOdbcDriver";
String url = "jdbc:odbc:Northwind";
String username = ""; // No username/password required
String password = ""; // for desktop access to MS Access.
showEmployeeTable(driver, url, username, password);
} /** Query the employee table and print the first and
* last names.
*/
public static void showEmployeeTable(String driver,
String url,
String username,
String password) {
try {
// Load database driver if it's not already loaded.
Class.forName(driver);
// Establish network connection to database.
Connection connection =
DriverManager.getConnection(url, username, password);
System.out.println("Employees\n" + "==========");
// Create a statement for executing queries.
Statement statement = connection.createStatement();
String query =
"SELECT firstname, lastname FROM employees";
// Send query to database and store results.
ResultSet resultSet = statement.executeQuery(query);
// Print results.
while(resultSet.next()) {
System.out.print(resultSet.getString("firstname") + " ");
System.out.println(resultSet.getString("lastname"));
}
connection.close();
} catch(ClassNotFoundException cnfe) {
System.err.println("Error loading driver: " + cnfe);
} catch(SQLException sqle) {
System.err.println("Error with connection: " + sqle);
}
}
}
解决方案 »
- 数据保存乱码,显示却正常,如何做到显示和数据库里面都是正常的
- 请高手解决下关天jsp页面表单验证
- 谁有FCKeditor-2.3.zip(java)压缩包,发给me一个!
- 为什么jsp的论坛系统很少。用的也很少?
- 文件管理办公系统的难题
- myeclipse下启动tomcat出现的问题
- 什么是java.lang.UnsupportedOperationException
- jsp操作字符串问题?
- [求助]JSP与ACCESS做的,用数据源ODBC作为驱动的在线购物网页
- RegisterHandler
- applet 工作流的效果,按照http://dev.csdn.net/develop/article/16/16454.shtm上面所说的
- help me!!!有分的[100分]
<%@ page import="java.sql.*"%>
<%@ page import="oracle.jdbc.driver.*"%>
<%@ page import="java.net.*"%>
<%@ page import="java.lang.*"%>
<%@ page import="java.io.*"%>
<%@ page import="java.util.*"%>
<html>
<body>
<%
try
{
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection conn= DriverManager.getConnection("jdbc:oracle:thin:@127.0.0.1:1521:ora90","wrq","wrq");
Statement stmt=conn.createStatement();
String sql="select * from TEST";
ResultSet rs=stmt.executeQuery(sql);
while(rs.next())
{
%>
您的第一个字段内容为:<%=rs.getString(1)%>
您的第二个字段内容为:<%=rs.getString(2)%>
<%
}
out.print("数据库操作成功,恭喜你");
}
catch(java.lang.ClassNotFoundException e)
{
System.out.println("myconn():"+e.getMessage());
}
catch(java.sql.SQLException e)
{
System.out.println("conn():"+e.getMessage());
}
catch(Exception e)
{
System.out.println("Driver error!");
System.err.println(e.getMessage());
}
%>
</body>
</html>