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服务
运行程序测试即可。
分享到:
2009-11-09 13:17
浏览 2139
评论