package Border;
import java.sql.*; //完成環境設定,匯入java.sql包
public class dbconn {
public dbconn()
{
}
//declare variable
private Connection conn = null;
private ResultSet rs = null
private String server = "127.0.0.1"; // Can't use localhost , you must use IP or CNAME
private String port = "3306"; //change to your port
private String db = "publish"; //change to your db name
private String user = "root"; //change to your username
private String pass = "root"; //change to your password
private String drivername="org.gjt.mm.mysql.Driver"; //mysql driver
private String URL="jdbc:mysql://"+server+":"+port+"/"+db+"?useUnicode=true&characterEncoding=GBK&user="+user+"&password="+pass;
Statement stmt = conn.createStatement();
stmt.executeUpdate("INSERT INTO book (title,price) VALUES ('"+tit+"','"+prc+"')");
%>
<jsp:forward page="text.jsp" />
6. 提供幾個函數:
(1). getConnection
public static Connection getConnection(String url,
String user,
String password)
throws SQLException
Attempts to establish a connection to the given database URL. The DriverManager attempts to select an appropriate driver from the set of registered JDBC drivers.
Parameters:
url - a database url of the form jdbc:subprotocol:subname
user - the database user on whose behalf the connection is being made
password - the user's password
Returns:
a connection to the URL
Throws:
SQLException - if a database access error occurs
(2). createStatement
public Statement createStatement()
throws SQLException
Creates a Statement object for sending SQL statements to the database. SQL statements without parameters are normally executed using Statement objects. If the same SQL statement is executed many times, it may be more efficient to use a PreparedStatement object.
Result sets created using the returned Statement object will by default be type TYPE_FORWARD_ONLY and have a concurrency level of CONCUR_READ_ONLY.
Returns:
a new default Statement object
Throws:
SQLException - if a database access error occurs
(3). executeQuery
public ResultSet executeQuery(String sql)
throws SQLException
Executes the given SQL statement, which returns a single ResultSet object.
Parameters:
sql - an SQL statement to be sent to the database, typically a static SQL SELECT statement
Returns:
a ResultSet object that contains the data produced by the given query; never null
Throws:
SQLException - if a database access error occurs or the given SQL statement produces anything other than a single ResultSet object
(4). getString
public String getString(String columnName)
throws SQLException
Retrieves the value of the designated column in the current row of this ResultSet object as a String in the Java programming language.
Parameters:
columnName - the SQL name of the column
Returns:
the column value; if the value is SQL NULL, the value returned is null
Throws:
SQLException - if a database access error occurs
(5). next
public boolean next()
throws SQLException
Moves the cursor down one row from its current position. A ResultSet cursor is initially positioned before the first row; the first call to the method next makes the first row the current row; the second call makes the second row the current row, and so on.
If an input stream is open for the current row, a call to the method next will implicitly close it. A ResultSet object's warning chain is cleared when a new row is read.
Returns:
true if the new current row is valid; false if there are no more rows
Throws:
SQLException - if a database access error occurs