java使用odbc连接mysql_如何用Java通过ODBC连接MySQL

public static void main(String[] args)

{

Connection connection = null;

Statement statement;

ResultSet resultSet;

// 加载驱动程序以连接数据库

try

{

Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");//这个驱动不能是其他的..

connection = DriverManager.getConnection("jdbc:odbc:user","root","123"); //user是data Source ,root是用

//户名,123是进入mysql的密码

System.out.println("open easy");

String query = "Select * from userrole";

statement = connection.createStatement();

resultSet = statement.executeQuery( query );

connection.close();

System.out.println("close easy");

}

// 捕获加载驱动程序异常

catch ( ClassNotFoundException cnfex )

{

System.err.println(

"装载 JDBC/ODBC 驱动程序失败。" );

cnfex.printStackTrace();

System.exit( 1 ); // terminate program

}

// 捕获连接数据库异常

catch ( SQLException sqlex )

{

System.err.println( "无法连接数据库" );

sqlex.printStackTrace();

System.exit( 1 ); // terminate program

}

catch (Exception e)

{

System.out.println(e.toString());

}

在运行中输入:net start mysql以启动mysql服务

运行程序测试即可。

分享到:

18e900b8666ce6f233d25ec02f95ee59.png

72dd548719f0ace4d5f9bca64e1d7715.png

2009-11-09 13:17

浏览 2139

评论

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值