jdbc简单示例

来源:互联网 发布:知乎 理科生与禅师 编辑:程序博客网 时间:2024/05/18 03:02
这里写代码片/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */package jdbc;import java.sql.*;/** * * @author 27720 */public class connect {    //连接数据库所需用户民和密码    static final String user = "root" ;    static final String pass = "123456" ;    // JDBC driver name and database URL    //URL:MySQL的JDBC URL编写方式:jdbc:mysql://主机名称:连接端口/数据库的名称?参数=值   static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";     static final String DB_URL = "jdbc:mysql://localhost:3306/ly?"           + "useUnicode=true&characterEncoding=UTF8";   public static void main(String[] args) {        Connection conn = null;        Statement stmt = null;        try{         //STEP 2: 注册jdbc驱动 JDBC driver            Class.forName(JDBC_DRIVER);            //STEP 3:建立一个物理链接            System.out.println("Connecting to database...");            conn = DriverManager.getConnection(DB_URL,user,pass);            //STEP 4:创建Statement对象构建和提交sql语句到数据库            System.out.println("Creating statement...");            stmt = conn.createStatement();            String sql;            sql = "SELECT id, name FROM user";            //执行sql语句,并获取返回结果至ResultSet            ResultSet rs = stmt.executeQuery(sql);            //遍历返回的结果            while(rs.next()){            //Retrieve by column name            int id  = rs.getInt("id");            String name = rs.getString("name")            //输出返回结果            System.out.print("ID: " + id);            System.out.println(", name: " + name);            }             //STEP 6: 关闭资源,                rs.close();                stmt.close();                conn.close();        }catch(Exception e){         e.printStackTrace();        }        finally{//如果没有关闭,再次关闭                try{              if(stmt!=null)                 stmt.close();                     }catch(SQLException se2){                         }// nothing we can do                try{                if(conn!=null)                 conn.close();                    }catch(SQLException se){                 se.printStackTrace();                }//end finally try          }//end try        System.out.println("There are no thing wrong!");     }//end main   }

步骤:
导入包:
注册驱动方式:
第一种: Class.forName()
第二种: DriverManager.registerDriver()
建立物理连接:
Connection conn = null;
conn=DriverManager.getConnection(“jdbc:mysql://hostname:port/db_name”,”db_username”,”db_password”);
执行sql语句:
stmt = conn.createStatement();
String sql = “CREATE DATABASE jdbc_db”;
stmt.executeUpdate(sql);//执行DML语句,返回一个整数,代表被sql语句影响的记录条数
或者:
//executeQuery()执行select语句返回结果集
String sql = “select * from table_name”;

ResultSet rs = stmt.executeQuery(sql);

再或者:execute()可执行任何SQL语句返回一个boolean值,若为true则执行后第一个结果是ResultSet
对结果进行遍历:
while(rs.next()){
int id = rs.getInt(“id”);
String name = rs.getString(“name”)
}

清理环境:
rs.close();
stmt.close();
conn.close();
java7 后,Connection,Statement,ResultSet等接口都继承了AutoClosedable接口,对于try语句中的资源都可以由try语句来关闭。

原创粉丝点击