JDBC连接MYSQL的示例代码

这个代码是从mysql的jdbc的doc里摘下来的:

import java.sql.*;

public class TestMySqlConnection {
public static void main(String[] args){
try {
Class.forName(
"com.mysql.jdbc.Driver").newInstance();
}
catch (Exception ex) {
ex.printStackTrace();
}
Connection conn
= null;
try {
conn
=
DriverManager.getConnection(
"jdbc:mysql://localhost/test?" +
"user=monty&password=greatsqldb");

}
catch (SQLException ex) {
System.out.println(
"SQLException: " + ex.getMessage());
System.out.println(
"SQLState: " + ex.getSQLState());
System.out.println(
"VendorError: " + ex.getErrorCode());
}
}

}

 

posted @ 2011-07-19 10:31  chenatu  阅读(421)  评论(0编辑  收藏  举报