一.数据表结构
CREATE TABLE `userinfo` (
`id` bigint(20) NOT NULL AUTO_INCREMENT,
`username` varchar(50) DEFAULT NULL,
`pwd` varchar(50) DEFAULT NULL,
`inDate` datetime DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=15 DEFAULT CHARSET=utf8;
二.示例代码
package cn.wdl.db;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class JDBCPreparedTest {
public static void main(String[] args) {
try {
Class.forName("com.mysql.jdbc.Driver");
String url="jdbc:mysql://127.0.0.1:3306/soft18?characterEncoding=UTF-8";
String username="root";
String pwd="xxxxxx";
Connection conn = DriverManager.getConnection(url,username,pwd);
String sql="select * from userinfo where username=? and pwd=?";
PreparedStatement stmt = conn.prepareStatement(sql);
stmt.setString(1, "wangdl");
stmt.setString(2, "wdl");
ResultSet rs = stmt.executeQuery();
while(rs.next()) {
System.out.print(rs.getInt("id")+"\t"+rs.getString("username")+"\t"+rs.getString("pwd"));
}
}catch(ClassNotFoundException ex) {
ex.printStackTrace();
}catch(SQLException ex1) {
ex1.printStackTrace();
}
}
}