"Templates" connect to a database in Java

Source: Internet
Author: User
Tags sybase sybase database


Import Java.sql.connection;import java.sql.drivermanager;import java.sql.resultset;import java.sql.Statement; Import Org.junit.test;import Com.mysql.jdbc.driver;public class Dbhelloworld {@Test public void Testsql () throws Exce ption{//1 Load Connector (driver)----access to MySQL database//class.forname ("Com.mysql.jdbc.Driver"); after//★jdbc4.0, connector (drive) loading can be omitted,      The jar package has already helped us do the action//★ load Connector (drive) mode 2----Technically, worse, because it exists class dependent//drivermanager.registerdriver (New Driver ());   2 Declare the database connected to which machine//string URL = "Jdbc:mysql://localhost:3306/hncu"; String url = "Jdbc:mysql://localhost:3306/hncu?useunicode=true&characterencoding=utf-8";      ★ Specify the connection code//3 establish the connection (with the connection string and user name, password and other information, connect the database) Connection con = drivermanager.getconnection (URL, "root", "1234");   System.out.println (con);      4 manipulating the contents of the database Statement st = Con.createstatement ();   String sql = "INSERT into stud (Id,sname,age,score) VALUES (1010, ' Ted ', 25,90)";      St.execute (SQL);   String sql = "SELECT * from Stud"; ResultSet rs = St.executequerY (SQL);   while (Rs.next ()) {Integer id = rs.getint ("id");   String name = rs.getstring ("sname");   Integer age = Rs.getint ("Age");   Double score = rs.getdouble ("score"); String Dept = rs.getstring (5);      Column number, starting from 1 System.out.println (id+ "," +name+ "," +age+ "," +score+ "," +dept ");   ★ GetObject () can be used to read various data types-----Take all data types of read mode Object ID2 = rs.getobject ("id");   System.out.println ("$$$:" +id2); }   }}

Common Data Connections:  (1) MySQL database       String dirver= "com.mysql.jdbc.Driver";//Drivers        String url= "Jdbc:mysql://localhost:3306/db_name"; //connected Url,db_name for database name        string Username= "UserName"; //user name       string password= "Password" ;   //password       class.forname (Driver). newinstance (); //Load Database driver        connection con=drivermanager.getconnection (Url,username,password);  (2) Microsoft SQL Server Database       String driver= "Com.microsoft.jdbc.sqlserver.SQLServerDriver"; //Driver       String url= "Jdbc:microsoft:sqlserver://localhost:1433;databasename=db_name";        //Connected Url,db_name for database       String username= " Username "; //username       String password=" Password ";   //password       Class.forName (Driver). newinstance ();      connection con= Drivermanager.getconnection (Url,username,password);  (3) Sybase database       String Driver= "Com.sybase.jdbc.sybDriver"; //driver       String url= "jdbc:sybase:// Localhost:5007/db_name ";  //connected Url,db_name to database       String username=" UserName ";  //user name       String password= "Password";   //password        Class.forName (Driver). newinstance ();      connection con= Drivermanager.getconnection (Url,username,password);   (4) Oracle (with thin mode) database        String driver= "Oracle.jdbc.driver.OracleDriver"; //Driver       string url= "jdbc : ORACLE:THIN://LOCALHOST:1521:ORCL ";        //Connected URL,ORCL is the SID of the database       String username="username"; //user name       String password= "Password";   //password        Class.forName (Driver). newinstance ();      connection con= Drivermanager.getconnection (Url,username,password);  (5) using JDBC-ODBC Bridge connection       String Driver= "Sun.jdbc.odbc.JdbcodbcDriver"; //driver       String url= "Jdbc:odbc:dbsource" The Url,dbsource for the  //connection is the data source name       String username= "UserName"; //user name        String password= "Password";   //password       class.forname (Driver ). newinstance ();      connection con=drivermanager.getconnection (Url,username,password) ;

"Templates" connect to a database in Java

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.