很菜,别笑!
java怎么连接数据库?数据源怎么弄?
从数据库中读取数据用什么语句?
书讲 的不详细就介绍了大概,连例子都是错的
谁能详细讲解一下,谢谢!

解决方案 »

  1.   

    补充:
    与数据库建立连接是这样的步骤吗?
    先声明数据库的驱动类型,然后再定义驱动对象,然后建在桥驱动程序,然后才用Connection与数据库建立连接???
    是这样的吗???
      

  2.   

    public static Connection getConnection() {
    Connection conn = null;
    try {
    Class.forName("oracle.jdbc.driver.OracleDriver");
    conn = DriverManager.getConnection("jdbc:oracle:thin:@127.0.0.1:1521:ORACLE9", "csdn", "csdn");
    } catch (ClassNotFoundException e) {
    e.printStackTrace();
    throw new java.lang.RuntimeException(e);
    } catch (SQLException e) {
    e.printStackTrace();
    throw new java.lang.RuntimeException(e);
    }
    return conn;
    } 这个是我写的一个获取Java连接Oracle的方法!请LZ参考一下!
      

  3.   

     Java程序连接不同数据库的方法,以下列出八种: 
    1、Oracle8/8i/9i数据库(thin模式) 
    Class.forName("oracle.jdbc.driver.OracleDriver").newInstance(); 
    String url="jdbc:oracle:thin:@localhost:1521:orcl"; //orcl为数据库的SID 
    String user="test"; 
    String password="test"; 
    Connection conn= DriverManager.getConnection(url,user,password); 
    2、DB2数据库 
    Class.forName("com.ibm.db2.jdbc.app.DB2Driver ").newInstance(); 
    String url="jdbc:db2://localhost:5000/sample"; //sample为你的数据库名 
    String user="admin"; 
    String password=""; 
    Connection conn= DriverManager.getConnection(url,user,password); 
    3、SQL Server2000/2005数据库 
    Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver").newInstance(); 
    String url="jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=mydb"; 
    //mydb为数据库 
    String user="sa"; 
    String password=""; 
    Connection conn= DriverManager.getConnection(url,user,password); 
    4、Sybase数据库 
    Class.forName("com.sybase.jdbc.SybDriver").newInstance(); 
    String url =" jdbc:sybase:Tds:localhost:5007/myDB"; //myDB为你的数据库名 
    Properties sysProps = System.getProperties(); 
    SysProps.put("user","userid"); 
    SysProps.put("password","user_password"); 
    Connection conn= DriverManager.getConnection(url, SysProps); 
    5、Informix数据库 
    Class.forName("com.informix.jdbc.IfxDriver").newInstance(); 
    String url = "jdbc:informix-sqli://123.45.67.89:1533/myDB:INFORMIXSERVER=myserver; 
    user=testuser;password=testpassword"; //myDB为数据库名 
    Connection conn= DriverManager.getConnection(url); 
    6、MySQL数据库 
    Class.forName("org.gjt.mm.mysql.Driver").newInstance(); 
    //或者Class.forName("com.mysql.jdbc.Driver"); 
    String url ="jdbc:mysql://localhost/myDB? 
    user=soft&password=soft1234&useUnicode=true&characterEncoding=8859_1" 
    //myDB为数据库名 
    Connection conn= DriverManager.getConnection(url); 
    7、PostgreSQL数据库 
    Class.forName("org.postgresql.Driver").newInstance(); 
    String url ="jdbc:postgresql://localhost/myDB" ; //myDB为数据库名 
    String user="myuser"; 
    String password="mypassword"; 
    Connection conn= DriverManager.getConnection(url,user,password); 
    8、access数据库直连用ODBC的 
    Class.forName("sun.jdbc.odbc.JdbcOdbcDriver") ; 
    String url="jdbc:odbc:Driver={MicroSoft Access Driver 
    (*.mdb)};DBQ="+application.getRealPath("/Data/ReportDemo.mdb"); 
    Connection conn = DriverManager.getConnection(url,"",""); 
    Statement stmtNew=conn.createStatement() ;
    下面时获得数据:PreparedStatement pstmt = conn.prepareStatement(sql);//你的sql语句
    ResultSet rs = pstmt.executeQuery();
    while (rs.next())
    {
      System.out.println(re.getString("数据库字段名称"));
    }
      

  4.   

    楼上的都讲了,不过连接完了,可别忘了关闭!conn.colse()
      

  5.   

    桥接用的比较少啦,直接用jdbc的多点
      

  6.   

    JDBC连接ORacle的例子(通过验证)包括各种操作数据库的方法
    给分吧。
    package com.fenghui.system.jdbc;import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.ResultSet;
    import java.sql.SQLException;
    import java.sql.Statement;import javax.servlet.http.HttpSession;public class ConnectionDB {
        private static String url = "jdbc:oracle:thin:@localhost:1521:arsas";//数据库连接字符串
        private static String username = "arsas";//数据库名
        private static String password = "arsas";//数据库密码
        private static String driverName = "oracle.jdbc.driver.OracleDriver";//数据库驱动名
        /**
         * @deprecated:获得数据库查询记录集
         * @param sql
         * @return ResultSet
         * @throws Exception
         */
        public ResultSet queryDB(String sql)throws Exception {
            ResultSet rs = null;//定义数据查询记录集
            try {
                stmt = this.creatStatement();
                rs = stmt.executeQuery(sql);//执行数据库操作
            }catch(SQLException se) {
                se.printStackTrace();
            }
            return rs;
        }
        /**
         * @deprecated:对数据库的操作(插入,删除,更新)
         * @param sql
         * @return boolean
         * @throws Exception
         */
        public boolean operateDB(String sql)throws Exception {
            try {
                stmt = this.creatStatement();//创建Statement对象
                if(stmt.executeUpdate(sql) == 1){//操作数据库成功
                    operate = true;
                }
            }catch(SQLException se) {
                se.printStackTrace();
            }finally {
                this.closeStatement();
                this.closeConnection();
            }
            return operate;
        }
        /**
         * @deprecated:创建Connection
         * @return Connection
         * @throws Exception
         */
        public Connection createConnection()throws Exception {
            try {
                //加载JDBC驱动
                Class.forName(driverName);
                //创建数据库连接
                conn = DriverManager.getConnection(url, username, password);
            }catch(ClassNotFoundException cnf) {
                System.out.println("Driver not found:" + cnf);
            }catch(SQLException sqle) {
                System.out.println("Can't connection db:" + sqle);
            }catch(Exception e) {
                System.out.println("Failed to load JDBC/ODBC driver.");
            }
            return conn;
        }
        /**
         * @deprecated:获得Statement对象
         * @return Statement
         * @throws Exception
         */
        public Statement creatStatement()throws Exception{
            Statement stmt = null;
            try {
                stmt = this.createConnection().createStatement();
            }catch(SQLException se) {
                se.printStackTrace();
            }
            return stmt;
        }
        /**
         * @deprecated:关闭Connection
         * @throws SQLException
         */
        public void closeConnection() throws SQLException {
            if(null != conn) {
                conn.close();
            }
        }
        /**
         * @deprecated:关闭Statement
         * @throws SQLException
         */
        public void closeStatement() throws SQLException {
            if(null != stmt) {
                stmt.close();
            }
        }
    }
      

  7.   

    1.手动配置数据源(jdbc-odbc,这种方法现在不常用)        String url ="jdbc:odbc:jsp";
    Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
    Connection con=DriverManager.getConnection(url); 
    Statement st=con.createStatement();
    2.不用配置数据源
        Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
    String url="jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=sqldb"; 
    Connection con= DriverManager.getConnection(url ,"sa","sa"); 
    Statement st=con.createStatement();