如何用Java实现数据库查询

如何用Java实现数据库查询,第1张

import java.sql.*

public class MSSQLText

{

public static void main(String args[])

{

String url="jdbc:microsoft:sqlserver://localhost:1433DatabaseName=Northwind"

String user="sa"//这里替换成你自已的数据库用户名

String password="sa"//这里替换成你自已的数据库用户密码

String sqlStr="select CustomerID, CompanyName, ContactName from Customers"

try

{ //这里的异常处理语句是必需的.否则不能通过编译!

Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver")

System.out.println("类实例化成功!")

Connection con = DriverManager.getConnection(url,user,password)

System.out.println("创建连接对像成功!")

Statement st = con.createStatement()

System.out.println("创建Statement成功!")

ResultSet rs = st.executeQuery(sqlStr)

System.out.println(" *** 作数据表成功!")

System.out.println("----------------!")

while(rs.next())

{

System.out.print(rs.getString("CustomerID") + "")

System.out.print(rs.getString("CompanyName") + "")

System.out.println(rs.getString("ContactName"))

}

rs.close()

st.close()

con.close()

}

catch(Exception err){

err.printStackTrace(System.out)

}

}

}

视图

SQL Server

select

a.name AS ViewName,

c.text AS CreateViewSQL

from

sys.views a

LEFT OUTER JOIN

dbo.syscomments c ON a.object_id = c.id

order by

a.name

MySQL

是否是视图 通过 table_type 字段是否为 VIEW 来区分的。

SELECT

table_name AS `视图名`,

table_type AS `类型`,

engine AS `引擎`,

table_comment AS `备注`

FROM

information_schema.tables

WHERE

table_schema = 'test' AND table_type = 'VIEW'

ORDER BY

table_name DESC

存储过程

SQL Server

select

pro.name AS ProcedureName,

c.text AS CreateProcedureSQL

from

sys.procedures pro LEFT OUTER JOIN

dbo.syscomments c ON pro.object_id = c.id

MySQL 里面,查存储过程的,我这里暂时没有。


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/sjk/9892017.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-03
下一篇 2023-05-03

发表评论

登录后才能评论

评论列表(0条)

保存