Java note (2)-connecting to sqlserver through eclipse

Source: Internet
Author: User
The connection code is as follows: Code
Public class test_sqlserver {
Static connection conn = NULL;
Static statement stmt = NULL;
Static resultset rs = NULL;

Private Static Boolean conn_sqlserver (){
Try {
Class. forname ("com. Microsoft. JDBC. sqlserver. sqlserverdriver ")
. Newinstance ();
String url = "JDBC: Microsoft: sqlserver: // localhost: 1433;
Databasename = test ";
String user = "sa ";
String Password = "sa ";
Conn = drivermanager. getconnection (URL, user, password );
Try {
Stmt = conn. createstatement ();
System. out. println ("connection successful! ");
Return true;
}
Catch (SQLException s_e ){
System. out. println ("s_E" + s_e.getMessage (). toString ());
Return false;
}
}
Catch (SQLException s_e ){
System. out. println ("connection error! SQLException "+ s_e.toString ());
Return false;
}
}

Private static void ExeSQL (String SQL, int flag ){
Try {
Long begin_ SQL _Ticks = System. currentTimeMillis ();
Boolean SQL _exe = false;
SQL _exe1_stmt.exe cute (SQL );
Long end_ SQL _ticks = system. currenttimemillis ();
If (SQL _exe ){
// Return results if required
If (flag = 1 ){
Rs = stmt. getresultset ();
Rs. First ();
While (! Rs. islast ()){
System. Out. Print (Rs. getstring (1) + "");
System. Out. Print (Rs. getint (2) + "");
System. Out. Print (Rs. getint (3) + "\ n ");
Rs. Next ();
}
Rs. Last ();
System. Out. Print (Rs. getstring (1) + "");
System. Out. Print (Rs. getint (2) + "");
System. Out. Print (Rs. getint (3) + "\ n ");
Long show_ticks = system. currenttimemillis ();
System. Out. println ("show total time consumption:" + (show_Ticks-end_ SQL _Ticks) + "millisecond ");
}
}
System. Out. println ("SQL statement execution time:" + (end_ SQL _Ticks-begin_ SQL _Ticks) + "millisecond ");
}
Catch (sqlexception s_e ){
System. Out. println (s_e.getmessage ());
}

Install the driver, patch SP4, and connect again. Everything is similar to MySQL. Code
Public static void main (string [] ARGs ){
If (conn_sqlserver ()){
Exesql ("use test;", 2 );
Exesql ("create table test_1 (name varchar (12), age smallint, Id INT)", 2 );
Exesql ("insert into test_1 values ('zs', 21,53535)", 1 );
Exesql ("select * From test_1", 1 );
// ExeSQL ("drop table test_1", 2 );
}

Create a table and insert data. Everything is OK. However, the following error is returned:
[Microsoft] [SQLServer 2000 Driver for JDBC] Unsupported method: ResultSet. first

It seems that JDBC does not support a method-this is a big headache.
Fortunately, after searching, we found that the problem could be solved.
Comprehensive Analysis: parameters included in createStatement () are described as follows:
1. TYPE_FORWORD_ONLY, which can be rolled forward only;
2. TYPE_SCROLL_INSENSITIVE: two-way scrolling, but not timely updating. If the data in the database has been modified, it is not reflected in the ResultSet.
3. TYPE_SCROLL_SENSITIVE, two-way scrolling, and timely tracking of database updates, in order to change the data in the ResultSet.
Therefore, you can solve the problem by changing to createStatement (ResultSet. TYPE_SCROLL_SENSITIVE, ResultSet. CONCUR_UPDATABLE.
Therefore, stmt = conn. createStatement (); is written
Stmt = conn. createStatement (ResultSet. TYPE_SCROLL_SENSITIVE, ResultSet. CONCUR_UPDATABLE); OK.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.