dbtest.java

来自「sturts好例子」· Java 代码 · 共 41 行

JAVA
41
字号
package com.db;

import javax.naming.*;
import javax.sql.*;
import java.sql.*;

public class DBTest {
	String foo = "Not Connected";
	int bar = -1;
	public void init() {
	try{
		Context ctx = new InitialContext();
		if(ctx == null ) 
			throw new Exception("Boom - No Context");
		DataSource ds =	(DataSource)ctx.lookup("java:comp/env/jdbc/TestDB");
		if (ds != null) {
			Connection conn = ds.getConnection();
		if(conn != null) {
			foo = "Got Connection "+conn.toString();
		
		Statement stmt = conn.createStatement();
		ResultSet rst =	stmt.executeQuery(" select id from message ");
		if(rst.next()) {
			foo=rst.getString(1);
			bar=208;
		}
		conn.close();
		}
		}
	}catch(Exception e) {
		System.out.println("connecting error");
		e.printStackTrace();
	}
	}

	
	public String getFoo() { return foo; }
	public int getBar() { return bar;}

}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?