javaweb中mysql数据库连接步骤方法及其实例

网友投稿 407 2023-05-23


javaweb中mysql数据库连接步骤方法及其实例

一、直接连接,不封装到工具类中,主要步骤:

先导包:mysql-connector-java-5.0.8-bin.jar(点击跳转到下载界面),放在WebRoot/WEB-INF/lib/下

1.加载驱动//com.MySQL.jdbc.Driver

2.获取连接 Connection对象

3.获取用于向数据库发送SQL的Statement对象

4.执行sql,获取数据,解析数据

5.关闭连接,释放资源

/*协议:子协议://主机:端口/数据库名*/

Stringurl="jdbc:mysql://localhost:3306/jdbctest";

//mysql数据库的用户名与密码,安装时自己设置,一般默认为root

Stringuser="root";

Stringpassword="root";

Connectionconnection=null;

Statementstatement=null;

ResultSetresultSet=null;

try{

//1.加载驱动//com.mysql.jdbc.Driver

/*

*DriverManager.registerDriver(new

*Driver());用这种方法会加载两次驱动,也就是说会创建两个drive对象

*/

Class.forName("com.mysql.jdbc.Driver");

//2.获取连接

connection=DriverManager.getConnection(url,user,password);

//3.获取用于向数据库发送SQL的Statement对象

statement=connection.createStatement();

//4.执行sql,获取数据

resultSet=statement.executeQuery("SELECT*FROMusers;");

//解析数据

while(resultSet.next()){

intid=resultSet.getInt("id");

Stringname=resultSet.getString("name");

Stringpsd=resultSet.getString("password");

Stringemail=resultSet.getString("email");

Stringbirthday=resultSet.getString("birthday");

System.out.println(id+""+name+""+psd+""+email

+""+birthday);

}

}catch(ClassNotFoundExceptione){

e.printStackTrace();

}catch(SQLExceptione){

e.printStackTrace();

}finally{

//5.关闭连接,释放资源

if(resultSet!=null){

try{

resultSet.close();

}catch(SQLExceptione){

//TODOAuto-generatedcatchblock

e.printStackTrace();

}

resultSet=null;

}

if(statement!=null){

try{

statement.close();

}catch(SQLExceptione){

//TODOAuto-generatedcatchblock

e.printhttp://StackTrace();

}

statement=null;

}

if(connection!=null){

try{

connection.close();

}catch(SQLExceptione){

//TODOAuto-generatedcatchblock

e.printStackTrace();

}

connection=null;

}

/* 协议:子协议://主机:端口/数据库名 */

String url = "jdbc:mysql://localhost:3306/jdbctest";

// mysql数据库的用户名与密码,安装时自己设置,一般默认为root

String user = "root";

String password = "root";

Connection connection = null;

Statement statement = null;

ResultSet resultSet = null;

try {

// 1.加载驱动//com.mysql.jdbc.Driver

/*

* DriverManager.registerDriver(new

* Driver());用这种方法会加载两次驱动,也就是说会创建两个drive对象

*/

Class.forName("com.mysql.jdbc.Driver");

// 2.获取连接

connection = DriverManager.getConnection(url, user, password);

// 3.获取用于向数据库发送SQL的Statement对象

statement = connection.createStatement();

// 4.执行sql,获取数据

resultSet = statement.executeQuery("SELECT * FROM users;");

// 解析数据

while (resultSet.next()) {

int id = resultSet.getInt("id");

String name = resultSet.getString("name");

String psd = resultSet.getString("password");

String email = resultSet.getString("email");

String birthday = resultSet.getString("birthday");

System.out.println(id + " " + name + " " + psd + " " + email

+ " " + birthday);

}

} catch (ClassNotFoundException e) {

e.printStackTrace();

} catch (SQLException e) {

e.printStackTrace();

} finally {

//5.关闭连接,释放资源

if (resultSet != null) {

try {

resultSet.close();

} catch (SQLException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

resultSet = null;

}

if (statement != null) {

try {

statement.close();

} catch (SQLException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

statement = null;

}

if (connection != null) {

try {

connection.close();

} catch (SQLException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

connection = null;

}

}

二、将数据库连接封装成一个工具类

这样做的好处是,在实际开发中,就能做到,改一处即可修改全局。

1.建一个名为db.properties的配置文件,放于src/

url=jdbc:mysql://localhost:3306/jdbctest

username=root

password=root

driver=com.mysql.jdbc.Driver

2.工具类:

importjava.io.IOException;

importjava.sql.Connection;

importjava.sql.DriverManager;

importjava.sql.ResultSet;

importjava.sql.SQLException;

importjava.sql.Statement;

importjava.util.Properties;

publicclassJdbcUtil{

//私有静态变量,用以读取配置文件

privatestaticPropertiesconfig=newProperties();

static{

try{

//配置资源文件

config.load(JdbcUtil.class.getClassLoader().getResourceAsStream("db.properties"));

//加载驱动

Class.forName(config.getProperty("driver"));

}catch(IOExceptione){

e.printStackTrace();

}catch(ClassNotFoundExceptione){

e.printStackTrace();

}

}

publicstaticConnectiongetConnection(){

Connectionconnection=null;

try{

connection=DriverManager.getConnection(config.getProperty("url"),config.getProperty("username"),config.getProperty("password"));

}catch(SQLExceptione){

e.printStackTrace();

}

returnconnection;

}

//用以关闭连接,释放资源

publicstaticvoidreleaseConn(Connectionconnection,Statementstatement,

ResultSetresultSet){

if(resultSet!=null){

try{

resultSet.close();

}catch(SQLExceptione){

e.printStackTrace();

}

resultSet=null;

}

if(statement!=null){

try{

statement.close();

}catch(SQLExceptione){

e.printStackTrace();

}

statement=null;

}

if(connection!=null){

try{

connection.close();

}catch(SQLExceptione){

e.printStackTrace();

}

connection=null;

}

}

}

3.使用实例:

Connectionconnection=null;

Statementstatement=null;

ResultSetresultSet=null;

try{

//调用工具类中的静态方法来获取连接

connection=JdbcUtil.getConnection();

statement=connection.createStatement();

resultSet=statement.executeQuery("select*fromusers");

while(resultSet.next()){

intid=resultSet.getInt("id");

Stringname=resultSet.getString("name");

Stringpsd=resultSet.getString("password");

Stringemail=resultSet.getString("email");

Stringbirthday=resultSet.getString("birthday");

System.out.println(id+""+name+""+psd+""+email

+""+birthday);

}

}catch(Exceptione){

e.printStackTrace();

}finally{

//调用工具类中的静态方法来关闭连接,释放资源

JdbcUtil.releaseConn(connection,statement,resultSet);

}

希望本文可以对需要的朋友有帮助


版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:JAVA 运算符归纳总结
下一篇:微信小程序教程系列之页面跳转和参数传递(6)
相关文章

 发表评论

暂时没有评论,来抢沙发吧~