马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有账号?立即注册
x
来源:http://www.open-open.com/code/view/1454633126620
- package learn;
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.SQLException;
- import java.sql.Statement;
- import java.sql.ResultSet;
- public class Example01 {
- public static void main(String[] args) {
- // TODO Auto-generated method stub
- String URL="jdbc:mysql://127.0.0.1:3306/test1";
- String DRIVER="com.mysql.jdbc.Driver";
- String USERNAME="root";
- String PASSWD="123456";
- String sql;
- Connection connection =null;
- try{
- Class.forName(DRIVER);
- connection=DriverManager.getConnection(URL,USERNAME,PASSWD);
- Statement stmt = connection.createStatement();
- sql = "select * from t1";
- ResultSet rs=stmt.executeQuery(sql);
- while(rs.next()){
- System.out.println(rs.getInt(1)+" "+rs.getString(2)+" "+rs.getString(3));
- }
- }catch(SQLException e){
- System.out.println("test1");
- e.printStackTrace();
- }catch(ClassNotFoundException e){
- System.out.println("test2");
- e.printStackTrace();
- }finally{
- try{
- connection.close();
- }catch(SQLException e){
- System.out.println("test3");
- e.printStackTrace();
- }
- }
- }
- }
复制代码
|