IT虾米网

JAVA mysql数据库连接

leader 2018年06月24日 数据库 1079 0

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();

}

}

}


}


评论关闭
IT虾米网

微信公众号号:IT虾米 (左侧二维码扫一扫)欢迎添加!