Skip to content

Instantly share code, notes, and snippets.

@cheng470
Last active August 29, 2015 14:14
Show Gist options
  • Save cheng470/905f875a72b7327e121e to your computer and use it in GitHub Desktop.
Save cheng470/905f875a72b7327e121e to your computer and use it in GitHub Desktop.
Java JDBC with mysql
id:1,name:admin,password:admin
id:2,name:zhangsan,password:zhangsan
id:3,name:lisi,password:lisi
-- --------------------------------------------------------
-- Host: 127.0.0.1
-- Server version: 5.6.15-enterprise-commercial-advanced - MySQL Enterprise Server - Advanced Edition (Commercial)
-- Server OS: Win64
-- HeidiSQL Version: 8.0.0.4396
-- --------------------------------------------------------
/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET NAMES utf8 */;
/*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */;
/*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */;
-- Dumping structure for table test.user
DROP TABLE IF EXISTS `user`;
CREATE TABLE IF NOT EXISTS `user` (
`id` int(10) NOT NULL AUTO_INCREMENT,
`name` varchar(50) NOT NULL,
`password` varchar(32) NOT NULL,
`email` varchar(32) NOT NULL,
`mobile` varchar(32) NOT NULL,
`status` tinyint(4) NOT NULL,
`createtime` datetime NOT NULL,
`changetime` datetime NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8;
-- Dumping data for table test.user: ~3 rows (approximately)
DELETE FROM `user`;
/*!40000 ALTER TABLE `user` DISABLE KEYS */;
INSERT INTO `user` (`id`, `name`, `password`, `email`, `mobile`, `status`, `createtime`, `changetime`) VALUES
(1, 'admin', 'admin', 'admin@test.com', '12312341234', 1, '2015-02-07 13:29:52', '2015-02-07 13:29:53'),
(2, 'zhangsan', 'zhangsan', 'zhangsan@test.com', '12312341234', 1, '2015-02-07 13:34:45', '2015-02-07 13:34:45'),
(3, 'lisi', 'lisi', 'lisi@test.com', '12312341234', 1, '2015-02-07 13:45:58', '2015-02-07 13:45:59');
/*!40000 ALTER TABLE `user` ENABLE KEYS */;
/*!40101 SET SQL_MODE=IFNULL(@OLD_SQL_MODE, '') */;
/*!40014 SET FOREIGN_KEY_CHECKS=IF(@OLD_FOREIGN_KEY_CHECKS IS NULL, 1, @OLD_FOREIGN_KEY_CHECKS) */;
/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
package com.test.dao.jdbc;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import org.junit.Test;
public class UserTest {
static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
static final String DB_URL = "jdbc:mysql://localhost/test";
static final String USER = "root";
static final String PASS = "123456";
@Test
public void test() {
Connection con = null;
Statement stmt = null;
ResultSet rs = null;
try {
Class.forName(JDBC_DRIVER);
con = DriverManager.getConnection(DB_URL, USER, PASS);
stmt = con.createStatement();
String sql = "select id, name, password from user";
rs = stmt.executeQuery(sql);
while (rs.next()) {
System.out.print("id:" + rs.getInt("id"));
System.out.print(",name:" + rs.getString("name"));
System.out.println(",password:" + rs.getString("password"));
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
} finally {
try {
if (rs != null) {
rs.close();
}
if (stmt != null) {
stmt.close();
}
if (con != null) {
con.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment