GJS
少年,奋起吧。

1.在Eclipse中新建Java工程

2.引入JDBC库(在bulid path 的extenrnal里)

3.

1)导入sql包(import java.sql.*)

2)加载(注册)mysql jdbc驱动程序

 Class.forName("com.mysql.jdbc.Driver");

3)连接数据库(url)

Connection connect=DriverManager.getConnection(
"jdbc:mysql://localhost:3306/test","root","123");

Connection connect=DriverManager.getConnection(
"jdbc:mysql://localhost:3306/test");

 

ok。。

 

层次结构:

   应用层《---Jdbc接口层《---jdbc驱动层《---数据库

jdbc接口层:为访问不同数据库提供统一接口     

jdbc驱动层:由特定的数据库厂家实现数据库的真正交互

 

JDBC URL问题:

    Jdbc驱动程序一旦注册完毕,就可以建立数据库连接。但是由于很多驱动程序都被注册过,所以需要DriverManager选择正确的驱动程序。

因此Jdbc都有一个专门的jdbc url作为自我标识,用于选择。

 

如下:

try
{

Class.forName("com.mysql.jdbc.Driver");
System.out.println("Suceess loading mysql driver");
}
catch(Exception e)
{
System.out.print("EEOR loading mysql driver");
e.printStackTrace();
}
try
{
Connection connect=DriverManager.getConnection(
"jdbc:mysql://localhost:3306/test","root","123");
System.out.println("sucess connect server");
Statement stmt=connect.createStatement();
ResultSet rs=stmt.executeQuery("select * from user");
while(rs.next())
{
System.out.println(rs.getString("name"));
}
}
catch(Exception e)
{
System.out.print("get data error!");
e.printStackTrace();
}

 

 

 

  

 

posted on 2014-05-16 13:05  GJS Blog  阅读(702)  评论(0编辑  收藏  举报