[Java] TestMysqlConnection ( Java 连接 mysql 初步)
2013-12-19 18:34
573 查看
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"); System.out.println("HERE"); conn = DriverManager.getConnection("jdbc:mysql://localhost/mydata?user=root&password=root"); System.out.println("HERE"); 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(); } } } }
相关文章推荐
- [Java] TestMysqlConnection ( Java 连接 mysql 初步)
- java连接zookeeper服务器出现“KeeperErrorCode = ConnectionLoss for /test”
- powerdesigner 连接mysql提示“connection test failed”
- Java使用mysql-jdbc连接MySQL出现如下警告: WARN: Establishing SSL connection without server's identity 。。。
- 现在的连接url:Connection connect = DriverManager.getConnection("jdbc:mysql://localhost:3306/test?useUnico
- power designer 连接mysql提示“connection test failed”
- 连接Ubuntu系统服务器Mysql出错(错误:10061)/Jdbc连接远程数据库出错(java.net.ConnectException: Connection refused: connect)
- powerdesigner 连接mysql提示“connection test failed”
- java web应用连接mysql会突然connection连接失败
- Java:JDBC篇,Connection连接至几种常用数据库(Oracle,MySQL,Access,SQL Server)
- power designer 连接mysql提示“connection test failed”
- Java连接mysql!并能读取中文
- JDBC_demo:java连接mysql过程
- java web连接mysql,汉字乱码显示为“???”
- java连接mysql 测试代码
- 对于java.sql.DriverManager 能在本地连接mysql 驱动而在tomcat服务器下无法加载的情况
- Java连接mySql——简单JDBC连接数据库
- java连接mysql
- java连接sql server2008 r2 错误Cannot create PoolableConnectionFactory 错误
- 简单好用! JAVA JDBC 连接 MYSQL