The reason for this problem is that the class can not be found. The following code is normal. It can run in JDK1.6. Oracle has canceled the ODBC bridge after JDK1.6. The solution is to change another driver mode. The current SQL Server driver is com.microsoft.sqlserver.jdbc.sqlserverdriver, and the connection string is jdbc:sqlserver :// localhost:1433; Databasename = database name
/**
* Author:C
* Date: 03/15/2020
* Demonstrate the use of jdbc-odbc bridge to operate the database
* 1. Configure the data source
* 2. Connect to the data source in the program
*/
package com.beekc.www;
import java.sql.*;
public class Odbc {
public static void main(String[] args) {
//2
Connection ct = null;
//3
Statement sm = null;
try {
//1. Load the driver
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
//2. Get the connection [specify which data source to connect to, username, password]
//If you choose Windons NT authentication when configuring the data source, you don't need to fill in the username and password
//Connection ct = DriverManager.getConnection("jdbc:odbc:beekc");
ct = DriverManager.getConnection("jdbc:odbc:beekc", "sa", "admin...") ;
//3. Create a Statement or PreparedStatement
//Statement use: mainly used to send sql statements to the database
sm = ct.createStatement();
//4. Execute (crud, create database, backup database, delete data...)
//1. Add a data to
//executeUpdate can perform cud operation, put back int type
//int i = sm.executeUpdate("insert into stu values('000013','Wu with','Three Kingdoms')");
//if (i == 1)
//{
//System.out.println("Added successfully");
///}else{
//System.out.println("Failed to add");
//}
//2. Query
//ResultSet result set, you can understand the ResultSet as a result set of table rows
ResultSet rs = sm.executeQuery("select * from stu");
//rs points to the previous row of the first row of the result set
//loop out
while (rs.next())
{
String str1 = rs.getString(1);
String str2 = rs.getString(2);
String str3 = rs.getString(3);
System.out.println("sno:" + str1 + "sname:" + str2 + "address:" + str2 );
}
}catch (Exception e){
e.printStackTrace();
}finally {
//Close resources
//Close order, create first, close first
try{
if(sm != null)
{
sm.close();
}
if (ct != null)
{
ct.close();
}
}catch (Exception e){
e.printStackTrace();
}
}
}
}
Similar Posts:
- Logstash output jdbc Error: java.lang.IllegalAccessError: tried to access class com.mysql.jdbc.EscapeProcessor from class com.mysql.jdbc.ConnectionImpl
- [Solved] Operation not allowed after ResultSet closed
- The jdbc driver imported from idea into MySQL appears “Java. Lang. classnotfoundexception: com. Mysql. CJ. JDBC. Driver”
- Java.sql.sqlexception: an analysis of the causes of exhausted resultset
- [Solved] JDBC connection to SQL Service reported an error: “the driver cannot establish a secure connection with SQL server by using SSL encryption“
- [Solved] Failed to bind properties under ” to com.zaxxer.hikari.Hikari DataSource Spring Boot
- [Solved] java.util.MissingResourceException: Can’t find bundle for base name db, locale zh_CN
- IO exception: socket read timed out always appears in Oracle program
- JAVA set to determine the null: List.isEmpty() & null == List && List.size()==0
- [Solved] Mybatis-config Error: Cannot load connection class because of underlying exception: com.mysql.cj.exceptions.WrongArgumentException: failed to parse the connection string near ‘;useUnicode=true&