Java使用JDBC连接数据库(一)

来源:互联网 发布:nginx server 配置 编辑:程序博客网 时间:2024/05/22 06:04

重点内容
(一)使用JDBC连接数据库的步骤如下:

  1. 明确数据库,选择适合你的数据库Driver。
  2. 下载相应的驱动程序Jar包,并添加到 project libraries。
  3. 使用 DriverManager类和JDBC URL连接数据库。

(二)连接Mysql数据库的所需的4个参数值

驱动器jar包:mysql-connector-java-5.1.6.jar

    private static String driver="com.mysql.jdbc.Driver";    private static String url="jdbc:mysql://localhost/数据库名";    private static String user="";    private static String password="";

(三)连接数据库代码演示

package com.connectiondatabase;import java.sql.*;public class GrudOperation {    //1.设置连接数据库的4个参数值    private static String driver="com.mysql.jdbc.Driver";    private static String url="jdbc:mysql://localhost/test";    private static String user="root";    private static String password="root";    @SuppressWarnings("resource")    public static void main(String[] args) {        //2.创建连接数据库对象、操作数据库对象和结果集对象        Connection conn=null;        PreparedStatement ps=null;        ResultSet rs=null;        //3.写出GRUD语句        String selectsql = "select*from student where id=?";        String insertsql = "insert into student(NAME,AGE)values(?,?)";        String updatesql = "update student set NAME=?,AGE=? where ID=?";        String deletesql = "delete from student where ID=?";        try {            //4.加载驱动器            Class.forName(driver);            conn = DriverManager.getConnection(url, user, password);             ps=conn.prepareStatement(selectsql);             ps.setInt(1,2);             rs=ps.executeQuery();             //5.查询数据库中的单个对象数据             while(rs.next()){             int id=rs.getInt(1);             String name=rs.getString(2);             int age=rs.getInt(3);             System.out.println("编号="+id);             System.out.println("姓名="+name);             System.out.println("年龄="+age);             }             //6.向数据库插入一条记录             ps=conn.prepareStatement(insertsql);             ps.setString(1,"凯耐");             ps.setInt(2,23);             int count1=ps.executeUpdate();             System.out.println("向student表中添加"+count1+"记录");             //7.修改一条数据ID为最后一个数             ps = conn.prepareStatement(updatesql);             ps.setInt(3, 2);             ps.setString(1, "888");             ps.setInt(2, 999);             int count2 = ps.executeUpdate();             System.out.println("向student表中修改" + count2 + "记录");             //8.删除一条记录             ps=conn.prepareStatement(deletesql);             ps.setInt(1,13);             int count3 = ps.executeUpdate();             System.out.println("向student表中删除"+count3+"记录");        } catch (ClassNotFoundException e) {            System.out.println("没有找到该类,请导入相应jar包");            e.printStackTrace();        } catch (SQLException e) {            System.out.println("sql语句异常");            e.printStackTrace();        } finally {            try {                if (conn != null)                    conn.close();                if (rs != null)                    rs.close();                if (ps != null)                    ps.close();            } catch (SQLException e) {                // TODO Auto-generated catch block                e.printStackTrace();            }        }    }}

(四)关闭创建的各种对象

当执行完对数据库的操作 或退出应用程序前,需要将数据库访问过程中的各个对象按顺序关闭。关闭顺序:关闭结果集对象—关闭PreparedStatement对象—关闭Connection对象。

原创粉丝点击