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

View: 18962|Reply: 0

[Source] Java's PreparedStatement question mark for reference

[Copy link]
Posted on 11/20/2014 1:14:01 PM | | |
Java source code: [mw_shl_code=java,true]import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Scanner;


public class PreparedStatement {

        /**
         * @param args
         * IT Forum:www.itsvse.com
         */
        public static void main(String[] args) {
                // TODO Auto-generated method stub
                Connection conn=null;
                java.sql.PreparedStatement pstmt =null;
                ResultSet rs=null;
                try {
                        Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
                        conn=DriverManager.getConnection("jdbc:sqlserver://localhost:1433; database=sqlDB; ", "sa", "123");
                        Scanner sc=new Scanner(System.in);
                        System.out.println("Please enter your registered username:");
                        int id=sc.nextInt();
                        System.out.println("Please enter password:");
                        String pwd=sc.next();
                        String sql="insert into userinfo values (?,?)";
                        pstmt =conn.prepareStatement(sql);
                        pstmt.setInt(1, id);
                        pstmt.setString(2, pwd);
                        pstmt.executeUpdate();
                        System.out.println("Registration Successful!") );
                       
                       
                } 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(pstmt!=null){pstmt.close(); }
                                if(conn!=null){conn.close(); }
                        } catch (SQLException e) {
                                // TODO Auto-generated catch block
                                e.printStackTrace();
                        }
                       
                       
                }

        }

}
[/mw_shl_code]

SQL Server database code:
[mw_shl_code=sql,true]create database sqlDB
go
use sqlDB
go

create table userinfo(
        id int primary key,
        pwd varchar(10)
)
go
select * from userinfo[/mw_shl_code]




Previous:Heaven-defying QQ breakthrough account main display plus friends + simple code breakthrough main display forced chat (only QQ number is needed to do whatever you want)
Next:javajdbc knowledge points
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