Java連線mysql的程式

jiacai2050發表於2012-03-13
package com.ytu.DBTest;

import java.sql.DriverManager;
import java.sql.ResultSet;

import com.mysql.jdbc.*;
public class Test {
	public static void main(String[] args) {
		String url = "jdbc:mysql://127.0.0.1:3306/student";
		try {
			Class.forName("com.mysql.jdbc.Driver");
			Connection conn = (Connection) DriverManager.getConnection(url, "root", "123456");
			Statement sql = (Statement) conn.createStatement();
			ResultSet rs = sql.executeQuery("select * from info");
			while(rs.next()) {
				//System.out.println(rs.getString(3));
				System.out.println(rs.getObject(1));
			}
				
			System.out.println("Successfull!!");
			
		} catch (Exception e) {
			System.out.println("Link Error");
			e.printStackTrace();
		}
		
	}
	
}


相關文章