java技术博客

jsp博客
数据加载中……
JDBC连接SQLSERVER
<%@page language="java" import="java.util.*,java.sql.*,Oper.*,voo.*" pageEncoding="GBK"%>
<table border=1>
<tr>
    
<th>编号</th>
    
<th>名称</th>
    
<th>类别</th>
    
</tr>
<%
DbOper op
=new DbOper();
ArrayList al
=op.select();
for(int i=0;i<al.size();i++)
{
    Book book
=(Book)al.get(i);
    
%>
<tr><td><%=book.getTitleid()%></td>
<td><%=book.getTitle()%></td>
<td><%=book.getType()%></td>
<td><%=book.getPrice()%></td>
</tr>
<%
}
%>
</table>






package db;
import java.sql.*;
public class DbConn{
public static Connection getConn(){
    
return getConnByJar();
    
    }




public static Connection getConnByJar(){Connection conn=null;
try{Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
    conn
=DriverManger.getConnection("jdbc:sqlserver://localhost:1433;databasename=pubs","sa","");
    }

catch(Exception e)
{
    e.printStackTrace();
    }
return conn;
}

//XP4不支持导入jar文件,只能用桥接JDBC-ODBC






public static Connection getConnByODBC(){
    Connection conn
=null;
    
try{Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
        conn
=DriverManager.getConnection("jdbc:odbc:abc");
        }

    
catch(Exception e)
    
{
        
        e.printStackTrace();
        }

        
return conn;}

}




package db;
import java.sql.*;
public class DbConn{
public static Connection getConn(){
    
return getConnByJar();
    
    }




public static Connection getConnByJar(){Connection conn=null;
try{Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
    conn
=DriverManger.getConnection("jdbc:sqlserver://localhost:1433;databasename=pubs","sa","");
    }

catch(Exception e)
{
    e.printStackTrace();
    }
return conn;
}

//XP4不支持导入jar文件,只能用桥接JDBC-ODBC






public static Connection getConnByODBC(){
    Connection conn
=null;
    
try{Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
        conn
=DriverManager.getConnection("jdbc:odbc:abc");
        }

    
catch(Exception e)
    
{
        
        e.printStackTrace();
        }

        
return conn;}

}


package oper;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;
import voo.Book;

public class DbOper{
    
public ArrayList select(){//动态数组和连接无关,不用resultset
        Connction conn=null;
        java.sql.PreparedStatement pstmt
=null;
        java.sql.ResultSet rs
=null;
        ArrayList al
=new ArrayList();
        
try{
            conn
=db.DbConn.getConn();
            pstmt
=conn.prepareStatement("select title_id,title,type,price from titles");
            rs
=pstmt.executeQuery();
            
while(rs.next())
            
{
                Book b
=new Book();
                b.setTitleid(rs.getString(
1));
                b.setType(rs.getString(
2));
                b.setPrice(rs.getString(
3));
                b.setPrice(rs.getFloat(
4))
                al.add(b);
                }
}

                
catch(SQLException e)
                
{
                    e.printStackTrace();
                    }

                    
finally{
                        
if(rs!=null)
                        
try{
                            rs.close();
                            }

                            
catch(SQLException e)
                            
{
                                e.printStackTrace();
                                }

                                
if(pstmt!=null)
                                
try{
                                    pstmt.close();
                                    }

                                    
catch(SQLException e)
                                    
{
                                        e.printStackTrace();
                                        }

                                        
if(conn!=null)
                                        
try{
                                    conn.close();}

                                    
catch(SQLException e)
                                    
{
                                        e.printStackTrace();
                                        }

                                    }

                                    
return al;
        
        }

        
}


package voo;
public class Book{
    
private String titleid;
    
private String title;
    
private String type;
    
private float price;
    //set和get方法就不再哆嗦了
    ........}



posted on 2008-10-26 09:46 郭兴华 阅读(1820) 评论(0)  编辑  收藏


只有注册用户登录后才能发表评论。


网站导航: