Java 怎么连接数据库??

来源:百度知道 编辑:UC知道 时间:2024/04/28 05:05:49

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class TestMySQL {

/**
* @param args
* @throws ClassNotFoundException
* @throws SQLException
*/
public static void main(String[] args) throws ClassNotFoundException,
SQLException {
// TODO Auto-generated method stub
// 1.加载驱动程序
Class.forName("org.gjt.mm.mysql.Driver");
//com.mysql.jdbc.Driver
// 2.建立连接
String url = "jdbc:mysql://localhost:3306/scott";
Connection conn = DriverManager.getConnection(url, "root", "123");
//3.创建语句对象
Statement stmt = conn.createStatement();
//4.查询
ResultSet rs = stmt.executeQuery("select DEPTNO,DNAME,LOC from dept");

while (rs.next()) {
System.out.println("------------"