Java USES the setAsciiStream method to retrieve the database specified content instance resolution

  • 2020-04-01 03:27:16
  • OfStack

This example shows the instance code for Java to retrieve a database using the setAsciiStream () method. Parameter queries must be assigned to parameters before the SQL statement is executed using methods such as SetBoolean(), SetInt(), SetString(), SetObject(), SetNull(), etc., of the PreparedStatement object. These methods establish a mapping between Java data types and SQL data types. JDBC can use an input stream as an input parameter to an SQL statement, and there are three ways to set the input stream: setAsciiStream(), setUnicodeStream(), and setBinaryStream(). This example USES the setAsciiStream() method, which inputs ASCII code values into parameters of type Longvarchar of SQL. After the query is executed, a ResultSet object is returned that contains the table containing the query results returned by the query statement, and the next record of the recordset can be obtained by using the next() method of the ResultSet object. Use methods such as getInt(), getString, getBoolean(), getByte(), getObject() of the ResultSet object to get the data in the record. Using these methods is based on the need to return the value. The isNull() method is used to determine whether the output parameter isNull. In this example, getString() is used to get the student's name, age, address, and phone information, and the getInt() method is used to get the student's class number.

The procedure implementation steps are as follows:

1. Write the basic framework for the useParameterResultSet class, which contains only the main() method, in which the driver is loaded, a connection to the database is established, a general query is performed on the database, followed by a parameter query, and finally the stored procedure is executed.

2. The complete code of this class is as follows:


//The JDBC class, DriverManager class, and system output are used, so the following package needs to be introduced:
import java.sql.*;
import java.io.*;
//import java.util.*;
class useParameterResultSet
{
public static void main(String argv[])
{
String url="jdbc.odbc:useDSN";
String name,age,address,telephone;
int cno;
java.sql.ResultSet rs;
try
{
//Load driver
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
//Establish a connection
Connection con=DriverManager.getConnection(url,"sa","");
//Create a file input stream
File file=new File("d:/java/usefile.txt");
int flength=2;
InputStream fis=new FileInputStream(file);
//Create a PreparedStatement object
String sqlstr="select * from student where age=?";
PreparedStatement ps=con.prepareStatement(sqlstr);
//Set input parameters
ps.setAsciiStream(1,fis,flength);
//Get the result set
rs=ps.executeQuery();
//Output result set
System.out.println(" Search results: ");
while(rs.next())
{
name=rs.getString("name");
age=rs.getString("age");
cno=rs.getInt("classno");
address=rs.getString("address");
telephone=rs.getString("telephone");
System.out.println(name+" "+age+" "+cno+" "+address+" "+telephone);
}
con.close();
}
catch(Exception e)
{
System.out.println(e.getMessage());
e.printStackTrace();
}
}
}

Interested readers can try out the examples described in this article, which will be helpful for your Java project development.


Related articles: