网站开发工作 岗位怎么称呼,网站的运营模式,小程序公众号网站开发,丹阳网站建设Java程序访问数据库#xff1a;
1、获取数据库厂商提供的驱动#xff08;jdbc接口的实现类#xff09;
如ojdbc14.jar——Oracle数据库驱动jar包
mysql-connector-java-5.1.8-bin.jar——MySQL数据库驱动jar包
自己去网上下载就行。
2、使用JDBC的API访问数据库
连接、…Java程序访问数据库
1、获取数据库厂商提供的驱动jdbc接口的实现类
如ojdbc14.jar——Oracle数据库驱动jar包
mysql-connector-java-5.1.8-bin.jar——MySQL数据库驱动jar包
自己去网上下载就行。
2、使用JDBC的API访问数据库
连接、SQL语句执行、结果
java.sql.Driver各个数据库厂商需要实现该接口驱动的标记
java.sql.Connection封装和数据库的连接
java.sql.Statement封装需要执行的SQL语句
java.sql.ResultSet封装查询的结果集
3、JDBC编程步骤
step1——加载驱动
step2——获取连接对象
step3——执行SQL语句
step4——处理结果集
step5——关闭资源
4、下面给出连接数据库的工具类自己写的连接MySql数据库如要连接Oeacle可修改对应参数
package com.day03;import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;//加入Java开发交流君样756584822一起吹水聊天
public class ConnectionUtils {// 线程单例private static ThreadLocalConnection tl new ThreadLocalConnection();private static String url;private static String driver;private static String username;private static String password;//加入Java开发交流君样756584822一起吹水聊天static {Properties props new Properties();try {// 从属性文件中读取数据库配置信息以加载类的方式加载配置文件props.load(ConnectionUtils.class.getClassLoader().getResourceAsStream(com/day03/db_mysql.properties));} catch (IOException e) {}if (props ! null) {url props.getProperty(url);driver props.getProperty(driver);username props.getProperty(username);password props.getProperty(password);// 装载并注册数据库驱动try {Class.forName(driver);} catch (ClassNotFoundException e) {e.printStackTrace();}}}//加入Java开发交流君样756584822一起吹水聊天public static Connection getConnection() throws SQLException {Connection con tl.get();if (con null) {con DriverManager.getConnection(url, username, password);tl.set(con);}return con;}public static void closeConnection() {Connection con tl.get();try {if (con ! null) {con.close();tl.set(null);}} catch (SQLException e) {e.printStackTrace();}}public static void closeStatement(Statement stmt) {try {if (stmt ! null) {stmt.close();}} catch (SQLException e) {e.printStackTrace();}}public static void closeResultSet(ResultSet rs) {try {if (rs ! null) {rs.close();}} catch (SQLException e) {e.printStackTrace();}}public static void closeAll(Statement stmt, ResultSet rs){closeConnection();closeStatement(stmt);closeResultSet(rs);}public static void main(String[] args) throws Exception{System.out.println(ConnectionUtils.getConnection());}}
5、配置参数文件db_mysql.properties
drivercom.mysql.jdbc.Driver
urljdbc:mysql://localhost:3306/test
usernameroot
passwordroot6、Dao模式操作数据库下面是代码示例
1Emp.java
//实体类
public class Emp {private int id;private String name;private double salary;public int getId() {return id;}Overridepublic String toString() {return Emp [id id , name name , salary salary ];}//加入Java开发交流君样756584822一起吹水聊天public void setId(int id) {this.id id;}public String getName() {return name;}public void setName(String name) {this.name name;}public double getSalary() {return salary;}public void setSalary(double salary) {this.salary salary;}public Emp(int id, String name, double salary) {super();this.id id;this.name name;this.salary salary;}public Emp() {super();}public Emp(String name, double salary) {super();this.name name;this.salary salary;}
}2)Dao接口类
import java.util.List;public interface EmpDao {ListEmp findAllEmp() throws Exception;
}3工厂类
public class EmpDaoFactory {// 读取文件中实现类的类名通过反射实例化public static EmpDao getEmpDao(){return new EmpDaoMySQL();}
}4Dao接口实现类
public class EmpDaoMySQL implements EmpDao{public static final String FIND_ALL_EMP select * from t_emp;//查询语句public ListEmp findAllEmp() throws Exception{ListEmp empList new ArrayListEmp();Connection conn ConnectionUtils.getConnection();PreparedStatement stmt conn.prepareStatement(FIND_ALL_EMP);ResultSet rs stmt.executeQuery();while(rs.next()){int id rs.getInt(1);String name rs.getString(2);double salary rs.getDouble(3);Emp emp new Emp(id, name, salary);empList.add(emp);}//加入Java开发交流君样756584822一起吹水聊天ConnectionUtils.closeAll(stmt, rs);return empList;}
}5测试类
public class EmpBiz {public static void main(String[] args) throws Exception{EmpDao dao EmpDaoFactory.getEmpDao();ListEmp empList dao.findAllEmp();for(Emp e : empList){System.out.println(e);}}
}