码迷,mamicode.com
首页 > 数据库 > 详细

java 链接mysql

时间:2017-12-04 00:15:04      阅读:171      评论:0      收藏:0      [点我收藏+]

标签:pid   word   tor   sync   dex   org   bsp   pac   cut   

//在pom的依赖里增加 mysql的artifact,形如
//groupId = mysql
//artifactId = mysql-connector-java
//version = 5.1.25

 

 

package chen;
import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.Driver;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;

import org.omg.Messaging.SyncScopeHelper;


public class mystest {

static String url;
static String name;
static String password;

static{
  Properties cfg=new Properties();
  InputStream in=chen.mystest.class.getClassLoader().getResourceAsStream("db.properties");
  try {
    cfg.load(in);
    System.out.println(cfg);
    url=cfg.getProperty("url");
    name=cfg.getProperty("name");
    password=cfg.getProperty("password");
  }

   catch (Exception e) {
    throw new RuntimeException(e);
    }
}

public static void main(String[] args) throws IOException, ClassNotFoundException, SQLException {

  Class.forName("com.mysql.jdbc.Driver");
  System.out.println("chenggong");
  Connection conn=DriverManager.getConnection(url,name,password);
  System.out.println("链接成功");
  Statement smt=conn.createStatement();
  String sql="select * from test";
  ResultSet rs= smt.executeQuery(sql);
  while(rs.next()){
    name=rs.getString("name");
    String id=rs.getString("id");
    int no=rs.getInt("no");
    String name=rs.getString("name");
    System.out.println(name);
    System.out.println(no);
    System.out.println(id);
    }
  }

}

 

java 链接mysql

标签:pid   word   tor   sync   dex   org   bsp   pac   cut   

原文地址:http://www.cnblogs.com/czb529514/p/7967837.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!