2007年5月16日星期三

Java connect Mysql via JDBC

This is a example.
--------------------
import junit.framework.JUnit4TestAdapter;
import org.junit.Test;
 
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;
import java.sql.ResultSet;
 
/**
 * Created by IntelliJ IDEA.
 * User: Henry Zhu
 * Date: 2007-5-17
 * Time: 11:16:13
 * To change this template use File | Settings | File Templates.
 */
public class MysqlConTest {
    public static junit.framework.Test suite() {
        return new JUnit4TestAdapter(MysqlConTest.class);
    }
    @Test
    public void linkt1() throws Exception
    {
    //  Class.forName("com.mysql.jdbc.Driver").newInstance();
     Class.forName( "com.mysql.jdbc.Driver");
     Connection cn= DriverManager.getConnection("jdbc:mysql://localhost:3306/shcool" ,"root","******");  //****** is your password of root
     Statement sm=cn.createStatement();
     ResultSet rs=sm.executeQuery("SELECT * FROM TEACHER");
        while(rs.next())
        {
         System.out.println(rs.getString("name"));
         System.out.println(rs.getString("salary"));
         System.out.println(rs.getString("age"));
        }
 
        rs.close();
        sm.close();
        cn.close();
    }
}

没有评论: