JDBC编程六部曲

 

package com.bjpowernode.jdbc;

import java.sql.Connection;
import java.sql.Driver;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class JDBCTest01 {

    public static void main(String[] args) {
        Connection conn=null;
        Statement stmt=null;
        ResultSet rs=null;
        try {
            //1.注册驱动
            Driver driver=new com.mysql.jdbc.Driver();
            DriverManager.registerDriver(driver);
            //2获取数据库连接
            String Url="jdbc:mysql://172.16.1.60:3306/gagamatch";
            String user="shejiao";
            String password="@shejiao20150604544090";
            conn=DriverManager.getConnection(Url,user,password);
            //3获取数据库操作对象
            stmt=conn.createStatement();
            //4执行sql语句
            String sql="select * from gagamatch.member limit 1";
            rs=stmt.executeQuery(sql);
//处理查询结果集
while(rs.next()) { String id=rs.getString("Email"); String name=rs.getString("name"); System.out.println(id+" "+ name); } } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); }finally{ //6关闭资源 if(rs!=null) { try{rs.close();}catch (Exception ex) {ex.printStackTrace();} } //关闭资源 if(stmt!=null) { try{stmt.close();}catch (Exception ex) {ex.printStackTrace();} } //关闭资源 if(conn!=null) { try{conn.close();}catch (Exception ex) {ex.printStackTrace();} } } } }

 

 

 

 

posted on 2017-04-19 17:39  算顺网  阅读(688)  评论(0编辑  收藏  举报

导航