JDBC将连接数据库的信息配置到配置文件中
jdbc.properties文件:driver=com.mysql.jdbc.Driverurl=jdbc:mysql://localhost:3366/bjpowernodeuser=rootpassword=123package 连接数据库的信息配置到配置文件;import java.sql.Connection;import java.sql.DriverManager...
·

jdbc.properties文件:
driver=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3366/bjpowernode
user=root
password=123
package 连接数据库的信息配置到配置文件;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ResourceBundle;
public class JDBCTest05 {
public static void main(String[] args) {
//使用资源绑定器绑定配置文件
ResourceBundle bundle = ResourceBundle.getBundle("jdbc");
String driver = bundle.getString("driver");
String url = bundle.getString("url");
String user = bundle.getString("user");
String password = bundle.getString("password");
Connection conn = null;
Statement stmt = null;
try {
//注册驱动
Class.forName(driver);
//获取连接
conn = DriverManager.getConnection(url,user,password);
//获取操作数据库对象
stmt = conn.createStatement();
//执行sql
String sql = "insert into dept(deptno,dname,loc) values(80,'人事部','北京')";
int count = stmt.executeUpdate(sql);
System.out.println(count);
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}finally {
if(stmt!=null){
try {
stmt.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if(conn!=null){
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
}
DAMO开发者矩阵,由阿里巴巴达摩院和中国互联网协会联合发起,致力于探讨最前沿的技术趋势与应用成果,搭建高质量的交流与分享平台,推动技术创新与产业应用链接,围绕“人工智能与新型计算”构建开放共享的开发者生态。
更多推荐


所有评论(0)