欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

JSP连接主流数据库 博客分类: JavaEE JSPSQLJDBCSybaseJava 

程序员文章站 2024-02-16 19:29:34
...
一、jsp连接Oracle8/8i/9i数据库(用thin模式)testOracle.jsp如下:
<%@ page contentType="text/html;charset=gb2312"%>
<%@ page import="java.sql.*"%>
<%
    String result = ""; // 查询结果字符串

    String sql = "select * from test"; // SQL 字符串

    // 连接字符串,格式: "jdbc:数据库驱动名称:连接模式:@数据库服务器ip:端口号:数据库SID"
    String url = "jdbc:oracle:thin:@localhost:1521:orcl";
    String username = "scott"; // 用户名
    String password = "tiger"; //密码

    // 创建oracle数据库驱动实例
    Class.forName("oracle.jdbc.driver.OracleDriver").newInstance();

    // 获得与数据库的连接 
    Connection conn = DriverManager.getConnection(url, username, password); 
    // 创建执行语句对象
    Statement  stmt = conn.createStatement();
    // 执行sql语句,返回结果集
    ResultSet  rs   = stmt.executeQuery(sql);

    while ( rs.next() ) 
    {
        result += "\n 第一个字段内容:" + rs.getString(1) + "<BR>";
    }

    rs.close(); // 关闭结果集
    stmt.close(); // 关闭执行语句对象
    conn.close(); // 关闭与数据库的连接
%> 

<HTML>
<BODY>
  <%=result%>
</BODY>
</HTML>


二、jsp连接Sql Server7.0/2000数据库testSqlServer.jsp如下
<%@ page contentType="text/html;charset=gb2312"%>
<%@ page import="java.sql.*"%>
<%
    String sql = "select * from test";

    // 连接字符串,格式: "jdbc:公司名称:数据库驱动名称://数据库服务器ip:端口号;DatabaseName=数据库名称"
    String url = "jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=pubs";
    String username = "scott";
    String password = "tiger";

    Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver").newInstance();

    Connection conn = DriverManager.getConnection(url, username, password); 
    Statement  stmt = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
    ResultSet  rs   = stmt.executeQuery(sql);

%>

<HTML>
<BODY>

<% 
    while ( rs.next() ) 
    {
        %>
        第一个字段内容为:<%=rs.getStrisng(1)%><BR>
        <%
    }

    rs.close();
    stmt.close();
    conn.close();
%> 

</BODY>
</HTML>


三、jsp连接DB2数据库testDB2.jsp如下:
<%@ page contentType="text/html;charset=gb2312"%>
<%@ page import="java.sql.*"%>
<%
    String sql = "select * from test";

    // 连接字符串,格式: "jdbc:数据库驱动名称://数据库服务器ip:端口号/数据库名称"
    String url = "jdbc:db2://localhost:5000/sample";
    String username = "scott";
    String password = "tiger";

    Class.forName("com.ibm.db2.jdbc.app.DB2Driver").newInstance();

    Connection conn = DriverManager.getConnection(url, username, password); 
    Statement  stmt = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
    ResultSet  rs   = stmt.executeQuery(sql);

    rs.close();
    stmt.close();
    conn.close();
%>

四、jsp连接Informix数据库
testInformix.jsp如下:
<%@ page contentType="text/html;charset=gb2312"%>
<%@ page import="java.sql.*"%>
<%
    String sql = "select * from test";

    // 连接字符串,格式: "jdbc:数据库驱动名称://数据库服务器ip:端口号/数据库名称:INFORMIXSERVER=服务器名;user=用户名;password=密码"
    String url = "jdbc:informix-sqli://123.45.67.89:1533/testDB:INFORMIXSERVER=myserver;user=testuser;password=testpassword";

    Class.forName("com.informix.jdbc.IfxDriver").newInstance();

    Connection conn = DriverManager.getConnection(url); 
    Statement  stmt = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
    ResultSet  rs   = stmt.executeQuery(sql);

    rs.close();
    stmt.close();
    conn.close();
%>


五、jsp连接Sybase数据库
testSybase.jsp如下:
<%@ page contentType="text/html;charset=gb2312"%>
<%@ page import="java.sql.*"%>
<%
    String sql = "select * from test";

    // 连接字符串,格式: "jdbc:公司名称:数据库驱动名称:数据库服务器ip:端口号/数据库名称"
    String url = "jdbc:sybase:Tds:localhost:5007/tsdata";

Properties prop = System.getProperties();
prop.put("user", "userid"); // 用户名
prop.put("password", "user_password"); // 密码

    Class.forName("com.sybase.jdbc.SybDriver").newInstance();

    Connection conn = DriverManager.getConnection(url, prop); 
    Statement  stmt = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
    ResultSet  rs   = stmt.executeQuery(sql);

    rs.close();
    stmt.close();
    conn.close();
%>

六、jsp连接MySQL数据库
testMySQL.jsp如下:
<%@ page contentType="text/html;charset=gb2312"%>
<%@ page import="java.sql.*"%>
<%
    String sql = "select * from test";

    // 连接字符串,格式: "jdbc:数据库驱动名称://数据库服务器ip/数据库名称?user=用户名&password=密码&使用Unicode=布尔值&字符编码=编码"
    String url = "jdbc:mysql://localhost/softforum?user=soft&password=soft1234&useUnicode=true&characterEncoding=8859_1";
    String username = "scott";
    String password = "tiger";

    Class.forName("org.gjt.mm.mysql.Driver").newInstance();

    Connection conn = DriverManager.getConnection(url, username, password); 
    Statement  stmt = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
    ResultSet  rs   = stmt.executeQuery(sql);

    rs.close();
    stmt.close();
    conn.close();
%>


七、jsp连接PostgreSQL数据库
testPostgreSQL.jsp如下:
<%@ page contentType="text/html;charset=gb2312"%>
<%@ page import="java.sql.*"%>
<%
    String sql = "select * from test";

    // 连接字符串,格式: "jdbc:数据库驱动名称://数据库服务器ip/数据库名称"
    String url = "jdbc:postgresql://localhost/soft";
    String username = "scott";
    String password = "tiger";

    Class.forName(""org.postgresql.Driver").newInstance();

    Connection conn = DriverManager.getConnection(url, username, password); 
    Statement  stmt = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
    ResultSet  rs   = stmt.executeQuery(sql);

    rs.close();
    stmt.close();
    conn.close();
%>