一.数据表结构
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();
}
}
}