Gui Guan’s BLOG

There’s more than one way to do it

Archive for the ‘Java’ Category

Age 1.0beta1

Here is my first Robocode robot - Age.

Age_1.0beta1.png

No need to have an introduction, just have a try if you want. BTW, it uses anti-gravity motion.

  • 3 Comments
  • Filed under: Java
  • Java simulation for Brownian motion

    RacingParticles

    Here is the Java program written by me to simulate the Brownian motion. In this prac, I tried to use double buffering to improve the performances, but actually it is triple buffering. That’s because the JPane has already implemented the double buffering, so plus mine own copy of buffer to store the background coordinate system and the path of particles, it is triple! Ho ho :P

  • 0 Comments
  • Filed under: Java, Life
  • repaint(), paint() and update()

    Many Java programmers are befuddled by the three methods repaint(), paint(Graphics), and update(Graphics). This is because they are designed to work in a wide variety of circumstances, and they interact in a non-obvious fashion. This happens in several contexts in Java, but GUIs are the most obvious. The designers of Java wanted Java programs to be able to run on any machine that had a Java VM. So, a particular program might be running on a desktop machine, or a laptop, or a hand-held machine, like a personal assistant or a phone. This presents quite a challenge for the designer of an abstract windows toolkit (AWT). It also makes the job of a novice programmer more difficult than it might otherwise be. So it goes.

    i) public void update(Graphics)
    By default update(Graphics) fills the drawable area of a Component with its background color, and then sends paint(Graphics) to the object. Thus, flicker that comes from redrawing the background over and over, can sometimes be fixed by overriding update() (see Code Example 9 on page 238, Simple Java).

    ii) public void paint(Graphics)
    Every Java Component implements paint(Graphics), which is responsible for painting that component in the Graphics context passed in the parameter. When you extend a Component (like when you write a Applet), if you want to display it differently than its superclass, you override public void paint(Graphics) . This was first illustrated in Chapter 3, Simple Java. (more…)

  • 0 Comments
  • Filed under: Java
  • FAQ about Java import

    1. Q: Does importing all classes in a package make my object file (.class or .jar) larger?

    A: No, import only tells the compiler where to look for symbols.

    2. Q: Is it less efficient to import all classes than only the classes I need?

    A: No. The search for names is very efficient so there is no effective difference.

    3. Q: Doesn’t it provide better documentation to import each class explicitly? (more…)

  • 2 Comments
  • Filed under: Java
  • Last week, during the prac 2 of CS IB, we were asked to program in Java to build a simple client/server system in which the server takes “commands” from the client and executes them. Due to the time restriction, we were only asked to implement the server, and use telnet to act as client to transact with server. You can have a look here for details.

    1
    2
    3
    4
    5
    
    ServerSocket server = new ServerSocket(6666);
    Socket s= server.accept();
    // We now have a client on the end of the socket s
    BufferedReader in = new BufferedReader( new InputStreamReader( s.getInputStream()));
    PrintWriter out = new PrintWriter(s.getOutputStream());

    It’s no more than the knowledge above. Maybe the simplest nextwork program in Java world.

    The telnet on lab’s Mac just work well, but after I brought my program to home and run on my own computer, I met a really troublesome problem with the Microsoft’s telnet in Windows XP.

    1. First, I started my server.

    telnet_1.png (more…)

    根本原因:

    访问数据库的用户有登录权限,但无操作表的权限

    解决办法:

    1. 在[企业控制台]窗口–[树]子窗口–[安全性]子树–[登录]项里将你使用的登陆用户的默认数据库设为你所使用的数据库。
    2. 在[企业控制台]窗口–[树]子窗口–[安全性]子树–[登录]项里新增一个登录用户(在其中选择SQL Server 身份验证、服务器角色和要访问的数据库),以后便可用些新增用户访问你勾选的数据库了。

    大家在用java连接ms sql时可能会这样的问题
    原因是sa用户为系统用户,它虽然能够登陆数据库,但是数据库里边的权限都是dbo的,所以,我们现在为这个数据库重新建立一个用户.
    当然,这个情况只发生在java里边. .net是不会这样的.
    只要新建立一个用户,再在权限那分配权限做好就行了.

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    
    public Connection getConnectionDB()
    {
    	String driverName = "com.microsoft.jdbc.sqlserver.SQLServerDriver";
    	String ConnectionString = "jdbc:microsoft:sqlserver://localhost:1433; DatabaseName=test;";
    	String userName = "username";
    	String userPwd = "password";
     
    	try {
    		Class.forName(driverName);
    		con = DriverManager.getConnection(ConnectionString, userName, userPwd);
    		//System.out.println("Connection Successful!");
    	} catch (Exception e) {
    		e.printStackTrace();
    	}
     
    	return con;
    }
     
    	ConnectionDB prod=new ConnectionDB();
    	Connection con=prod.getConnectionDB();
    	CallableStatement callStmt =null;
    	java.sql.Statement  stat  =  null;
    	java.sql.ResultSet  rs  =  null;
     
    	stat  =  con.createStatement();
    	String  sql="select * from table";
    	rs=stat.executeQuery(sql);