Java, XML and Database Programming Practice (Four)

zhaozj2021-02-16  47

Protected Boolean Alreadyin (String Tname, String Colname, String Value)

{

Int result;

ResultSet RST = NULL;

Try {

/ / Execute SQL statement

String query = "SELECT" colname "from" TNAME "Where" colname "= '" value "" "

Statement Statement = connFormax.createStatement ();

RST = Statement.executeQuery (Query);

IF (Rst.Next ())

{

STATEMENT.CLOSE ();

Rst.close ();

Return True;

}

}

Catch (sqlexception sqlex) {

SQLEX.PrintStackTrace ();

Return False;

}

Return False;

}

Protected int GetiDFromNumber (String TName, String Colname, String Value)

{

Int result;

ResultSet RST = NULL;

Try {

Connection conn = drivermanager.getConnection (destpara.getUsername (), destpara.getpassword ());

String query = "SELECT ID," ColName "from" TNAME "Where" colname "= '" value ""

System.out.println (query);

Statement Statement = Conn.createStatement ();

RST = Statement.executeQuery (Query);

IF (Rst.Next ())

{

Return Rst.Getint ("ID");

}

}

Catch (sqlexception sqlex) {

SQLEX.PrintStackTrace ();

Return 0;

}

Return 0;

}

/ **

* Get the largest ID number in a table

* /

protected int getmax (string tname)

{

Int result;

ResultSet RST = NULL;

Try {

/ / Execute SQL statement

String query = "SELECT MAX (ID) from" TNAME;

Statement Statement = connFormax.createStatement ();

RST = Statement.executeQuery (Query);

IF (Rst.Next ())

{

Return Rst.Getint (1) 1;

}

}

Catch (sqlexception sqlex) {

SQLEX.PrintStackTrace ();

Return 0;

}

Return 1;

}

/ **

* Execute a certain SQL statement

* /

Public Static Void Execute (Connpara Connpara, String Stmt) THROWS SQLEXCEPTION {

Connection conn = NULL;

PreparedStatement PS = NULL;

Try {

Conn = drivermanager.getConnection (connpara.getUrname (), connpara.getpassword ();

System.out.println (STMT);

PS = conn.preparestatement (STMT);

ps.executeUpdate ();

} catch (exception e) {

E.PrintStackTrace ();

System.out.println (E.getMessage ());

} finally {

IF (ps! = null) ps.close ();

IF (conn! = null) conn.close ();

}

}

Public static void main (String argc [])

{

Dbinput copydb = new dbinput ();

Copydb.dbinit ();

Copydb.copyproduct ();

}

}

problem:

1) Access database cannot be read directly from JDBC, the solution is to configure Access in the ODBC and then operate the Access database through ODBC.

2) Can't find the com.microsoft.jdbc.sqlser.sqlserverdriver class when executed, because when running this class, you want to download Microsoft's JDBC package, three files in this package: msbase.jar, mssqlser.jar, Msutil .jar, put these three files into in, there will be no problem.

转载请注明原文地址:https://www.9cbs.com/read-24426.html

New Post(0)