JDBC
数据库驱动
导入mysql-connector-java-数据库版本号.jar这个包
写一个JDBC程序:
代码如下:
public class jdbcTest {public static void main(String[] args) throws ClassNotFoundException, SQLException {//加载驱动Class.forName("com.mysql.cj.jdbc.Driver");//用户信息和urlString url="jdbc:mysql://localhost:3306/school?useUnicode=true&characterEncoding=utf8&useSSL=true";String username="root";String password="123456";//连接成功,数据库对象 connection代表数据库Connection connection= DriverManager.getConnection(url,username,password);Statement statement=connection.createStatement();//执行sql对象String sql="SELECT * FROM grade";ResultSet resultSet=statement.executeQuery(sql);//返回结果集,结果集中封装了查询的所有结果while (resultSet.next()){System.out.println("gradeid:"+resultSet.getObject("gradeid"));System.out.println("gradename:"+resultSet.getObject("gradename"));}//执行sql对象去执行sql,可能存在结果,查看结果resultSet.close();statement.close();connection.close();//释放连接}
DriverManager
DriverManager.registerDriver(new com.mysql.cj.jdbc.Driver());
Class.forName(“com.mysql.cj.jdbc.Driver”)//固定写法
URL
String url=
” jdbc:mysql://localhost:3306/school?useUnicode=true&characterEncoding=utf8&useSSL=true”;
mysql—3306
// 协议://主机地址:端口号/数据库名?参数1&参数2&参数3
oracle—1521
jdbc:oracle:thin:@localhost:1521:sid