jdbc连接mysql的问题

我已经有jdbc的驱动了,jar文件为mysql-connector-java-5.0.3-bin.jar 用的IDE是eclipse,我该怎样处理才能连接上mysql数据库

先要将jar文件弄到项目下面啊
方式如下:右击项目,选择build path—>
选择Add External Archives...再选择你要导入的jar文件就可以了。
如果没有就选择Configure Build Path...
然后再选择Libraries 点击Add External JARs...
再选择你要导入的jar文件就可以了。
温馨提示:答案为网友推荐,仅供参考
第1个回答  2008-05-22
package bysj;
/**
*桥连接数据库,导入包。
*/
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.sql.SQLException;
class DB
{
Connection con;
ResultSet rs;
Statement stmt;
public Connection huodelianjie()
{
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");//------------------------连接数据库的驱动字符串。
con=DriverManager.getConnection("jdbc:odbc:mysql","sa","");
}
catch(SQLException e1)
{
System.out.println(e1+"\n数据库链接不正确!");
}
catch(Exception e2)
{
System.out.println(e2+"\n发生错误!");
}
return con;
}
//查询操作
public ResultSet getResult(String sql)
{
try
{
stmt=this.huodelianjie().createStatement(); //----------------------获得Statement对象
rs=stmt.executeQuery(sql);

}
catch(SQLException e3)
{
System.out.println(e3+"\n数据库链接不正确!");
}
return rs;
}
//查询以外的操作
public void excuteSql(String sql)
{
try
{
stmt = this.huodelianjie().createStatement();
stmt.execute(sql);
} catch (SQLException e) {
System.out.println("Excute SQL is Error.......");
System.out.println(e);
}
}
public void close()
{
try
{
if(!con.isClosed())
{
rs.close();
con.close();
stmt.close();
}
}
catch(SQLException e)
{
System.out.println(e+"关闭连接出错!");
}
}
}