JDBC连接MySQL数据库关键的四个步骤(初学必看)

in 网络编程

1、查找驱动程序

  MySQL目前提供的Java驱动程序为Connection/J,可以从MySQL官方网站下载,并找到mysql-connector-java-3.0.15-ga-bin.jar文件,此驱动程序为纯Java驱动程序,JDBC链接MySQL不需做其他配置。

  2、动态指定classpath

  如果需要执行时动态指定classpath,就在执行时采用-cp方式。否则将上面的.jar文件加入到classpath环境变量中。

  3、加载JDBC 连接MySQL

 

  1. try{  
  2.  Class.forName(com.mysql.jdbc.Driver);  
  3.  System.out.println(Success loading Mysql Driver!);  
  4. }catch(Exception e)  
  5. {  
  6.  System.out.println(Error loading Mysql Driver!);  
  7.  e.printStackTrace();  

  4、设置JDBC连接MySQL的URL

 

jdbc:mysql://localhost/databasename[?pa=va][&pa=va] 
0 Comments

Leave a Reply

You must be logged in to post a comment.

Using Gravatars in the comments - get your own and be recognized!

XHTML: These are some of the tags you can use: <a href=""> <b> <blockquote> <code> <em> <i> <strike> <strong>