java源代码实例

点到线段的最短距离
Java code
   
<!-- Code highlighting produced by Actipro CodeHighlighter (freeware) http://www.CodeHighlighter.com/ --> private double pointToLine( int x1, int y1, int x2, int y2, int x0, int y0) { double space = 0 ; double a, b, c; a = lineSpace(x1, y1, x2, y2); // 线段的长度 b = lineSpace(x1, y1, x0, y0); // (x1,y1)到点的距离 c = lineSpace(x2, y2, x0, y0); // (x2,y2)到点的距离 if (c <= 0.000001 || b <= 0.000001 ) { space = 0 ; return space; } if (a <= 0.000001 ) { space = b; return space; } if (c * c >= a * a + b * b) { space = b; return space; } if (b * b >= a * a + c * c) { space = c; return space; } double p = (a + b + c) / 2 ; // 半周长 double s = Math.sqrt(p * (p - a) * (p - b) * (p - c)); // 海伦公式求面积 space = 2 * s / a; // 返回点到线的距离(利用三角形面积公式求高) return space; } // 计算两点之间的距离 private double lineSpace( int x1, int y1, int x2, int y2) { double lineLength = 0 ; lineLength = Math.sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2)); return lineLength; }
java各种数据库连接
Java code
   
<!-- Code highlighting produced by Actipro CodeHighlighter (freeware) http://www.CodeHighlighter.com/ --> MySQL: String Driver = " com.mysql.jdbc.Driver " ; // 驱动程序 String URL = " jdbc:mysql://localhost:3306/db_name " ; // 连接的URL,db_name为数据库名 String Username = " username " ; // 用户名 String Password = " password " ; // 密码 Class.forName(Driver). new Instance(); Connection con = DriverManager.getConnection(URL,Username,Password); Microsoft SQL Server 2 .0驱动(3个jar的那个): String Driver = " com.microsoft.jdbc.sqlserver.SQLServerDriver " ; // 连接SQL数据库的方法 String URL = " jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=db_name " ; // db_name为数据库名 String Username = " username " ; // 用户名 String Password = " password " ; // 密码 Class.forName(Driver). new Instance(); // 加载数据可驱动 Connection con = DriverManager.getConnection(URL,UserName,Password); // Microsoft SQL Server 3 .0驱动(1个jar的那个): // 老紫竹完善 String Driver = " com.microsoft.sqlserver.jdbc.SQLServerDriver " ; // 连接SQL数据库的方法 String URL = " jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=db_name " ; // db_name为数据库名 String Username = " username " ; // 用户名 String Password = " password " ; // 密码 Class.forName(Driver). new Instance(); // 加载数据可驱动 Connection con = DriverManager.getConnection(URL,UserName,Password); // Sysbase: String Driver = " com.sybase.jdbc.SybDriver " ; // 驱动程序 String URL = " jdbc:Sysbase://localhost:5007/db_name " ; // db_name为数据可名 String Username = " username " ; // 用户名 String Password = " password " ; // 密码 Class.forName(Driver).newInstance(); Connection con = DriverManager.getConnection(URL,Username,Password); Oracle(用thin模式): String Driver = " oracle.jdbc.driver.OracleDriver " ; // 连接数据库的方法 String URL = " jdbc:oracle:thin:@loaclhost:1521:orcl " ; // orcl为数据库的SID String Username = " username " ; // 用户名 String Password = " password " ; // 密码 Class.forName(Driver).newInstance(); // 加载数据库驱动 Connection con = DriverManager.getConnection(URL,Username,Password); PostgreSQL: String Driver = " org.postgresql.Driver " ; // 连接数据库的方法 String URL = " jdbc:postgresql://localhost/db_name " ; // db_name为数据可名 String Username = " username " ; // 用户名 String Password = " password " ; // 密码 Class.forName(Driver).newInstance(); Connection con = DriverManager.getConnection(URL,Username,Password); DB2: String Driver = " com.ibm.db2.jdbc.app.DB2.Driver " ; // 连接具有DB2客户端的Provider实例 // String Driver="com.ibm.db2.jdbc.net.DB2.Driver"; // 连接不具有DB2客户端的Provider实例 String URL = " jdbc:db2://localhost:5000/db_name " ; // db_name为数据可名 String Username = " username " ; // 用户名 String Password = " password " ; // 密码 Class.forName(Driver).newInstance(); Connection con = DriverManager.getConnection(URL,Username,Password); Informix: String Driver = " com.informix.jdbc.IfxDriver " ; String URL = " jdbc:Informix-sqli://localhost:1533/db_name:INFORMIXSER=myserver " ; // db_name为数据可名 String Username = " username " ; // 用户名 String Password = " password " ; // 密码 Class.forName(Driver).newInstance(); Connection con = DriverManager.getConnection(URL,Username,Password); JDBC - ODBC: String Driver = " sun.jdbc.odbc.JdbcOdbcDriver " ; String URL = " jdbc:odbc:dbsource " ; // dbsource为数据源名 String Username = " username " ; // 用户名 String Password = " password " ; // 密码 Class.forName(Driver).newInstance(); Connection con = DriverManager.getConnection(URL,Username,Password);

你可能感兴趣的:(java,oracle,jdbc,db2,Informix)