Java源码:[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论坛: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("请输入注册的用户名:");
int id=sc.nextInt();
System.out.println("请输入密码:");
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("注册成功!");
} 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数据库代码:
[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]
|