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

新手学java(3) - java连接mysql数据库

java连接mysql 数据库小例子

import?java.sql.*;

public?class?JDBCTest?{

????
public?static?void?main(String[]?args)?{
????????
//?1.?注册驱动
????????try?{
????????????Class.forName(
"com.mysql.jdbc.Driver");
????????}?
catch(ClassNotFoundException?ex)?{
????????????ex.printStackTrace();
????????}
????????
????????
//?声明变量,使用,而后关闭
????????Connection?conn?=?null;????????//数据库连接
????????Statement?stmt?=?null;?????????//数据库表达式
????????ResultSet?rs?=?null;?????????????//结果集
????????
????????
try?{
????????????
//2.?获取数据库的连接
????????????conn?=?DriverManager.getConnection
????????????????(
"jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=GBK","root","");
????????????
???