编程语言

img cm4ever

jsp数据库连接大全(代码分色+注释)版本

发表于2004/9/30 14:22:00  42437人阅读

分类: j2ee.jsp

一、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();
%>

阅读全文
1 0

相关文章推荐

img
取 消
img