连接数据库基本步骤

1.在项目中导入java.sql包
2.加载数据库驱动程序
Class.forName(“com.mysql.jdbc.Driver”);
3.定义数据库的链接地址
String url=“jdbc:mysql://localhost:3306/idea?characterEncoding=utf-8”;
String useeName=“root”;
String userPwd=“123456”;
4.得到与数据库的连接对象
Connection conn=DriverManager.getConnection(url, userName, userPwd);
5.声明sql语句
select * from Good  where goodId=?;
6.得到语句对象
PrepareStatement pst=null;

ResultSet rs=null;

7.执行sql语句
pst=conn.prepareStatement(sql); 

rs=pst.executeQuery();
8.处理sql语句的返回结果
9.关闭对象

posted @ 2019-06-18 17:42  男吖学java  阅读(1958)  评论(0编辑  收藏  举报