String driverClassName = null; 和 String driverClassName;在底层上的区别
时间: 2024-04-10 09:32:25 浏览: 14
在底层上,`String driverClassName = null;`和`String driverClassName;`的区别是变量的初始值不同。
1. `String driverClassName = null;`表示在声明变量时,将其初始化为`null`,即空值。这意味着变量`driverClassName`在使用之前需要显式地赋予一个具体的值,否则它将保持为`null`。
2. `String driverClassName;`表示在声明变量时,没有为其指定初始值。这意味着变量`driverClassName`的初始值是未定义的,取决于具体的环境和语言规范。在Java中,未初始化的局部变量会被默认赋予一个默认值,对于引用类型(如`String`),默认值是`null`。
所以,在Java中这两个声明的底层区别是很小的。它们都声明了一个`String`类型的变量,但是第一个明确指定了初始值为`null`,而第二个则依赖于语言规范为其赋予默认值。
相关问题
jdbc.driverClassName=com.mysql.jdbc.Driver jdbc.url=jdbc:mysql://127.0.0.1:3307/bookstore?useUnicode=true&characterEncoding=utf-8 jdbc.username=root jdbc.password=root这段代码是怎样连接数据库的
这段代码是一个 Java 项目中使用 JDBC 连接 MySQL 数据库的配置信息。其中:
- `jdbc.driverClassName` 指定了 JDBC 驱动的类名,这里使用的是 MySQL 官方提供的 `com.mysql.jdbc.Driver`。
- `jdbc.url` 指定了数据库的连接地址,其中 `127.0.0.1` 表示本地主机,`3307` 是 MySQL 服务器的端口号,`bookstore` 是要连接的数据库名称,`useUnicode=true&characterEncoding=utf-8` 则指定了使用 UTF-8 编码进行字符集转换。
- `jdbc.username` 和 `jdbc.password` 分别是数据库的登录用户名和密码。
在使用这些配置信息连接数据库时,可以使用如下代码:
```java
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class Main {
public static void main(String[] args) {
Connection conn = null;
try {
// 加载 JDBC 驱动
Class.forName("com.mysql.jdbc.Driver");
// 获取数据库连接
conn = DriverManager.getConnection(
"jdbc:mysql://127.0.0.1:3307/bookstore?useUnicode=true&characterEncoding=utf-8",
"root",
"root");
// TODO: 执行数据库操作
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
} finally {
// 关闭数据库连接
if (conn != null) {
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
}
```
package db; import java.sql.*; public class Shujuku { private static final String URL = "jdbc:mysql://localhost:3306/?db_student"; private static final String USERNAME = "root"; private static final String PASSWORD = "123456"; private static final String DRIVER_CLASS = "com.mysql.jdbc.Driver"; public void kk(){ try{ Class.forName(DRIVER_CLASS); Connection conn = DriverManager.getConnection(URL, USERNAME, PASSWORD); Statement statement=conn.createStatement(); System.out.println("已经成功连接到数据库!"); } catch (Exception e) { throw new RuntimeException(e); } }}优化
优化点:
1. 数据库连接信息应该封装到配置文件中,以便于修改和维护。
2. 数据库连接应该使用连接池进行管理,避免频繁创建和销毁连接对象。
3. 应该使用 PreparedStatement 对象执行 SQL 语句,避免 SQL 注入攻击。
4. 避免在方法中抛出 RuntimeException 异常,应该捕获异常并进行处理。
下面是优化后的代码:
```
package db;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Properties;
import javax.sql.DataSource;
import com.alibaba.druid.pool.DruidDataSourceFactory;
public class DatabaseUtil {
private static DataSource ds;
static {
try {
InputStream is = DatabaseUtil.class.getClassLoader().getResourceAsStream("db.properties");
Properties props = new Properties();
props.load(is);
ds = DruidDataSourceFactory.createDataSource(props);
} catch (Exception e) {
throw new ExceptionInInitializerError(e);
}
}
public static Connection getConnection() throws SQLException {
return ds.getConnection();
}
public static void close(Connection conn, PreparedStatement ps, ResultSet rs) {
try {
if (rs != null) {
rs.close();
}
if (ps != null) {
ps.close();
}
if (conn != null) {
conn.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
```
```
package db;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class Shujuku {
public void kk() {
Connection conn = null;
PreparedStatement ps = null;
ResultSet rs = null;
try {
conn = DatabaseUtil.getConnection();
ps = conn.prepareStatement("select * from student");
rs = ps.executeQuery();
while (rs.next()) {
System.out.println(rs.getInt("id") + " " + rs.getString("name") + " " + rs.getInt("age"));
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
DatabaseUtil.close(conn, ps, rs);
}
}
}
```
这里使用了阿里巴巴的 Druid 数据库连接池,可以在 Maven 中添加以下依赖项来获取:
```
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.2.4</version>
</dependency>
```
需要在类路径中添加 `db.properties` 配置文件,内容如下:
```
driverClassName=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/db_student
username=root
password=123456
initialSize=5
minIdle=5
maxActive=20
```
其中 `initialSize` 表示初始化连接数,`minIdle` 表示最小空闲连接数,`maxActive` 表示最大连接数。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)