jdbc的快速入门(需要mysql-connector-java-5.1.39-bin.jar包)

package Lianxi;


import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.Properties;

public class Topic1 {
public static void main(String[] args) throws Exception {
//1注册驱动(解决硬代码)
//1.1创建Properties
Properties p = new Properties();
//1.2创建输入流,关联配置文件
InputStream is = Topic1.class.getClassLoader().getResourceAsStream("wzwProp.properties");
//1.3读取配置文件中的信息,到properties中
p.load(is);
//1.4获取路径
String driverClass = p.getProperty("driverClass");
//1.5反射,注册驱动
Class.forName(driverClass);


//2.获取连接
Connection connection = DriverManager.getConnection(p.getProperty("url"),p.getProperty("user"),p.getProperty("password"));
//2.1获取sql语句执行对象(执行者,执行平台)
Statement statement = connection.createStatement();
//3.创建sql语句
String sql = "select * from user";
//4.执行sql语句,获取结果集 声明.执行查询(sql)
ResultSet resultSet = statement.executeQuery(sql);
//5.处理结果
while (resultSet.next()){
int id = resultSet.getInt("id");
String name = resultSet.getString("name");
String password = resultSet.getString("password");
System.out.println("id ="+id + "name ="+name + "password ="+password );
}
//6.释放资源
resultSet.close();
statement.close();
connection.close();
is.close();


}
}

文件内容
driverClass=com.mysql.jdbc.Driver        //jar中的Driver
url=jdbc:mysql://localhost:3306/wzw      //本地数据库
user=root                      //用户
password=root                    //密码

jdbc的快速入门(需要mysql-connector-java-5.1.39-bin.jar包)

上一篇:mysql—mysql查询语句提示Unknown column ‘xxx’ in ‘where clause’


下一篇:大数据第55天—Mysql练习题12道之十-查询各自区组的money排名前十的账号-杨大伟