使用java连接Mysql 和Using JDBC Statement Objects to Execute SQL
2014-02-25 14:00
603 查看
使用java连接Mysql 和Using JDBC Statement Objects to Execute SQL
首先引入mysql-connector-javaXXXXX.jar的包:
项目test右键 Build Path> Add External Achives.....>选择mysql-connector-javaXXXXX.jar(已经下载)
首先引入mysql-connector-javaXXXXX.jar的包:
项目test右键 Build Path> Add External Achives.....>选择mysql-connector-javaXXXXX.jar(已经下载)
package test; import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; // Notice, do not import com.mysql.jdbc.* // or you will have problems! public class LoadDriver { public static void main(String[] args) { Connection conn = null; Statement stmt= null; ResultSet rs = null; try { // The newInstance() call is a work around for some // broken Java implementations Class.forName("com.mysql.jdbc.Driver").newInstance(); } catch (Exception ex) { ex.printStackTrace(); } try { conn = DriverManager.getConnection("jdbc:mysql://localhost/mydatabase?" + "user=root&password=root"); stmt= conn.createStatement(); rs = stmt.executeQuery("select * from dept"); while(rs.next()){ System.out.println(rs.getString("name")); } // Do something with the Connection } 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(); } } } }
相关文章推荐
- Exemplar-SVMs for Object Detection and Beyond--Details (二)
- Exemplar-SVMs for Object Detection and Beyond—Overview(一)
- javaBean与Map<String,Object>互转
- equals 和 hashcode 的关系,Object.equals 默认比较的是地址
- [Objective C]super dealloc 调用时机
- Objective-c KVO and KVC
- sql server sysobjects xtype类型
- new一个Object对象占用多少内存?
- 定义“面向对象”的SQLite—在SQLite中存储Object
- objective-c 在线视频 学习资料...
- OBJECTIVE C内存实践准则与理解AUTORELEASE
- JavaScript objects from Acrobat supported in Designer ES2
- static struct QMetaObject const QwtPlot::staticMetaObjec
- static struct QMetaObject const QwtPlot::staticMetaObjec
- Delphi对象池MyObjectPool.pas
- org.hibernate.NonUniqueObjectException两种解决方法
- objective-c宏定义
- List<object>排序 z
- 谈Objective-C Block的实现
- Objective-C对象模型及应用