连接 mysql步骤,数据库连接步骤

数据库连接步骤

1.注册驱动driver

2.建立连接connection

3.创建sql语句通常是创建statement或者statem的子类PreparedStatement

4.执行语句

5.处理执行结果(在非查询语句中,这一步可以省略)

6.释放相关资源

第一种连接数据库的方法

BaseDao包里的basedao类

importjava.sql.Connection;

importjava.sql.DriverManager;

importjava.sql.SQLException;

publicclassBaseDao {

//  private String dbDriver = "oracle.jdbc.driver.OracleDriver";

//  private String dbUrl = "jdbc:oracle:thin:@localhost:1521:orcl";

//  private String dbUser ="scott";

//  private String dbPass ="tiger";

privatestaticfinalStringdriver="com.mysql.jdbc.Driver";

privatestaticfinalStringurl="jdbc:mysql://localhost:3306/area";

privatestaticfinalStringusername="root";

privatestaticfinalStringpassword="accp";

privatestaticConnectioncon;

static{

try{

Class.forName(driver);

con=DriverManager.getConnection(url,username,password);

}catch(Exception e) {

e.printStackTrace();

}

}

publicstaticConnection getCon(){

returncon;

}

publicstaticvoidmain(String[] args) {

Connection con1=BaseDao.getCon();

System.out.println(con1);

}

}

第二种连接数据库的方法

1.创建database文件

jdbc.driver_class=oracle.jdbc.driver.OracleDriver

jdbc.connection.url=jdbc\:oracle\:thin\:@localhost\:1521\:orcl

jdbc.connection.username=scott

jdbc.connection.password=tiger

2.创建ConfigManager类

importjava.io.InputStream;

importjava.util.Properties;

//在这个类是用来读取database.properties文件的

publicclassConfigManager{

privatestaticConfigManagerconfigManager;

privatestaticPropertiesproperties;

//创建构造方法

privateConfigManager(){

String datafile="database.properties";

//实例化我的properties

properties=newProperties();

//通过我的当前类读取到我的属性文件返回一个输入流

InputStream in=ConfigManager.class.getClassLoader().getResourceAsStream(datafile);

try{

properties.load(in);

in.close();

}catch(Exception e) {

System.out.println(e);

}

}

//保证只创建一个configManager对象

publicstaticConfigManagergetIntance(){

if(configManager==null){

configManager=newConfigManager();

}

returnconfigManager;

}

//通过键返回值

publicString getString(String key){

returnproperties.getProperty(key);

}

}

2.创建basedao类建立数据库连接

importjava.sql.Connection;

importjava.sql.DriverManager;

importjava.sql.SQLException;

importjavax.naming.NamingException;

importcom.dbqn.JDBC.util.ConfigManager;

//用来连接数据库的基础类,里面包含一个getCon()方法

publicclassBaseDao {

publicstaticConnection getCon(){

//通过键得到值driver驱动,url地址username用户名password密码

String driver=ConfigManager.getIntance().getString("jdbc.driver_class");

String url=ConfigManager.getIntance().getString("jdbc.connection.url");

String username=ConfigManager.getIntance().getString("jdbc.connection.username");

String password=ConfigManager.getIntance().getString("jdbc.connection.password");

System.out.println(driver);

System.out.println(url);

Connection con=null;

try{

//加载驱动

Class.forName(driver);

//连接数据库

con=DriverManager.getConnection(url, username,password);

}catch(Exception e) {

System.out.println(e);

}

returncon;//返回

}

//当前页测试

publicstaticvoidmain(String[] args)throwsNamingException, SQLException{

Connection con=BaseDao.getCon();

System.out.println(con);

}

}

注册驱动的三种方法

1.使用DriverManager的registerDriver方法进行加载驱动

DriverManager.registerDriver(new com.mysql.jdbc.Driver());

2.使用System.setProperty(“jdbc.driver”,”com.mysql.jdbc.Driver”)

3.Class.forName(“com.mysql.jdbc.Driver”)