Java 用jdbc连接数据库
准备
安装mysql
下载 mysql-connector-java-5.1.40-bin.jar驱动包
步骤
-
创建jdbc项目
-
导入mysql-connector-java-5.1.40-bin.ja包
-
创建配置文件
jdbc.properties
user=root password=1230 driverClass=com.mysql.jdbc.Driver jdbcUrl=jdbc:mysql:///atguigu |
-
代码
import java.io.IOException; import java.io.InputStream; import java.util.Properties; import java.sql.DriverManager; import java.sql.SQLException; import public public JDBCTools() {
} public Connection Properties properties = newProperties(); InputStream inStream = JDBCTools.class.getClassLoader() .getResourceAsStream(“jdbc.properties”); properties.load(inStream); String user = properties.getProperty(“user”); String password = properties.getProperty(“password”); String driverClass = properties.getProperty(“driverClass”); String jdbcUrl = properties.getProperty(“jdbcUrl”);
conn = (Connection) DriverManager.getConnection(jdbcUrl,user,password);
return <span 【声明】:本博客不参与任何交易,也非中介,仅记录个人感兴趣的主机测评结果和优惠活动,内容均不作直接、间接、法定、约定的保证。访问本博客请务必遵守有关互联网的相关法律、规定与规则。一旦您访问本博客,即表示您已经知晓并接受了此声明通告。
|