设为首页 加入收藏

TOP

Java JDBC数据库连接规范介绍(JavaDatabaseConnectivity)(二)
2018-03-24 09:50:30 】 浏览:392
Tags:Java JDBC 数据库 连接 规范 介绍 JavaDatabaseConnectivity
tId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public String getEmail() { return email; } public void setEmail(String email) { this.email = email; } public Date getBirthday() { return birthday; } public void setBirthday(Date birthday) { this.birthday = birthday; } @Override public String toString() { return "User[id=" + id + ", name=" + name + ", password=" + password + ", email=" + email + ", birthday=" + birthday + "]"; } } public class DemoException { public static void main(String[] args) { Connection connection = null; Statement statement = null; ResultSet resultSet = null; try{ //这里会有加载驱动失败异常 Class.forName("com.mysql.jdbc.Driver"); //这里是连接数据库异常 String url = "jdbc:mysql://localhos:3306/myjdbc"; connection = DriverManager.getConnection(url,"root","123456"); statement = connection.createStatement(); String sql = "select * from users"; resultSet = statement.executeQuery(sql); //创建一个集合,用于存储每条记录的当做一个对象的集合 ArrayList list = new ArrayList<>(); while(resultSet.next()){ //这里我们创建User对象,并通过结果集对象调用获取方法对应的给对象的变量赋值 User user = new User(); user.setId(resultSet.getInt("id")); user.setName(resultSet.getString("name")); user.setPassword(resultSet.getString("password")); user.setEmail(resultSet.getString("email")); user.setBirthday(resultSet.getDate("birthday")); //赋值完毕,添加到集合中 list.add(user); } //遍历打印集合中的对象数据 for(User user :list){ System.out.println(user); } } catch (SQLException e) { e.printStackTrace(); throw new RuntimeException("获取连接失败"); } catch (ClassNotFoundException e) { e.printStackTrace(); throw new RuntimeException("驱动加载失败"); }finally { } } }
//这里将,获取数据库的连接方法,连接数据库异常,建立工具类便于调用
//首先,我们要先创建一个配置文件
//1.在src下创建一个文本文件,这里起名一般保存的后缀名是.properties,dbinfo.properties(文本名)
//2.数据是:
driverClass=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/myjdbc
user=root
password=123456


public class JDBCUtil {
 private static String driverClass;
 private static String url;
 private static String user;
 private static String password;
 //第一种,先使用集合读取文件
 Properties properties = new Properties();
 try{
     FileInputStream fis = new FileInputStream("src/dbinfo.propeerties");//注:这里后缀要写全
     properties.load(fis);
    //根据键读取对应的值
    driverClass = properties,getProperty("driverClass");
    url = properties.getProperty("url");
    user = properties.getProperty("user");
    password = properties.getProperty("password");
    }catech (Exception e) {
            e.printStackTrace();
        }
//第二种,使用系统类来读取配置文件
//我们在静态代码块里编写读取配置文件
ResourceBundle rb = ResourceBundle.getBundle("dbinfo");//注:这里的后缀不写
//获取文件中的数据
driverClass = rb.getString("driverClass");
url = rb.getString("url
首页 上一页 1 2 3 下一页 尾页 2/3/3
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇JDBC加载驱动步骤 下一篇centos7.4+udev+12.2.0.1+gi+asm+..

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目