1、 下载jdbc driver : Connector-J
2、将jdbc driver 的jar文件加入到classpath环境变量中(classpath中需要增加.,即当前目录)
3、写java程序:MysqlJdbc.java
- import java.sql.*;
- public class MysqlJdbc
- {
- public static void main(String args[])
- {
- try
- {
- Class.forName("org.gjt.mm.mysql.Driver");
- System.out.println("Success loading Mysql Driver!");
- }
- catch(Exception e)
- {
- System.out.println("Error loading Mysql Driver!");
- e.printStackTrace();
- }
- try
- {
- String conStr = "jdbc:mysql://localhost:3306/testdb";
- Connection connect = DriverManager.getConnection(conStr, "testuser", "testpass");
- System.out.println("Success connect to server!");
- }
- catch(Exception e)
- {
- System.out.println("Error connecting to server!");
- e.printStackTrace();
- }
- }
- }
4、用javac MysqlJdbc.java编译
5、用java MysqlJdbc 运行