java中Mysql数据库连接及实例

来源:互联网 发布:sqlserver 建用户语录 编辑:程序博客网 时间:2024/05/29 18:38

目标:通过在eclipse中加载mysql驱动,将users表中的数据提取出来。
1.创建数据库
在mysql数据库中test目录下创建users数据库。存取字段:id username password。
2.构建java项目
2.1 在构建的java项目中src下面创建文件db.properties,里面设置内容为:
name=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/test
username=root
password=password
2.2 读取上面文件中的配置信息

public  class FileRead {    public static  String[]  read() throws FileNotFoundException{        Properties properties = new  Properties();        BufferedInputStream read = new BufferedInputStream(new FileInputStream("src/db.properties"));        String [] messages = new String[4];        try {            properties.load(read);        } catch (IOException e) {        }        messages[0] = properties.getProperty("name");        messages[1] = properties.getProperty("url");        messages[2] = properties.getProperty("username");        messages[3] = properties.getProperty("password");        return messages;    }}

2.3 创建连接
这里主要有三个对象:Connection Statement ResultSet
* Conncetion:与目标数据库建立连接
* Statement:向连接的数据库发送指令,并返回操作
* ResultSet:装载数据库指令执行结果
链接数据库的基本操作分六步:
* 1.加载驱动
* 2.获取连接
* 3.获取向数据发sql语句的statement对象
* 4.向数据库发送sql请求,获取数据库返回结果集
* 5.从结果集中获取数据
* 6.释放资源
2.4 数据库连接和释放
创建FilereadParaGet文件,在文件中创建两个函数getconnect()和release(),分别用来连接和释放数据库连接。
数据库连接函数:

public static Connection getconnect(){    Connection connection = null;        try {            String mess[] = FileRead.read();            connection = DriverManager.getConnection(mess[1], mess[2], mess[3]);        } catch (FileNotFoundException e) {            System.out.println("没有读取到配置文件!");        } catch (SQLException e) {            System.out.println("没有连接到数据库!");        }        return connection;      }

数据库释放:

public static void release(Connection con,Statement statement,ResultSet rs){        if (rs!=null) {            try {                rs.close();            } catch (Exception e) {                e.printStackTrace();            }            rs=null;        }        if (statement!=null) {            try {                statement.close();            } catch (Exception e) {                e.printStackTrace();            }            statement=null;        }        if (con!=null) {            try {                con.close();            } catch (Exception e) {                e.printStackTrace();            }            con=null;        }    }

2.5 实例操作
创建测试实例,实现从数据库获取数据:

public class TestMsql {     @Test    public void test() throws SQLException {        Connection con=FilereadParaGet.getconnect();        Statement statement = (Statement) con.createStatement();         String sql="select * from users;";        ResultSet rs = statement.executeQuery(sql);        while (rs.next()) {            System.out.println("id:"+rs.getInt("id")+"   "+"username:"+rs.getString("username")+ " "+"password:"+rs.getString("password"));        }        FilereadParaGet.release(con, statement, rs);    }}

执行结果:
id:1 username:zs password:123346
id:2 username:lisi password:454545
id:3 username:wangwu password:12455632
id:4 username:eee password:123
ok!

原创粉丝点击