• 常用java代码2


    java获取ip地址

    import java.awt.GridLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.net.InetAddress;
    import java.net.UnknownHostException;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    import javax.swing.JTextField;
    
    public class ip extends JFrame
      implements ActionListener
    {
      private static final long serialVersionUID = 3339481369781127417L;
      JButton jb1;
      JButton jb2;
      JButton jb3;
      JPanel jp;
      JLabel jl;
      JLabel jl1;
      JTextField jt;
    
      public ip()
      {
        this.jp = new JPanel();
        this.jl = new JLabel();
        this.jl1 = new JLabel("您的域名:");
        this.jb1 = new JButton("提交");
        this.jb2 = new JButton("重置");
        this.jb3 = new JButton("退出");
        this.jt = new JTextField(20);
        this.jb1.addActionListener(this);
        this.jb2.addActionListener(this);
        this.jb3.addActionListener(this);
        this.jp.setLayout(new GridLayout(3, 2));
        this.jp.add(this.jl1);
        this.jp.add(this.jt);
        this.jp.add(this.jb1);
        this.jp.add(this.jl);
        this.jp.add(this.jb2);
        this.jp.add(this.jb3);
    
        setBounds(200, 200, 500, 240);
        add(this.jp);
        setVisible(true);
        setDefaultCloseOperation(3);
      }
    
      public static void main(String[] args)
      {
        new ip();
      }
    
      public void actionPerformed(ActionEvent e) {
        if (e.getSource() == this.jb1) {
          String url = this.jt.getText();
          InetAddress ip = null;
          try {
            ip = InetAddress.getByName(url);
          }
          catch (UnknownHostException e1) {
            e1.printStackTrace();
          }
          this.jl.setText(ip.toString());
        }
        else if (e.getSource() == this.jb2) {
          this.jl.setText("");
          this.jt.setText("");
        } else {
          System.exit(0);
        }
      }
    }

    java各种数据库连接:

     1 MySQL:    
     2     String Driver="com.mysql.jdbc.Driver";    //驱动程序
     3     String URL="jdbc:mysql://localhost:3306/db_name";    //连接的URL,db_name为数据库名    
     4     String Username="username";    //用户名
     5     String Password="password";    //密码
     6     Class.forName(Driver).new Instance();
     7     Connection con=DriverManager.getConnection(URL,Username,Password);
     8 Microsoft SQL Server 2.0驱动(3个jar的那个):
     9     String Driver="com.microsoft.jdbc.sqlserver.SQLServerDriver";    //连接SQL数据库的方法
    10     String URL="jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=db_name";    //db_name为数据库名
    11     String Username="username";    //用户名
    12     String Password="password";    //密码
    13     Class.forName(Driver).new Instance();    //加载数据可驱动
    14     Connection con=DriverManager.getConnection(URL,UserName,Password);    //
    15 Microsoft SQL Server 3.0驱动(1个jar的那个): // 老紫竹完善
    16     String Driver="com.microsoft.sqlserver.jdbc.SQLServerDriver";    //连接SQL数据库的方法
    17     String URL="jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=db_name";    //db_name为数据库名
    18     String Username="username";    //用户名
    19     String Password="password";    //密码
    20     Class.forName(Driver).new Instance();    //加载数据可驱动
    21     Connection con=DriverManager.getConnection(URL,UserName,Password);    //
    22 Sysbase:
    23     String Driver="com.sybase.jdbc.SybDriver";    //驱动程序
    24     String URL="jdbc:Sysbase://localhost:5007/db_name";    //db_name为数据可名
    25     String Username="username";    //用户名
    26     String Password="password";    //密码
    27     Class.forName(Driver).newInstance();    
    28     Connection con=DriverManager.getConnection(URL,Username,Password);
    29 Oracle(用thin模式):
    30     String Driver="oracle.jdbc.driver.OracleDriver";    //连接数据库的方法
    31     String URL="jdbc:oracle:thin:@loaclhost:1521:orcl";    //orcl为数据库的SID
    32     String Username="username";    //用户名
    33     String Password="password";    //密码
    34     Class.forName(Driver).newInstance();    //加载数据库驱动
    35     Connection con=DriverManager.getConnection(URL,Username,Password);    
    36 PostgreSQL:
    37     String Driver="org.postgresql.Driver";    //连接数据库的方法
    38     String URL="jdbc:postgresql://localhost/db_name";    //db_name为数据可名
    39     String Username="username";    //用户名
    40     String Password="password";    //密码
    41     Class.forName(Driver).newInstance();    
    42     Connection con=DriverManager.getConnection(URL,Username,Password);
    43 DB2:
    44     String Driver="com.ibm.db2.jdbc.app.DB2.Driver";    //连接具有DB2客户端的Provider实例
    45     //String Driver="com.ibm.db2.jdbc.net.DB2.Driver";    //连接不具有DB2客户端的Provider实例
    46     String URL="jdbc:db2://localhost:5000/db_name";    //db_name为数据可名
    47     String Username="username";    //用户名
    48     String Password="password";    //密码
    49     Class.forName(Driver).newInstance();    
    50     Connection con=DriverManager.getConnection(URL,Username,Password);
    51 Informix:
    52     String Driver="com.informix.jdbc.IfxDriver";    
    53     String URL="jdbc:Informix-sqli://localhost:1533/db_name:INFORMIXSER=myserver";    //db_name为数据可名
    54     String Username="username";    //用户名
    55     String Password="password";    //密码
    56     Class.forName(Driver).newInstance();    
    57     Connection con=DriverManager.getConnection(URL,Username,Password);
    58 JDBC-ODBC:
    59     String Driver="sun.jdbc.odbc.JdbcOdbcDriver";
    60     String URL="jdbc:odbc:dbsource";    //dbsource为数据源名
    61     String Username="username";    //用户名
    62     String Password="password";    //密码
    63     Class.forName(Driver).newInstance();    
    64     Connection con=DriverManager.getConnection(URL,Username,Password);
  • 相关阅读:
    VMware VSAN 设计规则
    通过命令行给 XenServer 打补丁
    XenServer 根分区空间满的解决办法
    sftp命令不被识别
    windows cmd窗口提示“telnet”命令不能内部或外部命令,也不是可运行的程序
    Eclipse安装ModelGoon控件(ModelGoon控件反向生成UML)
    WINDOWS8.1安装ORACLE客户端及配置
    CentOs下安装maven
    centos下安装java8
    mono支持gb2312
  • 原文地址:https://www.cnblogs.com/CodeMaker/p/Userful2.html
Copyright © 2020-2023  润新知