JAVA JDBC代码
时间: 2024-02-18 07:39:30 浏览: 11
示例
这里有一个简单的JDBC示例代码,可以帮助你连接到数据库:
try {
// 加载驱动程序
Class.forName("com.mysql.jdbc.Driver");
// 连接数据库
String url = "jdbc:mysql://localhost:3306/databaseName";
Connection conn = DriverManager.getConnection(url,"username","password");
// 创建SQL语句
String sql = "select * from tableName";
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery(sql);
// 处理结果
while (rs.next()) {
// do something
}
// 关闭连接
rs.close();
stmt.close();
conn.close();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
相关问题
java jdbc代码
示例
这里有一个简单的JDBC示例代码,可以帮助你连接到数据库:
try {
// 加载驱动程序
Class.forName("com.mysql.jdbc.Driver");
// 连接数据库
String url = "jdbc:mysql://localhost:3306/databaseName";
Connection conn = DriverManager.getConnection(url,"username","password");
// 创建SQL语句
String sql = "select * from tableName";
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery(sql);
// 处理结果
while (rs.next()) {
// do something
}
// 关闭连接
rs.close();
stmt.close();
conn.close();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)