Home > Database > Mysql Tutorial > 关于Eclispse连接Mysql的Jdbc_MySQL

关于Eclispse连接Mysql的Jdbc_MySQL

WBOY
Release: 2016-06-01 13:14:04
Original
1033 people have browsed it

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();
}

 

 

 

  

 

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template