• 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);
        }
      }

    }

  • 相关阅读:
    Python自定义:粒子群优化算法
    deap实战_2017中国数学建模大赛_B题_第二题
    deap实战_2017中国数学建模大赛_B题_第二题
    webpack学习笔记(一) 核心概念
    webpack学习笔记
    CSS学习笔记(九) 居中方案
    CSS学习笔记(八) 弹性布局
    CSS学习笔记(七) 粘性布局
    CSS学习笔记(六) 定位
    CSS学习笔记
  • 原文地址:https://www.cnblogs.com/czb529514/p/7967837.html
Copyright © 2020-2023  润新知