姿姿霸霸~~!
贵在坚持!
posts - 106,  comments - 50,  trackbacks - 0
JDBC是一个应用程序编程接口API,它描述了标准的JAVA库如何使用结构化查询语言去连接数据源。
我的一个简单模板:(此处用的是mssql数据库)
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class JdbcTest {
    
    
public void getConnection() throws SQLException, ClassNotFoundException{
        
        Connection conn
=null;
        Statement stmt
=null;
        ResultSet rs
=null;
        
        String url 
="jdbc:microsoft:sqlserver://localhost:1433:DatabaseName=pubs" ;
        String user
="sa";
        String psw
=null;
        String sql
="select * from authors";
        
        Class.forName(
"com.microsoft.jdbc.sqlserver.SQLServerDriver");
        conn
=DriverManager.getConnection(url,user,psw);
        stmt
=conn.createStatement();
        rs
=stmt.executeQuery(sql);
        
        
if (rs.next()){
            System.out.println(
"connection is ok!");
        }

        
else{
            System.out.println(
"connection is fail!");
        }

        
        rs.close();
        stmt.close();
        conn.close();
    }

    
    
public static void main(String[] args) throws Exception {
        JdbcTest test
=new JdbcTest();
        test.getConnection();
    }

}

PS: 因为偷懒,所以直接对有异常的地方采用的是抛出异常,并没捕获。

三大数据库的驱动和URL:
MYSQL:
driver是 org.gjt.mm.mysql.driver
 url是 jdbc:mysql://ip:port:databasename
ORACLE
driver是 oracle.jdbc.driver.oracledriver
 url是 jdbc:oracle:thin@ip:port:sid
MSSQL
driver是 com.microsoft.jdbc.sqlserver.SQLServerDriver
 url是 jdbc:microsoft:sqlserver://ip:port
posted on 2007-01-12 22:11 xrzp 阅读(523) 评论(0)  编辑  收藏 所属分类: JAVA

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


网站导航:
 

<2007年1月>
31123456
78910111213
14151617181920
21222324252627
28293031123
45678910

常用链接

留言簿(4)

随笔分类

随笔档案

好友的blog

搜索

  •  

积分与排名

  • 积分 - 115122
  • 排名 - 505

最新评论

阅读排行榜

评论排行榜