aslucky

C++

 

jsp数据库连接大全和数据库操作封装到Javabean 转帖 备查

jsp数据库连接大全和数据库操作封装到Javabean

来源 : http://blog.csdn.net/cm4ever/


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

下面是简单的数据库操作Javabean的代码:

DbWrapper.java
import java.sql.*;import java.util.*;publicclass DbWrapper
                        {// 定义连接池对象为静态变量,将一直存在,直到工作目录关闭。privatestatic DataSource ds =null;
// 1.用连接池的方式获得连接// 如果不是做多数据库程序,推荐使用此方法// 相关内容:在tomcat管理界面配置连接池publicstatic Connection openConnection()throws Exception {// 只需要初始化1次if( ds ==null){ Context initContext =newInitialContext(); Context envContext =(Context) initContext.lookup("java:/comp/env"); DataSource ds =(DataSource) envContext.lookup("jdbc/MyDataSource");}return ds.getConnection();}// 2.用jdbc驱动获得连接// 相关内容:JSP数据库连接大全publicstatic Connection openConnection( String driver, String url, String username, String password)throws Exception { Class.forName(driver).newInstance();return DriverManager.getConnection(url, username, password);}publicstaticvoidcloseConnection(Connection conn)throws Exception {if( conn !=null){ conn.close();}}
publicstaticintexecuteUpdate(String sql)throws Exception {int count =0; Connection conn =null; Statement stmt =null;try{ conn =openConnection(); stmt = conn.createStatement(); count = stmt.executeUpdate(sql);}catch( Exception e ){throw e;}finally{closeConnection(conn);}return count;}publicstatic List executeQuery(String sql)throws Exception { List list =newArrayList(); Connection conn =null; Statement stmt =null; ResultSet rs =null;try{ conn =openConnection(); stmt = conn.createStatement(); rs = stmt.executeQuery(sql); ResultSetMetaData rsmd = rs.getMetaData();while( rs.next()){ Map map =newHashMap();for(int i =1; i <= rsmd.getColumnCount(); i++){ map.put(rsmd.getColumnName(i), rs.getObject(i));} list.add(map);}
}catch( Exception e ){ e.printStackTrace();}finally{if( rs !=null) rs.close();closeConnection(conn);}
return list;
}
}

 

// 1.对于insert, update, delete语句
int count = DbWrapper.executeUpdate(sql);
// 2.对于selete语句 java.util.List list = DbWrapper.executeQuery(sql);
// 方法一:按名字取值,注意大小写是严格区分的for(int i =0; i < list.size(); i++){ java.util.Map map =(java.util.Map)list.get(i); out.println(mag.get("column_name").toString());}
// 方法二:遍历取值for(int i =0; i < list.size(); i++){ java.util.Map map =(java.util.Map)list.get(i);
for(java.util.Iterator it = map.keySet().iterator(); it.hasNext();){ String column_name = it.next().toString());

// 取值时注意null判断 out.println(column_name +" = "+ map.get(column_name) ==null?"": map.get(column_name).toString());}}

posted on 2008-10-14 14:40 aslucky 阅读(559) 评论(0)  编辑 收藏 引用 所属分类: web


只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   博问   Chat2DB   管理


导航

统计

常用链接

留言簿(1)

随笔分类(6)

随笔档案(6)

文章分类(2)

文章档案(2)

Open source library

Software often use

搜索

积分与排名

最新评论

阅读排行榜

评论排行榜