Myeclipse连接mysql小程序案例

来源:互联网 发布:矩阵正交化有什么用 编辑:程序博客网 时间:2024/05/17 21:49

package Mysql;
import java.sql.*;
//连接数据的
public class MysqlConnection {
public static void main(String[] args) {
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
try {
Class.forName("com.mysql.jdbc.Driver");//
conn = DriverManager.getConnection("" +
"jdbc:mysql://localhost/mydata?user=root&password=123456");//连接地址,用户名和密码;
stmt = conn.createStatement();//连接状态;
rs = stmt.executeQuery("select * from dept");
while (rs.next()) {
System.out.println(rs.getString("deptno"));
}
} catch (ClassNotFoundException ex) {
ex.printStackTrace();
} catch (SQLException ex) {
System.out.println("SQLException:" + ex.getMessage());
System.out.println("SQLState:" + ex.getStackTrace());
System.out.println("VendorErro:" + ex.getErrorCode());
} finally {
try {
if (rs != null) {
rs.close();
rs = null;
}
if (stmt != null) {
stmt.close();
stmt = null;
}
if (conn != null) {
conn.close();
conn = null;
}

} catch (SQLException e) {
e.printStackTrace();
}
}
}
}

0 0