不需要任何框架和开发工具,怎么连接数据库

解决方案 »

  1.   


    JDBC?小的不才,沒看明白
      

  2.   

    连接数据库通用类:import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.PreparedStatement;
    import java.sql.ResultSet;
    import java.sql.SQLException;
    import java.sql.Statement;public class ConnectionManager { /**
     * 返回连接
     * 
     * @return Connection
     */
    public static synchronized Connection getConnection() {
    // 读出配置信息
    String driverClassName = Env.getInstance().getProperty("driver");
    String url = Env.getInstance().getProperty("url");
    String password = Env.getInstance().getProperty("password");
    String user = Env.getInstance().getProperty("user");
    Connection con = null;
    try {
    // 加载数据库驱动程序
    Class.forName(driverClassName);
    con = DriverManager.getConnection(url,user,password);
    } catch (Exception ex) {
    ex.printStackTrace();
    }
    return con;
    } /**
     * 关闭连接
     * 
     * @param con
     */
    public static void closeConnection(Connection con) {
    try {
    if (con != null && !con.isClosed()) {
    con.close();
    }
    } catch (SQLException ex1) {
    System.err.println("Could not close connection : "
    + ex1.getMessage());
    }
    } /**
     * 关闭结果集
     * 
     * @param res
     *            ResultSet
     */
    public static void closeResultSet(ResultSet res) {
    try {
    if (res != null) {
    res.close();
    res = null;
    }
    } catch (SQLException e) {
    e.printStackTrace();
    }
    } /**
     * 关闭语句
     * 
     * @param pStatement
     *            PreparedStatement
     */ public static void closeStatement(PreparedStatement pStatement) {
    try {
    if (pStatement != null) {
    pStatement.close();
    pStatement = null;
    }
    } catch (SQLException e) {
    e.printStackTrace();
    }
    }

    /**
     * 关闭Statement
     */
    public static void closeStatement(Statement state) {
    try {
    if (state != null) {
    state.close();
    state = null;
    }
    } catch (SQLException e) {
    e.printStackTrace();
    }
    }
    }
    配置文件名为:db.properties的信息
    driver=com.microsoft.sqlserver.jdbc.SQLServerDriver
    url=jdbc:sqlserver://localhost:1433;DatabaseName=bookInfo
    user=sa
    password=123456获取配置文件信息类import java.io.InputStream;
    import java.util.Properties;public final class Env extends Properties { private static final long serialVersionUID = 1L; private static Env instance; /**
     * 公有的获取实例的方法
     * @return Env 实例
     */
    public static Env getInstance() {
    if (instance != null) {
    return instance;
    } else {
    makeInstance();
    return instance;
    }
    } /**
     * 同步的创建实例方法
     *
     */
    private static synchronized void makeInstance() {
    if (instance == null) {
    instance = new Env();
    }
    }

    /**
     * 私有的构造方法,确保实例的唯一性
     *
     */
    private Env() {
    InputStream is = getClass().getResourceAsStream("/db.properties");
    try {
    load(is);
    } catch (Exception e) {
    System.err.println("错误:没有读取属性文件,请确认db.property文件是否存在。");
    return;
    }
    }
    }
    通用操作类(增删改查)import java.sql.Connection;
    import java.sql.PreparedStatement;
    import java.sql.ResultSet;
    import java.sql.SQLException;
    import java.sql.Statement;
    import java.util.ArrayList;
    import java.util.List;import javax.servlet.jsp.jstl.sql.*;public class SqlCommandBean { private Connection conn = null;// 连接数据库对象
    private List sqlValue;// sql语句的参数
    private String sql;// 执行的sql语句 // 设置参数
    public void setSqlValue(List sqlValue) {
    this.sqlValue = sqlValue;
    } // 设置sql语句
    public void setSql(String sql) {
    this.sql = sql;
    }

    //设置数据库连接
    public void setConn(Connection conn) {
    this.conn = conn;
    }

    /**
     * 定义语句的参数
     * 
     * @throws Exception
     */
    private void setParameter(PreparedStatement preStatement) throws Exception {
    for (int i = 0; i < sqlValue.size(); i++) {
    preStatement.setObject(i + 1, sqlValue.get(i));
    }
    } /**
     * 执行Update,Insert,Delete语句
     */
    public int executeUpdate() {
    int result = 0;
    PreparedStatement preStatement = null;
    Statement state = null;
    try {
    // 判断是否带有参数
    if (sqlValue != null && sqlValue.size() > 0) {
    preStatement = conn.prepareStatement(sql);
    setParameter(preStatement);// 设置参数
    result = preStatement.executeUpdate();
    } else {
    state = conn.createStatement();
    result = state.executeUpdate(sql);
    }
    } catch (Exception e) {
    e.printStackTrace();
    } finally {
    if (preStatement != null) {
    ConnectionManager.closeStatement(preStatement);
    }
    if (state != null) {
    ConnectionManager.closeStatement(state);
    }
    if (conn != null) {
    ConnectionManager.closeConnection(conn);
    }
    }
    return result;
    } /**
     * 执行数据库中的查询语句
     */
    public Result executeQuery() {
    Result result = null;
    ResultSet rs = null;
    PreparedStatement preStatement = null;
    Statement state = null;
    try {
    // 判断是否带有参数
    if (sqlValue != null && sqlValue.size() > 0) {
    preStatement = conn.prepareStatement(sql);
    setParameter(preStatement);// 设置参数
    rs = preStatement.executeQuery();
    } else {
    state = conn.createStatement();
    rs = state.executeQuery(sql);
    }
    result = ResultSupport.toResult(rs); } catch (Exception e) {
    e.printStackTrace();
    } finally {
    if (rs != null) {
    ConnectionManager.closeResultSet(rs);
    }
    if (preStatement != null) {
    ConnectionManager.closeStatement(preStatement);
    }
    if (state != null) {
    ConnectionManager.closeStatement(state);
    }
    if (conn != null) {
    ConnectionManager.closeConnection(conn);
    }
    }
    return result;
    }
    }
      

  3.   

    说的什么意思? 你意思用查询分析器这种东西去连数据库? 或者类似是SQLPLUS去连ORACLE这种样子?
      

  4.   

    不要开发工具? 这是连JAVA都不用么? 那只能用手动的ODBC了。。 
      

  5.   

    1 注册数据库驱动
    2 建立连接(地址,用户名,密码之类的)
    3 设置SQL
    4 运行SQL,可能包括COMMIT,ROLLBACK操作
    5 关闭连接
      

  6.   

    用jdbc  然后去下载相应的数据库驱动