JAVA之数据库(二)

来源:互联网 发布:网络大学招生 编辑:程序博客网 时间:2024/06/06 10:43
昨天小狼简单介绍了数据库的初步验证连接,连接成功后因为有事锁键盘时无意按到了键盘上的某个键,也没太在意,等回来的时候再连接就显示如下错误:

java.sql.SQLException: Access denied for user 'root'@'localhost' (using password: YES)
折腾半天最后才发现密码处有了小小的改动,这里提醒大家一定要细心,不要犯这种错误。
好的,接下来我们说下如何简单的用JAVA语言在数据库中进行修改。

package com.amaker.test;import java.sql.Connection;import java.sql.ResultSet;import java.sql.SQLException;import java.sql.Statement;import com.maker.util.DBUtil;public class Test {    public static void main(String[] args){        Test test = new Test();        test.list();    }    public void list(){        DBUtil util = new DBUtil();        Connection conn = util.getConnection();        String sql = "Select id,username,password from UserTbl ";        try{            Statement stmt = conn.createStatement();            ResultSet rs = stmt.executeQuery(sql);            while(rs.next()){                int id = rs.getInt("id");                String username = rs.getString(2);                String password = rs.getString(3);                System.out.println(id+":"+username+":"+password);                }            }catch(SQLException e){                e.printStackTrace();            }finally{                util.closeConnection(conn);            }        }    }

里面涉及到SQL语言,大家可以了解下。
从数据库中获取到信息

在(一)中只说了一种数据库连接方式,下面在详细介绍下连接方式:

package com.maker.util;import java.sql.Connection;import java.sql.DriverManager;import java.util.Properties;public class DBUtil {    public static void main(String[] args){        DBUtil util = new DBUtil();        //Connection conn = util.getConnection();        Connection conn = util.openConnection();        System.out.println(conn);    }    //**number one**    public Connection openConnection(){        Properties prop = new Properties();        String driver   = null;        String url      = null;        String username = null;        String password = null;        try {            prop.load(this.getClass().getClassLoader().getResourceAsStream("DBConfig.properties"));            driver   = prop.getProperty("driver");            url      = prop.getProperty("url");            username = prop.getProperty("username");            password = prop.getProperty("password");            Class.forName("com.mysql.jdbc.Driver");            return DriverManager.getConnection("jdbc:mysql://localhost:3306/jdbc_db","root","521babylgb,.");        } catch (Exception e) {            // TODO: handle exception        }        return null;    }    //**number two**    public Connection getConnection(){        try {            Class.forName("com.mysql.jdbc.Driver");            return DriverManager.getConnection("jdbc:mysql://localhost:3306/jdbc_db","root","521babylgb,.");        } catch (Exception e) {            // TODO: handle exception            e.printStackTrace();        }        return null;    }    //**number three**    public Connection getConnection(String url,String driver,String username,String password){        try {            Class.forName(driver);            return DriverManager.getConnection(url,username,password);        } catch (Exception e) {            // TODO: handle exception        }        return null;    }}

这里推荐第一种方式,因为数据库的数据经常被修改,所以写入.properties配置文件也是一种好习惯
这里写图片描述

See U~~

原创粉丝点击