package org.mysqljdbc;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class Jdbc {
/**
* @param args
*/
public static void main(String[] args) {
ResultSet rs = null;
Statement stmt = null;
Connection conn = null;
String driver = "org.gjt.mm.mysql.Driver";
String url ="jdbc:mysql://localhost/mydata?user=root&password=root&useUnicode=true&characterEncoding=8859_1";
//加载驱动
try {
Class.forName(driver);
conn = DriverManager.getConnection(url);
stmt = (Statement) conn.createStatement();
rs =stmt.executeQuery("select * from dept");
while(rs.next())
{
System.out.println(rs.getString("deptno"));
System.out.println(rs.getString("dname"));
}
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally
{
try
{
if(rs != null)
{
rs.close();
rs = null;
}
if(conn != null)
{
conn.close();
conn = null;
}
}catch (Exception e) {
e.printStackTrace();
}
}
}
}