我的一个Java程序

作者 : admin 于 2008-10-03 17:42:56 标签: ,
2008
10-3

PHP总有其局限性,比如解释型语言在速度上的弱点,制约了它在效率方面的发挥。facebook开放了源代码,其底层大都是c来编写,而我现在计划用Java为一些服务写后台,这是我的第一个Java程序,希望大家多批评,虽然上边依然有太多php的影子。

  1. import java.sql.*;
  2. /*
  3.  *  Java Mysql数据库连接类
  4.  *  我的第一个java程序
  5.  */
  6. public class MysqlConn
  7. {
  8. private String dsn = "jdbc:mysql://localhost:3306/test";
  9. private String username = "root";
  10. private String password = "123456";
  11.  
  12. private Connection conn = null;
  13. private Statement stmt = null;
  14. private ResultSet rs = null;
  15.  
  16.  
  17.  
  18.  
  19.     public void MysqlConn()
  20. {
  21. }
  22.     //初始化连接参数
  23. public void SetDsn( String Dsn )
  24. {
  25. this.dsn = Dsn;
  26. }
  27. //初始化用户名密码
  28. public void SetUserPass( String Username , String Password )
  29. {
  30. this.username = Username;
  31. this.password = Password;
  32. }
  33. //连接函数
  34. public void Conn()
  35. {
  36. try
  37. {
  38. //加载Connetc/J驱动
  39. //Class.forName("com.mysql.jdbc.Driver");
  40. Class.forName("org.gjt.mm.mysql.Driver");
  41. //开始连接
  42. this.conn = DriverManager.getConnection( this.dsn , this.username , this.password );
  43. this.stmt = this.conn.createStatement( ResultSet.TYPE_SCROLL_SENSITIVE , ResultSet.CONCUR_UPDATABLE );
  44. }
  45. catch(SQLException ex)
  46. {
  47. System.out.println("Error : " + ex.toString());
  48. System.out.println("没有找到JDBC/ODBC驱动程序!");
  49. System.exit(0);
  50. }
  51. catch(Exception e)
  52. {
  53. System.out.println("Errors: " + e.toString());
  54. System.exit(0);
  55. }
  56. }
  57.     //返回结果集
  58. public ResultSet sqlQuery( String sql )
  59. {
  60. try
  61. {
  62. this.rs = this.stmt.executeQuery( sql );
  63. }
  64. catch (SQLException ex)
  65. {
  66. System.out.println("Error : " + ex.toString());
  67. System.exit(0);
  68. }
  69. catch (Exception ex)
  70. {
  71. System.out.println("Error : " + ex.toString());
  72. System.exit(0);
  73. }
  74. return rs;
  75. }
  76. //执行 update,insert之类
  77. public void Exec( String sql )
  78. {
  79. try
  80. {
  81. this.stmt.executeUpdate( sql )
  82. }
  83. catch (SQLException ex)
  84. {
  85. System.out.println("Error : " + ex.toString());
  86. System.exit(0);
  87. }
  88. catch (Exception ex)
  89. {
  90. System.out.println("Error : " + ex.toString());
  91. System.exit(0);
  92. }
  93. }
  94.  
  95. }

发表评论




XHTML:你可以使用的标签: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

(若看不到验证码,请重新加载页面。)