使用java第一次连接jdbc
package com.zxy.pojo;
import java.sql.*;
public class Jdbc {
public static void main(String[] args) throws ClassNotFoundException, SQLException {
//mysql加载驱动
Class.forName("com.mysql.jdbc.Driver");
/**
?后面跟参数 &分割参数
jdbctext 是数据库名
useSSL :与服务器进行通信时使用SSL(是否安全连接)
useUnicode: 处理字符串时,驱动程序是否应使用Unicode字符编码
characterEncoding: 如果“useUnicode”被设置为“真”,处理字符串时,驱动程序应使用什么字符编码
**/
//用户信息和url
String url = "jdbc:mysql://localhost:3306/jdbctext?useUnicode=true&characterEncoding=utf8&useSSL=false";
String username="root";
String password="2429203557zxy";
//获取数据库对象 ,连接成功
Connection connection = DriverManager.getConnection(url, username, password);
//执行sql 的对象
Statement statement = connection.createStatement();
//执行sql
String sql="select * from jdbctable";
//返回的结果集
ResultSet resultSet = statement.executeQuery(sql);
//在控制台打印数据
while(resultSet.next()){
System.out.println("id="+resultSet.getObject("id"));
System.out.println("name="+resultSet.getObject("name"));
}
//关闭连接
connection.close();
statement.close();
resultSet.close();
}
}