其他分享
首页 > 其他分享> > jdbc连接

jdbc连接

作者:互联网

package com.song.testjdbc;

import java.sql.*;

public class test {
public static void main(String[] args) throws ClassNotFoundException, SQLException {
//导入驱动
Class.forName("com.mysql.cj.jdbc.Driver");
//连接信息 用户信息url
String url = "jdbc:mysql://localhost:3306/java_test?useUnicode=true&characterEncoding=utf8&useSSL=true";
String name = "root";
String pwd = "123456";
//连接成功 返回数据库对象 connection代表数据
Connection connection = DriverManager.getConnection(url, name, pwd);
//执行sql statement执行sql对象
Statement statement = connection.createStatement();
String sql = "SELECT * FROM tb_user";
ResultSet resultSet = statement.executeQuery(sql);
while (resultSet.next()){
System.out.println("id" + resultSet.getObject("userid"));
System.out.println("id" + resultSet.getObject("username"));
System.out.println("id" + resultSet.getObject("userPwd"));
System.out.println("id" + resultSet.getObject("userAge"));
}
//释放连接
resultSet.close();
statement.close();
connection.close();
}
}

搜索

复制

标签:jdbc,String,resultSet,id,getObject,sql,连接,out
来源: https://www.cnblogs.com/wutonga/p/16351505.html