zoukankan      html  css  js  c++  java
  • 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);
        }
      }

    }

  • 相关阅读:
    /pentest/enumeration/0trace/0trace.sh
    2.2synchronized同步语句块
    2.1synchronized同步方法
    第二章:对象及变量的并发序言
    第一章总结
    1.11守护线程
    1.10线程的优先级
    1.9yield方法
    1.8暂停线程
    微信自动回复
  • 原文地址:https://www.cnblogs.com/czb529514/p/7967837.html
Copyright © 2011-2022 走看看