jsp或servlet什么连接服务器数据库,数据库是sql server2008 ,老师只给了其地址,数据库名,数据库用户名,密码。我要什么访问上面的表。访问语句该什么写。   谢谢

解决方案 »

  1.   

    以前写的代码
    package com.sias.util;import java.util.PropertyResourceBundle;import java.util.Enumeration;
    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.SQLException;
    import java.sql.Statement;
    import java.sql.PreparedStatement;
    import java.sql.ResultSet;import java.sql.*;public class DB {
    private static  String dbDriver = "com.microsoft.sqlserver.jdbc.SQLServerDriver";

    //localhost改成你的服务器IP地址
    private static String dbUrl = "jdbc:sqlserver://localhost:1433; DatabaseName=storagemanage";

    private static String userName = "root";
    //改成你的数据库名和密码
    private static String passWord = "root";

    private static java.sql.Connection conn = null;
    private static java.sql.Statement stmt = null;
    private static java.sql.ResultSet rs = null;// Connection conn = DriverManager.getConnection(url, "sa", "root");

    static {
    try {
    Class.forName(dbDriver);
    } catch (java.lang.ClassNotFoundException e) {
    e.printStackTrace();
    }
    } public static Connection getConnection() throws java.sql.SQLException {
    if (conn == null || conn.isClosed())
    conn = java.sql.DriverManager.getConnection(dbUrl, userName,
    passWord);
    return conn;
    } public ResultSet excuteQuery(String sqlStr) {
    if (sqlStr == null || sqlStr.equals("")) {
    return null;
    }
    try {
    this.getConnection();
    this.stmt = this.conn.createStatement();
    this.rs = this.stmt.executeQuery(sqlStr);
    } catch (java.sql.SQLException e) {
    e.printStackTrace();
    } return this.rs;
    } public static boolean excuteUpdate(String sqlStr) {
    if (sqlStr == null || sqlStr.equals("")) {
    return false;
    }
    try {
    getConnection();
    stmt = conn.createStatement();
    stmt.executeUpdate(sqlStr);
    } catch (java.sql.SQLException e) {
    e.printStackTrace();
    return false;
    } finally {
    try {
    if (stmt != null) {
    stmt.close();
    }
    } catch (java.sql.SQLException e) {
    e.printStackTrace();
    }
    try {
    if (conn != null) {
    conn.close();
    }
    } catch (java.sql.SQLException e) {
    e.printStackTrace();
    }
    }
    return true;
    } public boolean executeUpdates(String[] sqlStrs) {
    try {
    this.getConnection();
    this.conn.setAutoCommit(false);
    this.stmt = conn.createStatement();
    for (int i = 0; i < sqlStrs.length; i++) {
    this.stmt.addBatch(sqlStrs[i]);
    }
    stmt.executeBatch();
    this.conn.commit();
    this.conn.setAutoCommit(true);
    } catch (SQLException e) {
    try {
    if (conn != null) {
    conn.rollback();
    }
    } catch (SQLException ex) {
    ex.printStackTrace();
    }
    e.printStackTrace();
    return false;
    } finally {
    try {
    if (stmt != null)
    stmt.close();
    } catch (java.sql.SQLException e) {
    e.printStackTrace();
    }
    try {
    if (conn != null)
    conn.close();
    } catch (java.sql.SQLException e) {
    e.printStackTrace();
    }
    }
    return true;
    } public void closeStmt() {
    try {
    if (this.stmt != null) {
    this.stmt.close();
    }
    } catch (java.sql.SQLException e) {
    e.printStackTrace();
    } } public static  void closeConnection() {
    try {
    if (conn != null) {
    conn.close();
    }
    } catch (java.sql.SQLException e) {
    e.printStackTrace();
    }
    }
    }