MySql连接数据库
2011-11-10 12:33
232 查看
import java.sql.*; public class TestMysqlConnection { 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=105148802"); stmt = conn.createStatement(); rs = stmt.executeQuery("select * from dept "); while (rs.next()) { System.out.println(rs.getString("deptno")); } } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (SQLException ex) { // handle any errors System.out.println("SQLException: " + ex.getMessage()); System.out.println("SQLState: " + ex.getSQLState()); System.out.println("VendorError: " + 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(); } } } }
相关文章推荐
- MySQL-----远程连接数据库
- navicat连接mysql,数据库的表中文出现乱码
- MySql连接数据库简单示例
- MySQL-数据库内连接与外连接与交叉连接
- mysql连接数据库的三种方法(连接池,jdbc,Hibernate)
- 执行Class.forName("com.mysql.jdbc.Driver");连接数据库时抛异常
- Tomcat容器下 jdni 数据库连接也的配置 (oracle,mysql)
- shell连接mysql并创建数据库表
- python连接MySQL、MongoDB、Redis、memcache等数据库的方法
- MYSQL 连接数据库命令收藏
- QTP中连接MySQL的方法(数据库验证点和ADO连接)
- navicat for mysql 远程连接数据库时出现2003-Can't connect to Mysql on '主机名'(10061)解决方法
- Linux 下 C 语言连接 MySQL 数据库 / 西安嵌入式培训
- Jsp 连接 mySQL、Oracle 数据库备忘(Windows平台)
- MySQL加载 数据库连接的两个方法:
- 新手Centos7安装mysql,并使用libzdb数据库链接池连接数据库
- Jsp 连接 mySQL、Oracle 数据库备忘(Windows平台) 选择自 dazern 的 Blog
- kittle连接数据库(连接SQL server、MySQL)
- JDBC连接mysql和各种数据库连接串
- Navicate for mysql 连接数据库