日期:2014-05-16  浏览次数:20817 次

连接mysql数据库问题
如何将myeclipse连接到数据库啊求各位帮帮忙!本人是初学者!


------解决方案--------------------
Java code
package Javabeans;
import java.io.*;
import javax.servlet.http.*;
import java.sql.*;
public class DBCon implements HttpSessionBindingListener
{
    //锟斤拷锟斤拷锟斤拷锟斤拷Session时锟皆讹拷执锟叫此猴拷锟斤拷
    public void valueBound(HttpSessionBindingEvent event){}
    //Session锟斤拷锟截憋拷时执锟叫此猴拷锟斤拷
    public void valueUnbound(HttpSessionBindingEvent event) {}
    private Connection con = null;
    public DBCon()
    {
        //锟斤拷b锟斤拷菘锟絫锟斤拷
        BulidConnection();
    }
    //锟斤拷b锟斤拷菘锟絫锟接的凤拷锟斤拷
    private void BulidConnection()
    {
        try 
        {
            //锟斤拷锟斤拷锟斤拷菘锟斤拷锟斤拷锟斤拷
          //  Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
            Class.forName("com.mysql.jdbc.Driver");
            //锟斤拷锟斤拷菘锟絣锟斤拷
            con = DriverManager.getConnection("jdbc:mysql://localhost:3306/cangku","root","long");
        } catch(Exception ex)
        {
            System.out.println(ex.toString());
        }
    }
    public Connection getConnection()
    {
        //锟斤拷锟絚on为null锟斤拷锟斤拷锟斤拷锟铰斤拷bl锟斤拷
        if(con == null)
            BulidConnection();
        return con;
    }
    public void close()
    {
        try
        {
            //锟截憋拷con
            con.close();
            con = null;
        } catch(SQLException ex){}
    }
    
}