日期:2014-05-16  浏览次数:20422 次

jdbc:java连接mysql数据库示例

直接上代码吧!

package com.insigma.zd.group4.liuchao.jdbc;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;

public class TestJDBC {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		//定义mysql数据库驱动类
		String driver = "com.mysql.jdbc.Driver";
		
		//连接字符串
		String url = "jdbc:mysql://localhost:3306/test";
		
		//用户名
		String username = "root";
		
		//密码
		String password = "1234";
		
		try {
			//加载数据库驱动程序
			Class.forName(driver);
			
			//获得连接
			Connection conn = DriverManager.getConnection(url, username, password);
			System.out.println(conn);
		
			
			} catch (ClassNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
	
	}
}