当前位置:首页>开发>正文

java中使用JDBC连接数据库的步骤 求用jdbc 连接数据库的java代码(只连接部分即可)

2023-04-21 17:40:40 互联网 未知 开发

 java中使用JDBC连接数据库的步骤 求用jdbc 连接数据库的java代码(只连接部分即可)

java中使用JDBC连接数据库的步骤?

1.class.forName("驱动所在类及包名") 还需导入jar包
2.java.sql.Connection conn = DriverManager.getConnection(URL(即数据库连接的ulr),USERNAME(对应数据的用户名),PASSWORD(密码))
3.String sql = "SELECT * FROM tableName WHERE id=?"
4.java.sql.PreparedStatement ptmt = conn.getPreparedStatement(sql)
5.ptmt.setInt(1,id值);
6.java.sql.ResultSet rs = ptmt.executeQuery()
7.while(rs.next){
System.out.println(rs.getString("字段名"))
}
8.conn.close()ptmt.close()rs.close()

求用jdbc 连接数据库的java代码(只连接部分即可)

public Connection getConnection(){//事先导入驱动
try{
Class.forname("sun.jdbc.odbc.jdbcodbcDriver")//加载驱动
System.out.println("驱动加载完毕")
}catch(ClassNotFoundException e){
e.printStackTrace()
}
try{
Connecion con=DriverManager.getConnecion("jdbc:odbc:数据库名成","用户名","密码")//创建连接对象,即已经连接到数据库
system.out.println("连接成功!")
}
catch(exception e){
e.printStackTrace()
}
}

如何用jdbc链接数据库

1.在新建的Project中右键新建Floder

2.创建名为lib的包

3.创建完毕之后的工程目录

4.接下来解压你下载的mysql的jar包,拷贝其中的.jar文件

5.在工程lib包下邮件 选择paste即粘贴,把mysql的jar包拷贝进来

6.拷贝完毕如图:

7.在mysql的jar包上右键选择 build path - add to build path

8.添加完毕之后,工程才与Mysql的jar包关联起来,现在可以使用相关类和方法了

9.在工程中新建JdbcTest1.java类

10.输入如下代码:

如何使用JDBC连接数据库在java中

1.找驱动程序,不同的数据库有不同的jdbc驱动,一般官网都有下,不行就baidu,google..注意把驱动程序jar包放到项目classpath里;

2.确定该数据库连接的URL,用户名,密码;

3.写代码(已经知道驱动、url、用户名、密码了):

String url = "xxxx"
String userName = "xxxxx"
String password = "xxxxxx"

Class.forName("com.xx.yyy.Driver")//引号里为驱动程序的类名
Connection conn = DriverManager.getConnection(url, userName, password)

//.......用conn操作数据库

conn.close()//关闭资源,最好放到finally里

请简述Java中如何使用JDBC连接数据库

import java.sql.*  
public class MysqlTest {  
    public static void main(String[] args){  
               // 驱动程序名        
        String driver = "com.mysql.jdbc.Driver"  
               // URL指向要访问的数据库名world        
        String url = "jdbc:mysql://127.0.0.1:3306/world"  
               // MySQL配置时的用户名           
        String user = "root"           
        // MySQL配置时的密码          
        String password = "123456"  
        String name  
                try {               
                 // 加载驱动程序        
                Class.forName(driver)  
                    // 连续数据库       
               Connection conn = DriverManager.getConnection(url, user, password)  
                   if(!conn.isClosed())          
                  System.out.println("Succeeded connecting to the Database!")  
                  // statement用来执行SQL语句             
                     Statement statement = conn.createStatement()  
                 // 要执行的SQL语句           
                   String sql = "select * from city"  
                // 结果集       
                  ResultSet rs = statement.executeQuery(sql)  
                while(rs.next())  {         
               // 选择Name这列数据     
               name = rs.getString("Name")  
                  // 输出结果              
                  System.out.println(rs.getString("CountryCode")   " "   name)           
             }  
         rs.close()       conn.close()  }   
        catch(ClassNotFoundException e) {  
         System.out.println("Sorry,can`t find the Driver!")              
         e.printStackTrace()  
        } catch(SQLException e) {  
         e.printStackTrace()  
        } catch(Exception e) {  
         e.printStackTrace()  
        }   
        }  
}

java如何连接数据库?

通过JDBC(Java DataBase Connectivity),它可以帮你屏蔽掉一些细节。但链接每个不同的数据库其中的操作,不同的语句还是被体现在程序中的。
链接数据库的方法是程式化的,就是那么几个句子。
下面是我自己的一个链接mySQL的代码,你看看
try {
Class.forName("com.mysql.jdbc.Driver")
//这个地方,链接不同的数据库,就加载不同的驱动
Connection conn = DriverManager.getConnection(
"jdbc:mysql://localhost/apriori","root","root")
Statement stmt = conn.createStatement()
ResultSet rs = stmt.executeQuery("select * from info")
while (rs.next()) {
int tid = rs.getInt("tid")
String emItem = rs.getString("icoll")
infos.add(new Info(tid, emItem))
}
} catch (ClassNotFoundException e) {
e.printStackTrace()
} catch (SQLException e) {
e.printStackTrace()
}

最新文章