本人一直都不怎么习惯JSP中和数据库的连接问题,对这个方面很陌生,可能是在学习数据库的时候经常逃课,呵呵·请教高手详细的讲解下在eclips中要连接mysql要做那些具体的事情。比如要加载哪些驱动,注意哪些问题,谢谢大家了

解决方案 »

  1.   

    bai du  !!google !!
    多的是﹗﹗
      

  2.   

     把驱动包加到工程中。右击项目-〉属性-〉Java Build Path-〉库-〉添加外部库-〉JDBC驱动程序Jar包:mysql-connector-java-5.0.4-bin.jar 建立连接数据库的类。import java.sql.*;public class MySqlDataStore{    public static void main(String args[]){    try{       //注册驱动       Class.forName("com.mysql.jdbc.Driver");    }catch(ClassNotFoundException e){       e.printStackTrace();       }//MySql的驱动    //建立数据库操作对象实例    Connection con=null;//数据库联接对象    Statement stm=null;//数据库表达式    ResultSet rs=null;//结果集    try{       //建立数据库的连接       //三个参数分别为:连接到MySql数据库的JDBC URL,并设定了数据库中中字符编码方式(?号后面的内容),主要是为解决中文问题而添加的;
          //用户名;密码      
          con=DriverManager.getConnection("
                     jdbc:mysql://localhost:3306/auto53?useUnicode=true&charaterEncoding=UTF-8","username","password");       //下面就是你的SQL操作,       ……                     }    }catch(SQLException e){       e.printStackTrace();         }finally{       //关闭数据库联接,释放资源       try{           rs.close();       }catch(SQLException e){                }       
           try{           con.close();       }catch(SQLException e){                }    }}}
      

  3.   

    参考下吧 。。学习不要偷懒哦,加油
    http://www.pconline.com.cn/pcedu/empolder/wz/jsp/0506/652349.html
      

  4.   

    在ECLIPSE中的配置很简单,上面都说清楚了package com.data;
    import java.sql.*;
    public class dataaccess
    {
    private String odbcQuery;
    private Connection odbcconn;
    private Statement odbcstmt;
    private String myquerystr;
    private ResultSet sqlRst;
    private String realpath;public dataaccess(){
      odbcQuery=null;
      odbcconn=null;
      odbcstmt=null;
      myquerystr=null;
      sqlRst=null;
      realpath=null;
      //注册驱动
       try{
     Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
    }
    catch(ClassNotFoundException e){
    e.printStackTrace();
    }
    }
    public dataaccess(String mysql){
       this.myquerystr=mysql;
    }//设置SQL语句
    public void setSqlstr(String mysql){
       this.myquerystr=mysql;
    }//设置数据库的物理路径
    public void setrealpath(String rpath){
       this.realpath=rpath;
    }
    //初始化需要的数据
    public void initial(){
    try{ 
    String url="jdbc:odbc:Driver={MicroSoft Access Driver (*.mdb)};DBQ="+realpath;
    this.odbcconn = DriverManager.getConnection(url); 
    this.odbcstmt = odbcconn.createStatement(); 
    this.odbcQuery = this.myquerystr;
    }catch (SQLException e) 

    e.printStackTrace();

    }
    //执行添加删除修改
    public void exeUpdate(){
    try{
    odbcstmt.executeUpdate(this.odbcQuery);
    }catch(SQLException e){
    e.printStackTrace();
    }
    }//执行查询
    public ResultSet exeQuery(){
    try{
    return odbcstmt.executeQuery(this.odbcQuery);

    }
    catch(SQLException e)
    {
    return null;
    }
    }public void Closedb(){   
                      try   
                      {   
       if(sqlRst!=null) sqlRst.close();         
       if(odbcstmt!=null) odbcstmt.close();   
                            if(odbcconn!=null) odbcconn.close();   
                      }   
                      catch(Exception e)   
                      {   
                              system.out.println(e.tostring());   
                      }   
    }   
    }
    可以自己先写一个访问数据库的JAVABEAN,在项目中用就行.
      

  5.   

    嗯嗯,google google就能出来!