JAVA链接mysql数据库

作者在 2018-03-26 14:56:01 发布以下内容
package sjk_1;
import java.sql.*;
public class sjkmysql_1 {
public static void main(String[] args){
Connection con;
String driver = "com.mysql.jdbc.Driver";
String url="jdbc:mysql://localhost:3306/usb";
String user="root";
String password="root";
try{
Class.forName(driver);
con=DriverManager.getConnection(url,user,password);
if(!con.isClosed())
System.out.println("—————————————");
System.out.println("姓名"+"\t"+"职称");
Statement statement = con.createStatement();
String sql = "select * from emp";
ResultSet rs = statement.executeQuery(sql);
System.out.println("—————————————");
String job = null;
String id = null;
while(rs.next()){
job = rs.getString("job");
id = rs.getString("ename");
               System.out.println(id + "\t" + job);
           }
           rs.close();
           con.close();
       } catch(ClassNotFoundException e) {   
           System.out.println("Sorry,can`t find the Driver!");   
           e.printStackTrace();   
           } catch(SQLException e) {
           e.printStackTrace();  
           }catch (Exception e) {
           e.printStackTrace();
       }finally{
           System.out.println("数据库数据成功获取!!");
       }
   }


}
默认分类 | 阅读 771 次
文章评论,共0条
游客请输入验证码
浏览775次
文章分类
文章归档
最新评论