这里采用jdbc-odbc桥,这里的bookbase是在odbc中配置的系统数据源(dsn)
查询数据:
import java.io.*;
import java.sql.*;
class BookQuery
{
public static void main(String args[])
{
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
}
catch(ClassNotFoundException ce)
{
System.out.println("SQLException: "+ce.getMessage());
}
try
{
Connection con=DriverManager.getConnection("jdbc:odbc:bookbase");
Statement stmt=con.createStatement();
ResultSet rs=stmt.executeQuery("select * from bookTab");
while(rs.next())
{
System.out.println(
"书号: "+rs.getInt(1)+"\t"+
"书名: "+rs.getString(2)+"\t"+
"作者: "+rs.getString(3)+"\t"+
"单价: "+rs.getFloat(4));
}
stmt.close();
con.close();
}
catch(SQLException e)
{
System.out.println("SQLException: "+e.getMessage());
}
}
}
查询数据:
import java.io.*;
import java.sql.*;
class BookQuery
{
public static void main(String args[])
{
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
}
catch(ClassNotFoundException ce)
{
System.out.println("SQLException: "+ce.getMessage());
}
try
{
Connection con=DriverManager.getConnection("jdbc:odbc:bookbase");
Statement stmt=con.createStatement();
ResultSet rs=stmt.executeQuery("select * from bookTab");
while(rs.next())
{
System.out.println(
"书号: "+rs.getInt(1)+"\t"+
"书名: "+rs.getString(2)+"\t"+
"作者: "+rs.getString(3)+"\t"+
"单价: "+rs.getFloat(4));
}
stmt.close();
con.close();
}
catch(SQLException e)
{
System.out.println("SQLException: "+e.getMessage());
}
}
}
解决方案 »
- SpringMVC的Controller,Struts2的Action问题
- myeclipse文件发布到jboss上没反应并且没报错,有时候jboss会启动不了一般是好的
- 一个rmi入门级的程序
- 用Tomcat作为应用服务器的WEB应用程序的远程维护问题,请大家指教。
- 请问如何用hibernate处理跨数据库的长事务
- setProperty我哪里错了啊?
- 采用j2ee就肯定是B/s形式吗?
- 在weblogic8.1中,怎样配置可以,class修改后,不用重新deploy或重起服务。
- 一个关于上传的问题
- struts的初级问题
- 配置JDBC
- 怎么把文件放到oracle里啊??(小弟不懂oracle)
这里tel字段为关键字段
import java.sql.*;
public class CreateAuthTab
{
public static void main(String args[])
{
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
}
catch(java.lang.ClassNotFoundException e)
{
System.out.println(e.getMessage());
}
try
{
Connection con=DriverManager.getConnection("jdbc:odbc:bookbase");
Statement stmt=con.createStatement();
stmt.executeUpdate("create table authTab(" +
"auth char(10),"+
"tel char(12) CONSTRAINT authIndex PRIMARY KEY)");
stmt.close();
con.close();
}
catch(SQLException ex)
{
System.out.println("SQLException: " +ex.getMessage());
}
}
}
import java.sql.*;
public class InsertAuthRec
{
public static void main(String args[])
{
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
}
catch(ClassNotFoundException e)
{
System.out.println(e.getMessage());
}
try
{
Connection con=DriverManager.getConnection("jdbc:odbc:bookbase");
Statement stmt=con.createStatement();
stmt.executeUpdate("insert into authTab values(" +
"'张三','(021)33311111')");
stmt.executeUpdate("insert into authTab values(" +
"'李四','(021)33322222')");
stmt.executeUpdate("insert into authTab values(" +
"'王五','(021)33344444')"); ResultSet rs=stmt.executeQuery("select * from authTab");
while(rs.next())
{
System.out.println(rs.getString("auth")+"\t"+rs.getString("tel"));
}
stmt.close();
con.close();
}
catch(SQLException ex)
{
System.err.println("SQLException: " +ex.getMessage());
}
}
}
string dbDriver
string dbURL,dbUID,dbPWD
Connection m_conn;
dbDriver = sun.jdbc.odbc.JdbcOdbcDriver
dbURL = jdbc:odbc:数据源名
dbUID = 用户名
dbPWD = 密码
********************************************/public boolean connect()
{
Statement stmt = null;
ResultSet rs = null;
String sql = null;
try
{
Class.forName(dbDriver);
m_conn=DriverManager.getConnection(dbURL,dbUID,dbPWD);
stmt = m_conn.createStatement();
sql = "select i* from tableName where ........."'";
rs = stmt.executeQuery(sql); //rs可以返回查询的结果集 可以用执行任何标准的sql语句
}
catch(Exception ex)
{
debugError("连接数据库失败"+ex.toString());
return false;
}
}
<head>
<title>Ìí¼ÓÓû§ÐÅÏ¢-Access2000</title>
</head>
<body>
<center><h2>Ìí¼ÓÓû§ÐÅÏ¢</h2>
<hr>
<form action=insert_2.jsp method=post>
<p>ÐÕÃû:<input type=text name=name>
<P>ÐÔ±ð:<input type=text name=sex>
<p>µØÖ·:<input type=text name=address>
<p>E-Mail:<input type=text name=email>
<p><input type=submit name=confirm value=È·¶¨>
<input type=reset name=reinput value= ÖØÌî>
</p>
</form>
</center>
</body>
</html>ÕâÀïÊÇinsert_2.jspÎļþ£¬µ÷ÓÃinsert_3.jsp<%@ page language="java" import ="java.sql.*"%>
<%@ page contentType="text/html;charset=gb2312"%>
<html>
<head>
<title>Ìí¼ÓÓû§ÐÅÏ¢-Access2000</title>
</head>
<body>
<jsp:useBean id="view" scope="page" class="access.access_dbconnect"/>
<center><h2>Ìí¼ÓÓû§ÐÅÏ¢</h2>
<hr>
<%
String name1=request.getParameter("name");
byte[]tempbyte1=name1.getBytes("ISO8859_1");
String name=new String(tempbyte1);
String sex1=request.getParameter("sex");
byte[]tempbyte2=sex1.getBytes("ISO8859_1");
String sex=new String(tempbyte2);
String address1=request.getParameter("address");
byte[]tempbyte3=address1.getBytes("ISO8859_1");
String address=new String(tempbyte3);
String email1=request.getParameter("email");
byte[]tempbyte4=email1.getBytes("ISO8859_1");
String email=new String(tempbyte4);
%>
<p>ÐÕÃû:<%=name%>
<p>ÐÔ±ð:<%=sex%>
<p>µØÖ·:<%=address%>
<p>E-Mail:<%=email%>
<form action=insert_3.jsp method=post>
<input type=hidden name=name value=<%=name%>>
<input type=hidden name=sex value=<%=sex%>>
<input type=hidden name=address value=<%=address%>>
<input type=hidden name=email value=<%=email%>>
<p>
<input type=submit name=confirm value=È·¶¨>
<input type=button name=back value=·µ»Ø onclick="javascript:history.go(-1)">
</p>
</center>
</form>
</body>
</html>ÕâÀïÊÇinsert_3.jspÎļþ<%@ page language="java" import ="java.sql.*"%>
<%@ page contentType="text/html;charset=gb2312"%>
<html>
<head>
<title>Ìí¼ÓÓû§ÐÅÏ¢-Access2000</title>
</head>
<body>
<jsp:useBean id="insert" scope="page" class="access.access_dbconnect"/>
<% String name1=request.getParameter("name");
byte[]tempbyte1=name1.getBytes("ISO8859_1");
String name=new String(tempbyte1);
String sex1=request.getParameter("sex");
byte[]tempbyte2=sex1.getBytes("ISO8859_1");
String sex=new String(tempbyte2);
String address1=request.getParameter("address");
byte[]tempbyte3=address1.getBytes("ISO8859_1");
String address=new String(tempbyte3);
String email1=request.getParameter("email");
byte[]tempbyte4=email1.getBytes("ISO8859_1");
String email=new String(tempbyte4); String sql_1="insert into user_info(name,sex,address,email) values ('"+name +"','"+sex+"','"+address+"','"+email+"')";
insert.executeUpdate(sql_1);
%>
<center><h2>Ìí¼ÓÓû§ÐÅÏ¢</h2>
<hr>
<br>
Ç×°®µÄ<font color=blue><%=name%></font>Óû§,ÄúµÄÐÅÏ¢ÒѾ­Ìí¼Óµ½Access2000Êý¾Ý¿âÖÐ,лл!!
<form action=index.htm method=post>
<input type=submit name=back value=·µ»Ø>
</form>
</body>
</html>
<html>
<head>
<title>添加用户信息-Access2000</title>
</head>
<body>
<center><h2>添加用户信息</h2>
<hr>
<form action=insert_2.jsp method=post>
<p>姓名:<input type=text name=name>
<P>性别:<input type=text name=sex>
<p>地址:<input type=text name=address>
<p>E-Mail:<input type=text name=email>
<p><input type=submit name=confirm value=确定>
<input type=reset name=reinput value= 重填>
</p>
</form>
</center>
</body>
</html>这为insert_2.jsp,调用insert_3.jsp
%@ page language="java" import ="java.sql.*"%>
<%@ page contentType="text/html;charset=gb2312"%>
<html>
<head>
<title>添加用户信息-Access2000</title>
</head>
<body>
<jsp:useBean id="view" scope="page" class="access.access_dbconnect"/>
<center><h2>添加用户信息</h2>
<hr>
<%
String name1=request.getParameter("name");
byte[]tempbyte1=name1.getBytes("ISO8859_1");
String name=new String(tempbyte1);
String sex1=request.getParameter("sex");
byte[]tempbyte2=sex1.getBytes("ISO8859_1");
String sex=new String(tempbyte2);
String address1=request.getParameter("address");
byte[]tempbyte3=address1.getBytes("ISO8859_1");
String address=new String(tempbyte3);
String email1=request.getParameter("email");
byte[]tempbyte4=email1.getBytes("ISO8859_1");
String email=new String(tempbyte4);
%>
<p>姓名:<%=name%>
<p>性别:<%=sex%>
<p>地址:<%=address%>
<p>E-Mail:<%=email%>
<form action=insert_3.jsp method=post>
<input type=hidden name=name value=<%=name%>>
<input type=hidden name=sex value=<%=sex%>>
<input type=hidden name=address value=<%=address%>>
<input type=hidden name=email value=<%=email%>>
<p>
<input type=submit name=confirm value=确定>
<input type=button name=back value=返回 onclick="javascript:history.go(-1)">
</p>
</center>
</form>
</body>
</html>这为insert_3.jsp<%@ page language="java" import ="java.sql.*"%>
<%@ page contentType="text/html;charset=gb2312"%>
<html>
<head>
<title>添加用户信息-Access2000</title>
</head>
<body>
<jsp:useBean id="insert" scope="page" class="access.access_dbconnect"/>
<% String name1=request.getParameter("name");
byte[]tempbyte1=name1.getBytes("ISO8859_1");
String name=new String(tempbyte1);
String sex1=request.getParameter("sex");
byte[]tempbyte2=sex1.getBytes("ISO8859_1");
String sex=new String(tempbyte2);
String address1=request.getParameter("address");
byte[]tempbyte3=address1.getBytes("ISO8859_1");
String address=new String(tempbyte3);
String email1=request.getParameter("email");
byte[]tempbyte4=email1.getBytes("ISO8859_1");
String email=new String(tempbyte4); String sql_1="insert into user_info(name,sex,address,email) values ('"+name +"','"+sex+"','"+address+"','"+email+"')";
insert.executeUpdate(sql_1);
%>
<center><h2>添加用户信息</h2>
<hr>
<br>
亲爱的<font color=blue><%=name%></font>用户,您的信息已经添加到Access2000数据库中,谢谢!!
<form action=index.htm method=post>
<input type=submit name=back value=返回>
</form>
</body>
</html>
package access;
import java.sql.*;
public class access_dbconnect
{
String sDBDriver="sun.jdbc.odbc.JdbcOdbcDriver";
String sConnStr="jdbc:odbc:access_test"; Connection conn=null;
ResultSet rs=null; public access_dbconnect()
{
try
{
Class.forName(sDBDriver);
}
catch(java.lang.ClassNotFoundException e)
{
System.err.println("Class aceess_dbconnect not found!"+e.getMessage());
}
} public ResultSet executeQuery(String sql)
{
rs=null;
try
{
conn=DriverManager.getConnection(sConnStr);
Statement stmt=conn.createStatement();
rs=stmt.executeQuery(sql);
}
catch(SQLException ex)
{
System.err.println("aq.executeQuery:"+ex.getMessage());
}
return rs;
}