public void init(){ add(Vis); add(Er); openCon(); exceSql(Sql); closeCon(); } } 这个可以看见:public class SimpleQuery { /** * <p>Main entry point for the application */ public static void main(String args[]) { try { // Perform the simple query and display the results performQuery(); } catch (Exception ex) { ex.printStackTrace(); } }
public static void performQuery() throws Exception { // The name of the JDBC driver to use String driverName = "sun.jdbc.odbc.JdbcOdbcDriver";
// The JDBC connection URL String connectionURL = "jdbc:odbc:MyAccess";
// The JDBC Connection object Connection con = null;
// The JDBC Statement object Statement stmt = null;
// The SQL statement to execute String sqlStatement ="SELECT * FROM T_CUSTOMER";
// The JDBC ResultSet object ResultSet rs = null;
try { System.out.println("Registering " + driverName); // Create an instance of the JDBC driver so that it has // a chance to register itself Class.forName(driverName);
System.out.println("Connecting to " + connectionURL);
// Create a new database connection. We're assuming that // additional properties (such as username and password) // are not necessary con = DriverManager.getConnection(connectionURL);
// Create a statement object that we can execute queries // with stmt = con.createStatement();
// Execute the query rs = stmt.executeQuery(sqlStatement);
// Process the results. First dump out the column // headers as found in the ResultSetMetaData ResultSetMetaData rsmd = rs.getMetaData();
int columnCount = rsmd.getColumnCount();
System.out.println(""); String line = ""; for (int i = 0; i < columnCount; i++) { if (i > 0) { line += ", "; }
// Note that the column index is 1-based line += rsmd.getColumnLabel(i + 1); } System.out.println(line);
// Count the number of rows int rowCount = 0;
// Now walk through the entire ResultSet and get each // row while (rs.next()) { rowCount++;
// Dump out the values of each row line = ""; for (int i = 0; i < columnCount; i++) { if (i > 0) { line += ", "; } // Note that the column index is 1-based line += rs.getString(i + 1); } System.out.println(line); }
// Always clean up properly! if (rs != null) { rs.close(); } if (stmt != null) { stmt.close(); } if (con != null) { con.close(); } } } }
又是applet。 applet連數據必須配安全策略文件
con = DriverManager.getConnection(connectionURL); 这个你没有设置用户名和密码两个参数,即使你的数据源没设密码,也应该写个"",所以应该是: con = DriverManager.getConnection(connectionURL,"",""); 对了,不要用Applet来访问数据库,那个不能访问.
con = DriverManager.getConnection(connectionURL,"",""); 写了好像也不行的, applet不能用jdbc吗,不会吧!
public class dis extends Applet{
TextArea Vis =new TextArea(12,100);
TextField Er=new TextField(100);
String DriveName="sun.jdbc.odbc.JdbcOdbcDriver";
String DriveUrl="jdbc:odbc:MyAccess";
String Sql="select * from Jcustomer";
Connection con=null;
Statement sta=null;
ResultSet rs=null;
public void openCon(){
try{
System.out.println(DriveUrl);
Vis.setText(DriveUrl);
Class.forName(DriveName).newInstance();
Vis.setText(DriveName);
System.out.println(DriveName);
con = DriverManager.getConnection(DriveUrl);
Er.setText("OOOOOOOOOOOOOOOOOOOk");
}
catch(Exception e){
handleException(e);
System.out.println(DriveName);
// Er.setText("MNOOOOOOOOOOOOOOOOOOOk");
}
}
public void closeCon(){
try{
con.close();
}
catch(Exception e){
handleException(e);
}
}
public void exceSql(String com){
ResultSetMetaData Met;
try{
sta=con.createStatement();
rs=sta.executeQuery(Sql);
Met=rs.getMetaData();
int columnCount=Met.getColumnCount();
Vis.setText("");
while(rs.next()){
for(int i=1;i<=columnCount;i++){
String colValue=rs.getString(i);
if(colValue==null)
{ colValue="";}
Vis.append(colValue+";");
}
Vis.append("\n");
}
}catch(Exception e){
handleException(e);
}
}
public void handleException(Exception e){
Er.setText("ErrorSS:"+e.getMessage());
// e.printStackTrace();
if(e instanceof SQLException){
while((e=((SQLException)e).getNextException())!=null){
System.out.println(e);
}
}
}
public void init(){
add(Vis);
add(Er);
openCon();
exceSql(Sql);
closeCon();
}
}
这个可以看见:public class SimpleQuery
{
/**
* <p>Main entry point for the application
*/
public static void main(String args[])
{
try {
// Perform the simple query and display the results
performQuery();
}
catch (Exception ex) {
ex.printStackTrace();
}
}
public static void performQuery() throws Exception
{
// The name of the JDBC driver to use
String driverName = "sun.jdbc.odbc.JdbcOdbcDriver";
// The JDBC connection URL
String connectionURL = "jdbc:odbc:MyAccess";
// The JDBC Connection object
Connection con = null;
// The JDBC Statement object
Statement stmt = null;
// The SQL statement to execute
String sqlStatement ="SELECT * FROM T_CUSTOMER";
// The JDBC ResultSet object
ResultSet rs = null;
try {
System.out.println("Registering " + driverName);
// Create an instance of the JDBC driver so that it has
// a chance to register itself
Class.forName(driverName);
System.out.println("Connecting to " + connectionURL);
// Create a new database connection. We're assuming that
// additional properties (such as username and password)
// are not necessary
con = DriverManager.getConnection(connectionURL);
// Create a statement object that we can execute queries
// with
stmt = con.createStatement();
// Execute the query
rs = stmt.executeQuery(sqlStatement);
// Process the results. First dump out the column
// headers as found in the ResultSetMetaData
ResultSetMetaData rsmd = rs.getMetaData();
int columnCount = rsmd.getColumnCount();
System.out.println("");
String line = "";
for (int i = 0; i < columnCount; i++) {
if (i > 0) {
line += ", ";
}
// Note that the column index is 1-based
line += rsmd.getColumnLabel(i + 1);
}
System.out.println(line);
// Count the number of rows
int rowCount = 0;
// Now walk through the entire ResultSet and get each
// row
while (rs.next()) {
rowCount++;
// Dump out the values of each row
line = "";
for (int i = 0; i < columnCount; i++) {
if (i > 0) {
line += ", ";
}
// Note that the column index is 1-based
line += rs.getString(i + 1);
}
System.out.println(line);
}
System.out.println("" + rowCount + " rows, " +
columnCount + " columns");
}
finally {
// Always clean up properly!
if (rs != null) {
rs.close();
}
if (stmt != null) {
stmt.close();
}
if (con != null) {
con.close();
}
}
}
}
applet連數據必須配安全策略文件
这个你没有设置用户名和密码两个参数,即使你的数据源没设密码,也应该写个"",所以应该是:
con = DriverManager.getConnection(connectionURL,"","");
对了,不要用Applet来访问数据库,那个不能访问.
写了好像也不行的,
applet不能用jdbc吗,不会吧!
public static void main(String[] arg)
{ Connection conn=null;
ResultSet rs=null;
Statement stmt=null;
String strTmpUser="";
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
String url="jdbc:odbc:jeah_data_2";
conn=DriverManager.getConnection(url,"","");
stmt=conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE); String SQL="SELECT * FROM 书库表";
rs=stmt.executeQuery(SQL);
while(rs.next())
{
strTmpUser=rs.getString("书号");
System.out.print(" "+strTmpUser);
strTmpUser=rs.getString("书名");
System.out.print(" "+strTmpUser);
strTmpUser=rs.getString("作者");
System.out.print(" "+strTmpUser);
strTmpUser=rs.getString("体裁");
System.out.print(" "+strTmpUser);
strTmpUser=rs.getString("价格");
System.out.println(" "+strTmpUser);
}
rs.close();
stmt.close();
conn.close(); }catch(Exception e){e.printStackTrace();System.out.println("shit");}
}
}这是我连数据库的一个测试程序。