This article is a mirror article of machine translation, please click here to jump to the original article.

View: 16497|Reply: 5

[Communication] Java queries sqlserver database code

[Copy link]
Posted on 11/18/2014 1:01:22 PM | | |
[mw_shl_code=java,true]import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;


public class test2 {

        /**
         * @param args
         */
        public static void main(String[] args) {
                // TODO Auto-generated method stub
                Connection conn=null;
                Statement stmt=null;
                ResultSet rs=null;
                try {
                        Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
                        Get a connection object
                        conn=DriverManager.getConnection(
                                        "jdbc:sqlserver://localhost:1433; database=sqlDB; ", "sa", "123");
                        sql statement
                        String sql="select * from userinfo";
                        Create an object to execute the command
                        stmt=conn.createStatement();
                        Execute SQL statements
                        rs=stmt.executeQuery(sql);
                        while(rs.next()){
                                int id=rs.getInt("id");
                                String name=rs.getString("qname");
                                int age=rs.getInt("age");
                                System.out.println("ID:"+id+",Name:"+name+",Age:"+age);
                        }
                       
                       
                       
                } catch (ClassNotFoundException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                } catch (SQLException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                }finally{
                        try {
                                if(rs!=null){rs.close(); }
                                if(stmt!=null){stmt.close(); }
                                if(conn!=null){conn.close(); }
                        } catch (SQLException e) {
                                // TODO Auto-generated catch block
                                e.printStackTrace();
                        }
                }
        }

}
[/mw_shl_code]

Database script:

[mw_shl_code=sql,true]create database sqlDB
go
use sqlDB
go

create table userinfo(
        id int primary key,
        qname varchar(10),
        age int
)
go

select * from userinfo[/mw_shl_code]

Java jdbc package: sqljdbc.jar (300.98 KB, Number of downloads: 0, Selling price: 1 Grain MB)




Previous:Java updates database code (adds, deletes, changes), and JDBC updates code
Next:The two of them are engaged in the foundation: they don't give me a dime, they don't give me 100 yuan
Posted on 11/18/2014 3:52:41 PM |
If there is a bug in closing a resource, the closing result set is abnormal, and closing objects and connections will not be executed.
Posted on 11/18/2014 3:52:47 PM |
If there is a bug in closing a resource, the closing result set is abnormal, and closing objects and connections will not be executed.
 Landlord| Posted on 11/18/2014 6:40:12 PM |
Delver_Si Posted on 2014-11-18 15:52
If there is a bug in closing a resource, the closing result set is abnormal, and closing objects and connections will not be executed.

How should that be solved?
Posted on 11/18/2014 6:46:08 PM |
admin posted on 2014-11-18 18:40
How should that be solved?

Try catch every close
 Landlord| Posted on 11/18/2014 6:54:19 PM |

Speechless, this is what He Ke taught us to write
Disclaimer:
All software, programming materials or articles published by Code Farmer Network are only for learning and research purposes; The above content shall not be used for commercial or illegal purposes, otherwise, users shall bear all consequences. The information on this site comes from the Internet, and copyright disputes have nothing to do with this site. You must completely delete the above content from your computer within 24 hours of downloading. If you like the program, please support genuine software, purchase registration, and get better genuine services. If there is any infringement, please contact us by email.

Mail To:help@itsvse.com