eclipse连接mysql代码。。。。。。
程序员文章站
2022-03-03 17:41:54
...
package qwe;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class asd{
private static final String DB_URL = "jdbc:mysql://localhost:3306/nu123?useUnicode =true&characterEncoding = UTF-8&useSSL =false&serverTimezone = GMT";
static final String userName = "root"; //nu123是数据库库名
static final String passwd = "111111";
public static void main(String[] args) throws SQLException, ClassNotFoundException {
Connection con = null;
Statement sta = null;
Class.forName("com.mysql.cj.jdbc.Driver");
System.out.println("Connecting to database...");
con = DriverManager.getConnection(DB_URL, userName, passwd);
System.out.println("Creating statement...");
sta = con.createStatement();
String sql;
sql = "SELECT stu_id, stu_name FROM asd123"; //asd123是表名,stu_id, stu_name是列名
ResultSet rs = sta.executeQuery(sql);
System.out.println("rs: " + rs.toString());
while (rs.next()) {
int id = rs.getInt("stu_id");
String name = rs.getString("stu_name");
System.out.print("ID: " + id + " ");
System.out.println("name: " + name);
}
sta.close();
con.close();
rs.close();
}
}
上一篇: MySQL简单增删改查
下一篇: JAVA使用JDOM创建简单xml
推荐阅读
-
node连接数据库如何输出(前端js连接mysql数据库的方法)
-
MySQL错误代码1862 your password has expired的解决方法
-
Spring Data JPA例子代码[基于Spring Boot、Mysql]
-
Mysql 出现故障应用直接中断连接导致数据被锁(生产故障)详解
-
Ubuntu16.04 server下配置MySQL,并开启远程连接的方法
-
Eclipse查看开发包jar里源代码的方法
-
JSP 连接MySQL配置与使用
-
阿里云ECS云服务器(linux系统)安装mysql后远程连接不了(踩坑)
-
mysql存数组的实例代码和方法
-
详解MySQL开启远程连接权限