各位大哥,帮我一把,一个简单的配置问题!在线等!(74分)

  • 主题发起人 主题发起人 may123321203
  • 开始时间 开始时间
M

may123321203

Unregistered / Unconfirmed
GUEST, unregistred user!
我没有用过jbuilder,有没有人可以做一个简单的增删改查询的实例,让我看一下,最好有jbuilder与后台的数据库的配置,因为我不会配。
 
不会吧,就是数据库的配制啊,还有连结啊,可以的啊,你慢慢学啊!
好,我下面演示一下用JDBC访问ACCESS数据库的例子!
import java.sql.*
public class databaseaccess{
public static void main(String[] args){
String dbdriver ="sun.jdbc.odbc.jdbcodbcdriver";
string connstr="jdbc:odbc:Employee";
string sql="select * from person"//update ,delete,insert into ,OK!
Connection conn=null;
ResultSet rs=null;
try{
Class.forName(dbdriver);//add database's driver;
conn=drivermanager.getconnection(connstr)//connect to DBMS
Statement stmt=conn.createStatement();
rs=stmt.executQuery(sql);
System.out.println(" id name province health");
while(rs.next()){
System.out.print(" "+rs.getInt("ID"));
System.out.print(" "+rs.getString("Name"));
S
S//above
}
rs.close;
stmt.close;
}
catch(Exception e){
e.getMessage();
e.printStackTrace();
}
}
}
DataBase Config ,rich man contain! small big/small not spilt!
 
query怎样调用呢,是不是query.executQuery(sql);
就可以了啊
 
S
S//above
}
rs.close;
stmt.close是什么啊
会也错
 
我的是连informix
import java.sql.*;
public class demo2
{
static String connURL;
//the URL to be used for establishing the JDBC
//connection
Connection conn;
// the database connection
ResultSet queryResults;
// the ResultSet containing the rows returned by
// the query
public static void main(String args[])
{
/* if (args.length != 0)
{
connURL = args[0];
demo2 thisDemo = new demo2();
}
else
{
System.out.println("Missing Input!! Please specify the connection URL");
System.out.println("Usage...java demo2 <jdbc_conn_url>");
}
} //end of main method
*/
connURL = "jdbc:informix-sqli://huang:1526/elftech:informixserver=dbper;" +
"user=informix;password=informix";
demo2 thisDemo = new demo2();
}

public demo2()
{
connectToDBServer();
executeQuery();
displayResults();
} // end of demo2 constructor

private void connectToDBServer()
{
try
{
String ifxDriver = "com.informix.jdbc.IfxDriver";
// Register the INFORMIX-JDBC driver
Driver IfmxDrv = (Driver) Class.forName(ifxDriver).newInstance();
// Get a connection to the database server
conn = DriverManager.getConnection(connURL);
System.out.println("Driver loaded...and connection established");
}
catch (Exception e)
{
System.out.println("Could not connect to database server....");
System.out.println(e.getMessage());
}
} // end of connectToDBServer method

private void executeQuery()
{
// The select statement to be used for querying the customer table
String selectStmt = "SELECT * FROM department";
try
{
// Create a Statement object and use it to execute the query
Statement stmt = conn.createStatement();
queryResults = stmt.executeQuery(selectStmt);
System.out.println("Query executed...");
}
catch (Exception e)
{
System.out.println("Could not execute query....");
System.out.println(e.getMessage());
}
} //end of executeQuery method

private void displayResults()
{
try
{
// A place-holder to hold the first row of the result set table
// (headers of the returned columns)
StringBuffer headerRow = new StringBuffer();
StringBuffer divider = new StringBuffer();
// Get ResultSetMataData containing the number, types and properties
// of the columns in the result set
ResultSetMetaData queryResultsMetaData = queryResults.getMetaData();
int numOfCols = queryResultsMetaData.getColumnCount();
// Construct the first row (headers) of the result table and
// display it
for (int i=1;
i<=numOfCols;
i++)
{
headerRow.append(queryResultsMetaData.getColumnLabel(i));
headerRow.append("/t");
}
System.out.println(headerRow);
// Construct the divider and display it
for (int i=0;
i<=headerRow.length();
i++) // to account for the tabs
divider.append('-');
System.out.println(divider);
// Get the values of the result set rows and display them
while (queryResults.next())
{
// A place-holder to hold the data in the current row of the
// result set
StringBuffer dataRow = new StringBuffer();
// Get column data from every column &amp;
display it
for (int i=1;
i<=numOfCols;
i++)
{
dataRow.append(queryResults.getString(i));
dataRow.append("/t");
}
System.out.println(dataRow);
}
}
catch (Exception e)
{
System.out.println("Could not display results....");
System.out.println(e.getMessage());
}
} // end of displayResults method
} // end of class demo2
运行可以通过的
 
多人接受答案了。
 
后退
顶部