JavaWeb基础(一)——JDBC连接数据库

JDBC连接数据库

1.JDBC是Java数据库连接技术的简称(Java DataBase Connectivity),提供连接各种常用数据库的能力

2.JDBC API主要功能:与数据库建立连接、执行SQL 语句、处理结果

DriverManager :依据数据库的不同,管理JDBC驱动 Connection :负责连接数据库并担任传送数据的任务 Statement :由 Connection 产生、负责执行SQL语句 ResultSet:负责保存Statement执行后所产生的查询结果

3.Java.sql中的主要接口

4.使用URL找到数据库

jdbc:mysql://localhost:3306/数据库名?user=root&password=密码

/**
	 * 4.得到连接
	 * @return
	 */
	public Connection getConn(){
          
   
		try {
          
   
			conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/"+
					"news?user=root&password=123456");
			System.out.println("连接成功!");
		} catch (SQLException e) {
          
   
			// TODO Auto-generated catch block
			System.out.println("连接失败:"+e.getMessage());
			e.printStackTrace();
		}
		return conn;
	}

5.JDBC工作模块

6.JDBC驱动

7.JDBC连接数据库步骤

./**
 * 文档注释  JDBC入门
 * JDBC 连接数据库的步骤
 * 1.导入jar包
 * 2.导包
 * 3.注册驱动
 * 4.得到连接
 * 5.得到Statement
 * 6.处理结果集
 * 7.不用的时候关闭连接
 * @author 
 * 
 */

8.完整代码

package com.hk.db;

import com.mysql.jdbc.Driver; //单行注释
import java.sql.*;
/**
 * 文档注释  JDBC入门
 * JDBC 连接数据库的步骤
 * 1.导入jar包
 * 2.导包
 * 3.注册驱动
 * 4.得到连接
 * 5.得到Statement
 * 6.处理结果集
 * 7.不用的时候关闭连接
 * @author 
 * 
 */

public class DBConnect {
          
   
	Connection  conn;   //用来得到连接
	Statement sts;      //用来执行sql语句
	ResultSet rs;       //用来处理返回的结果
	
	/*3.注册驱动*/
	static{
          
   
		try {
          
   
			Class.forName("com.mysql.jdbc.Driver");
			System.out.println("注册成功!");
		} catch (ClassNotFoundException e) {
          
   
			// TODO Auto-generated catch block
			System.out.println("注册失败!"+e.getMessage());
			e.printStackTrace();
		}
	}
	
	/**
	 * 4.得到连接
	 * @return
	 */
	public Connection getConn(){
          
   
		try {
          
   
			conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/"+
					"news?user=root&password=123456");
			System.out.println("连接成功!");
		} catch (SQLException e) {
          
   
			// TODO Auto-generated catch block
			System.out.println("连接失败:"+e.getMessage());
			e.printStackTrace();
		}
		return conn;
	}
	
	/**
	 * select方法返回结果集
	 * @param sql
	 * @return
	 */
	public ResultSet select(String sql){
          
   
		try {
          
   
			sts=conn.createStatement();
			rs=sts.executeQuery(sql);
		} catch (SQLException e) {
          
   
			// TODO Auto-generated catch block
			System.out.println("发生异常:"+e.getMessage());
			e.printStackTrace();
		}
		return rs;
	}
	
	/*7.不用的时候关闭连接*/
	public void close(){
          
   
		if(rs!=null)
			try {
          
   
				rs.close();
				if(sts!=null)
					sts.close();
				if(conn!=null)
					conn.close();
				
			} catch (SQLException e) {
          
   
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
	}
	
	/**
	 * 
	 * @param args
	 */
	public static void main(String[] args) {
          
   
		// TODO Auto-generated method stub
		DBConnect dbc=new DBConnect();
		Connection conn=dbc.getConn();
		
		try {
          
   
			//5.生成Statement
			dbc.sts=conn.createStatement();
			String sql="select * from users ";
			//6.处理结果集
			dbc.rs=dbc.sts.executeQuery(sql);
			//处理结果集
			while(dbc.rs.next()){
          
   
				System.out.println(dbc.rs.getInt(1)+"	"+dbc.rs.getString(2)+"	"
				+dbc.rs.getString(3));	
			}
			
		} catch (SQLException e) {
          
   
			// TODO Auto-generated catch block
			System.out.println("发生异常:"+e.getMessage());
			e.printStackTrace();
		}finally{
          
   
			dbc.close();	
		}
	}

}

9.运行结果

经验分享 程序员 微信小程序 职场和发展