一.数据表结构
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.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class JDBCTest {
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";
//访问MySQL数据库的用户名
String username="root";
//访问MySQL的密码
String pwd="XXXXXX";
Connection conn = DriverManager.getConnection(url,username,pwd);
Statement stmt = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE);
String sql="select * from userinfo where username='wangdl' and pwd='wdl'";
ResultSet rs = stmt.executeQuery(sql);
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();
}
}
}