如题(听说,还要用到Microsoft SQL Server 2005 JDBC Driver 我不知道怎么用),写的详细些,有加分,谢谢大家了。

解决方案 »

  1.   

    package com.shoppingcar.db;import java.sql.*;/**
     * 此类用于连接数据库和关闭数据库连接
     * */public class ConnectionDB { //此方法连接数据库
    public static Connection getDBCon()
    {
    Connection con = null;
    String DRIVER = "com.microsoft.jdbc.sqlserver.SQLServerDriver";
    String URL = "jdbc:microsoft:sqlserver://localhost:1433;databaseName=shoppingcar";
    String USER = "sa";
    String PASSWORD = "";

    try {
    Class.forName(DRIVER);
    con = DriverManager.getConnection(URL,USER,PASSWORD);
    } catch (Exception e) {
    System.out.println("数据库连接失败!");
    e.printStackTrace();
    }
    return con;
    }

    //此方法关闭数据库连接
    public static void closeCon(Connection con)
    {
    if(con != null)
    {
    try {
    con.close();
    } catch (SQLException e) {
    e.printStackTrace();
    }
    }
    }

    }
      

  2.   

    首先把Microsoft SQL Server 2005 JDBC Driver 放到你的构建路径下大致步骤如下:
    Class.forName("com.microsoft.sqlserver.jdbc.SQLServer");
    String url=...//你的数据库连接
    Connection con = DriverManager.getConnection(url,"用户名","密码");
    Statement st = con.createStatement();
    ResultSet rs = st.executeQuery("你的数据库查询语句");
    rs.get.......
      

  3.   

    我用的SQLserver2000
    直连须要三个连接数据库的jar包。
    不知道你说的2005是否也要
      

  4.   

    先去
    http://www.microsoft.com/downloads/details.aspx?displaylang=zh-cn&FamilyID=c47053eb-3b64-4794-950d-81e1ec91c1ba
    下载jdbc driver
    解压然后把里面的sqljdbc.jar放入你的环境变量参考程序:import java.sql.*;public class jdbc_test {
    public static void main(String[] args){
    String url ="jdbc:sqlserver://数据库所在主机IP:端口;DatabaseName=数据库名";  //端口可以不填,那就是默认的3433端口
    String username = "你的用户名";
    String password = "你的密码";
    Connection conn = null; try
    {
    Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver"); //加载jdbc驱动
    }
    catch(ClassNotFoundException e)
    {
    System.out.println(e);
    }
    try
    {
    conn = DriverManager.getConnection(url,username,password); //连接数据库//然后查询\插入\删除语句就在后面写了,具体你是喜欢用PreparedStatemen(推荐)还是Statement就看你自己了,调用存储过程CallableStatement
    //举个例子给你
    Statement stmt =conn.createStatement();
    sql = "select * from XXX";
    ResultSet rs = stmt.executeQuery(sql); //执行sql语句并将结果集给rswhile(rs.next())
    {
     String xxx = rs.getString(1); //按照结果集内容取出
     int xxxx = rs.getInt(2); rs.close();
     stmt.close();
    }
    }
    catch(SQLException e)
    {
    System.out.println(e);
    }
    finally
    {
    try
    {
    if(conn != null)
    conn.close(); //关闭数据库
    }
    catch(SQLException e)
    {
    System.out.println("关闭数据库异常");
    }
    }