java程序模拟JDBC
建立一个接口代表jdbc
package JDBC;
/**
* @author whlie(true){learn}
* 规定的JDBC连接方法
*/
public interface Interface {
//连接
public void Connect();
//操作
public void crud();
//关闭
public void close();
}
编写Mysql类代表mysql
package JDBC;
/**
* @author whlie(true){learn}
* 模拟实现JDBC接口
*/
public class Mysql implements Interface{
@Override
public void Connect() {
System.out.println("连接成功");
}
@Override
public void crud() {
System.out.println("操作成功");
}
@Override
public void close() {
System.out.println("关闭连接");
}
}
编写test代表idea
package JDBC;
/**
* @author whlie(true){learn}
*/
public class Test {
public static void main(String[] args) {
Interface in=new Mysql();
in.Connect();
in.crud();
in.close();
}
}
实际的操作,在sql中创建表
CREATE TABLE actor(
id INT PRIMARY KEY AUTO_INCREMENT,
`name` VARCHAR(32) NOT NULL DEFAULT '',
sex VARCHAR(32) NOT NULL DEFAULT '女',
borndate DATETIME,
phone VARCHAR(12))
SELECT *FROM actor
在idea中对该表进行操作
package JDBC;
import com.mysql.jdbc.Driver;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;
/**
* @author whlie(true){learn}
*/
public class Jdbc01 {
public static void main(String[] args) throws SQLException {
//1.注册驱动
Driver driver = new Driver();
/**
* 2.建立连接
* jdbc:mysql规定好表示协议,通过jdbc的方式连接mysql
* localhost表示主机,也可以用IP地址
* 3306表示mysql监听的端口
* wzg_db02连接数据库的名称
*/
String url="jdbc:mysql://localhost:3306/wzg_db02";
//将用户名和密码放入Properties对象
Properties properties = new Properties();
//user和password是规定好的,后面的值根据情况写
properties.setProperty("user","root");
properties.setProperty("password","***");
Connection connect = driver.connect(url, properties);
//3.执行sql语句
String sql="insert into actor values(01,'周星驰','男','1999-09-09','001')";
//statement用于执行静态sql语句
Statement statement = connect.createStatement();
//i表示影响的行数,如果>0说明成功
int i = statement.executeUpdate(sql);
System.out.println(i>0?"成功":"失败");
//4.关闭连接
statement.close();
connect.close();
}
}
连接数据库的5种方式
方式1静态加载
Driver driver = new Driver();
String url="连接地址";
Properties properties = new Properties();
properties.setProperty("user","root");
properties.setProperty("password","***");
Connection connect = driver.connect(url, properties);
String sql="sql语句";
Statement statement = connect.createStatement();
int i = statement.executeUpdate(sql);
statement.close();
connect.close();
方式2动态加载,更加灵活减少依赖性
Class<?> aClass = Class.forName("com.mysql.jdbc.Driver");
Driver driver = (Driver)aClass.newInstance();
方式3使用DriverManager
Class<?> aClass = Class.forName("com.mysql.jdbc.Driver");
Driver driver = (Driver)aClass.newInstance();
String url="连接地址";
String user="root";
String password="***";
DriverManager.registerDriver(driver);
Connection connection = DriverManager.getConnection(url, user, password);
方式4 Class.forName自动完成注册驱动
//其实这一句也可以不写,因为jdk1.5后有自动注册功能
Class.forName("com.mysql.jdbc.Driver");
String url="jdbc:mysql://localhost:3306/wzg_db02";
String user="root";
String password="***";
Connection connection = DriverManager.getConnection(url, user, password);
方式5 配置文件+方式4
配置文件
url=jdbc:mysql://localhost:3306/wzg_db02;
user=root;
password=***;
driver=com.mysql.jdbc.Driver
Properties properties = new Properties();
properties.load(new FileInputStream("src\\mysql"));
String driver= properties.getProperty("driver");
String url=properties.getProperty("url");
String user=properties.getProperty("user");
String password=properties.getProperty("password");
Class.forName(driver);
Connection connection = DriverManager.getConnection(url, user, password);