gpt4 book ai didi

java - ms sql server 连接上的 java 存在基本问题

转载 作者:行者123 更新时间:2023-12-01 07:13:19 25 4
gpt4 key购买 nike

需要以下代码的帮助。

我正在尝试从同一个表查询并更新记录。

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

如果我做错了什么,请告诉我。

最佳答案

使方法静态,如下所示:

private static void iupdateQuery(int i, String dm, String wi) {
...

关于java - ms sql server 连接上的 java 存在基本问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9503318/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com