欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

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();
 	}
}

相关标签: eclipse MySQL