java 链接mysql

Posted 琦琦爸爸

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java 链接mysql相关的知识,希望对你有一定的参考价值。

//在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的主要内容,如果未能解决你的问题,请参考以下文章

java怎么连接mysql

java 链接mysql

MySQL - java链接mysql8 并兼容链接mysql5 亲测可用

java链接MySQL

java链接数据库--Mysql

建议与实现的此类概念的链接-java和mysql [关闭]