JDBC basic

http://www.tutorialspoint.com/jdbc/jdbc-sample-code.htm

maven

<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <version>5.1.6</version>
</dependency>

example code

public class DBUtil {
    public static Connection getConnection() throws ClassNotFoundException,
            SQLException {
        // JDBC driver name and database URL
        final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
        final String DB_URL = "jdbc:mysql://127.0.0.1:3309/lily";

        // Database credentials
        final String USER = "root";
        final String PASS = "123";

        // Register JDBC driver
        // no need for JDBC 4+
        // Class.forName(JDBC_DRIVER);

        // Open a connection
        System.out.println("Connecting to database...");
        Connection conn = DriverManager.getConnection(DB_URL, USER, PASS);
        return conn;
    }

    public static void executeSelect(Connection conn) throws SQLException {
        System.out.println("Creating statement...");
        Statement stmt = conn.createStatement();
        String sql;
        sql = "SELECT id, username FROM users";
        ResultSet rs = stmt.executeQuery(sql);
        // Extract data from result set
        while (rs.next()) {
            // Retrieve by column name
            int id = rs.getInt("id");
            String username = rs.getString("username");

            // Display values
            System.out.print("ID: " + id);
            System.out.print(" username: " + username);
            System.out.println();
        }
        // Clean-up environment
        rs.close();
        stmt.close();
    }

    public static void main(String[] args) throws ClassNotFoundException,
            SQLException {
        Connection conn = getConnection();
        executeSelect(conn);

    }
}
时间: 2025-01-14 03:49:49

JDBC basic的相关文章

Oracle 客户端安装

Oracle 客户端的安装方式一种有两种: 1.Oracle标准客户端   点击下载 这是Oracle提供的标准版11r2的客户端 2.Oracle Database Instant Client(即时客户端)  点击下载 程序包说明 Instant Client 程序包 描述 说明 Basic 运行 OCI.OCCI 和 JDBC-OCI 应用程序所需的所有文件 OCI OCCI JDBC Basic Lite Basic 的精简版本,其中仅包含英文错误消息和 Unicode.ASCII 以及

Oracle Instant Client的安装和使用

转自:https://www.cnblogs.com/chinalantian/archive/2011/09/09/2172145.html 根据自己需求到Oracle网站(http://www.oracle.com/technetwork/database/features/instant-client/index-097480.html)下载相应的Instant Client版本软件包,各软件包的描述如下: Instant Client Package Description Notes

Tomcat使用JDBC Realm配置Basic认证

1. 下载MySQL JDBC驱动(mysql-connector-java-x.x.x-bin.jar)放到TOMCAT_HOME/lib目录下. 2. 配置TOMCAT_HOME/server.xml,添加JDBCRealm,注释掉原来的LockOutRealm. <Realm className="org.apache.catalina.realm.JDBCRealm"       driverName="org.gjt.mm.mysql.Driver"

Atitit.跨语言数据库db&#160;&#160;api兼容性&#160;jdbc&#160;odbc&#160;ado&#160;oledb&#160;增强方案

Atitit.跨语言数据库db  api兼容性 jdbc odbc ado oledb 增强方案 1. 跨语言db api兼容性..1 2. 目前访问数据库的接口很多.比较常用的jdbc odbc 以及c api (php用此接口),共约101个类+128个c函数1 2.1. Jdbc api数量  约60个类..java.sql包下面的..大概几百个方法函数吧..2 2.2. Odbc api数量::约78个,odbc是个非oo的api2 2.3. C api (mysql ): 约50个..

JDBC DataSource Example – Oracle, MySQL and Apache DBCP Tutorial

We have already seen that JDBC DriverManager can be used to get relational database connections. But when it comes to actual programming, we want more than just connections. Most of the times we are looking for loose coupling for connectivity so that

com.microsoft.sqlserver.jdbc.SQLServerException: 用户 &#39;sa&#39; 登录失败。

com.microsoft.sqlserver.jdbc.SQLServerException: 用户 'sa' 登录失败. at com.microsoft.sqlserver.jdbc.SQLServerException.makeFromDatabaseError(SQLServerException.java:196) at com.microsoft.sqlserver.jdbc.TDSTokenHandler.onEOF(tdsparser.java:246) at com.micr

RAC JDBC 配置

我们都知道,ORACLE 对外提供服务有 这么几种方式 THIN CONNECT ,OCI CONNECT ,DATALINK等数据连接方式. 1.THIN Connect 在单实例情况下,JDBC Connect String : jdbc:oracle:thin:10.11.1.243:1521/orcl 其中IP 10.11.1.243:1521 服务器IP地址和Lisener监听端口. orcl 为数据服务名. 在RAC环境中,JDBC Connect String : jdbc:ora

SoapUI Pro Project Solution Collection-change the JDBC Request behavior

change the jdbc request : 1.change the driver name,connection string,query string or assert. the object we will use here is : JdbcRequestTestStep TestCase newcase=testRunner.getTestCase(); JdbcRequestTestStep step1=(JdbcRequestTestStep) newcase.getTe

数据库 —— 使用JDBC操作数据库

[Link] http://www.developer.com/java/data/manipulating-a-database-with-jdbc.html Manipulating a Database with JDBC March 26, 2014 By Manoj Debnath Bio » Send Email » More Articles » Java programs communicate with the database and manipulate its data