Нужна помощь с кодом ниже.
Я пытаюсь сделать запрос из той же таблицы и обновить запись.
import java.sql.* ;
class Connect
{
public static void main( String args[] )
{
try
{
// Load the database driver
Class.forName( "com.microsoft.sqlserver.jdbc.SQLServerDriver" ) ;
// Get a connection to the database
Connection conn = DriverManager.getConnection( "jdbc:sqlserver://localhost:1433;user=sa;password=root;databaseName=blueprint") ;
// Print all warnings
for( SQLWarning warn = conn.getWarnings(); warn != null; warn = warn.getNextWarning() )
{
System.out.println( "SQL Warning:" ) ;
System.out.println( "State : " + warn.getSQLState() ) ;
System.out.println( "Message: " + warn.getMessage() ) ;
System.out.println( "Error : " + warn.getErrorCode() ) ;
}
// Prepare a statement
String sql = "select DN, WD from table1 ";
Statement cs = conn.createStatement();
// Execute the query
ResultSet rs = cs.executeQuery(sql) ;
// Loop through the result set
int i = 0;
while( rs.next() )
System.out.println( rs.getString("DN") + " " + rs.getString("WD") ) ;
String dm = rs.getString("DN");
String wi = rs.getString("WD");
iupdateQuery(i, dm, wi);
i++;
// Close the result set, statement and the connection
rs.close() ;
cs.close() ;
conn.close() ;
}
catch( SQLException se )
{
System.out.println( "SQL Exception:" ) ;
// Loop through the SQL Exceptions
while( se != null )
{
System.out.println( "State : " + se.getSQLState() ) ;
System.out.println( "Message: " + se.getMessage() ) ;
System.out.println( "Error : " + se.getErrorCode() ) ;
se = se.getNextException() ;
}
}
catch( Exception e )
{
System.out.println( e ) ;
}
}
private void iupdateQuery(int i, String dm, String wi) {
String sdm = dm;
String swi = within;
String query = "UPDATE table1 SET WDIO ='" + swi
+ "' WHERE (DN ='" + sdm + "');";
// debug
System.out.println(i + ". " + query);
try {
Statement update = conn.createStatement();
update.executeUpdate(query);
} catch (Exception e) {
// debug out output this way
System.err.println("Mysql Statement Error: " + query);
e.printStackTrace();
}
}
}
Ошибка ниже, если я выполню.
C:\test>javac Connect.java
Connect.java:48: non-static method iupdateQuery(int,java.lang.String,java.lang
tring) cannot be referenced from a static context
iupdateQuery(i, dm, within);
^
Connect.java:91: cannot find symbol
symbol : variable conn
location: class Connect
Statement update = conn.createStatement();
^
2 errors
Пожалуйста, дайте мне знать, если я что-то делаю неправильно.