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

mysql是怎么连接数据库服务器 MySql 中如何连接一列字符串

2023-04-25 00:19:26 互联网 未知 开发

 mysql是怎么连接数据库服务器 MySql 中如何连接一列字符串

mysql是怎么连接数据库服务器

1)连接Oracle 8/8i/9i/10g/11g(thin模式)

Class.forName("oracle.JDBC.driver.OracleDriver").newInstance()
String url="JDBC:oracle:thin:@localhost:1521:orcl" //orcl为Oracle数据库的SID
String user="test"
String password="test"
Connection con=DriverManager.getConnection(url,user,password)
2)连接DB2数据库
Class.forName("com.ibm.db2.jcc.DB2Driver")
String url="JDBC:db2://localhost:5000/testDb"/**数据库连接串**/
String user="test" String password="test"
Connection con=DriverManager.getConnection(url,user,password)
3)连接MySQL数据库
Class.forName("com.mysql.jdbc.Driver")
String url="JDBC:mysql://localhost:8080/testDB"
String user="test" String password="test"
Connection con=DriverManager.getConnection(url,user,password)
4)连接SQL Server数据库
Class.forName("com.microsoft.JDBC.sqlserver.SQLServerDriver")
String url="JDBC:microsoft:sqlserver://localhost:1433DatabaseName=testDb"
String user="test" String password="test"
Connection con=DriverManager.getConnection(url,user,password)
5)连接PostgreSQL数据库
Class.forName("org.postgresql.Driver")
String url="JDBC:postgresql://localhost/testDb"
String user="test" String password="test"
Connection con=DriverManager.getConnection(url,user,password)
6)连接Access数据库
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver")
String url="JDBC:odbc:Driver={Microsoft Access Driver (*.mdb)}DBQ=" application.getRealPath("/Data/testDb/mdb")
Connection conn=DriverManager.getConnection(url,"","")
7连接Sybase数据库
Class.forName("com.sybase.JDBC.SybDriver")
String url="JDBC:sybase:Tds:localhost:5007/testDb"
Properties pro=System.getProperties()
pro.put("user","userId")
pro.put("password","user_password")
Connection con=DriverManager.getConnection(url,pro)
8连接informix数据库
Class.forName("com.informix.JDBC.ifxDriver")
String url="JDBC:informix-sqli:localhost:1533/testDb:INFORMIXSERVER=myserver"user=testUserpassword=testpassword" Connection con=DriverManager.getConnection(url)

示例:
连接SQL Server2008R2数据库
首先Build Path → 添加外部sqljdbc.jar驱动

import java.sql.*
public class DB {
public static void main(String[] args) throws Exception {
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver")
Connection conn = DriverManager.getConnection("jdbc:sqlserver://localhost:1433 DatabaseName=数据库名", "sa", "1234")
Statement stmt = conn.createStatement()
ResultSet rs = stmt.executeQuery("select * from 表名")
while(rs.next()) {
System.out.println("id为:" rs.getString("id") "name为:" rs.getString("name"))
}
System.out.println("数据库连接成功!")
rs.close()
stmt.close()
conn.close()
System.out.println("数据库成功关闭!")
}
}

MySql 中如何连接一列字符串

GROUP_CONCAT(expr)
完整句法如下: GROUP_CONCAT([DISTINCT] expr [,expr ...]
[ORDER BY {unsigned_integer | col_name | formula} [ASC | DESC] [,col ...]]
[SEPARATOR str_val])这个函数在 MySQL 4.1 中被加入。函数返回一个字符串结果,该结果由分组中的值连接组合而成: mysql> SELECT student_name,
-> GROUP_CONCAT(test_score)
-> FROM student
-> GROUP BY student_nameormysql> SELECT student_name,
-> GROUP_CONCAT(DISTINCT test_score
-> ORDER BY test_score DESC SEPARATOR " ")
-> FROM student
-> GROUP BY student_name在MySQL 中,你可以得到表达式结合体的连结值。通过使用 DISTINCT 可以排除重复值。如果希望对结果中的值进行排序,
可以使用 ORDER BY 子句。为了以倒序排序,可以在 ORDER BY 子句中用于排序的列名后添加一个 DESC (递减 descending) 关键词
。缺省为升序;这也可以通过使用 ASC 关键词明确指定。
SEPARATOR 是一个字符串值,它被用于插入到结果值中。缺省为一个逗号 (",")。
你可以通过指定 SEPARATOR "" 完全地移除这个分隔符。
在你的配置中,通过变量 group_concat_max_len 要以设置一个最大的长度。
在运行时执行的句法如下: SET [SESSION | GLOBAL] group_concat_max_len = unsigned_integer如果最大长度被设置,结果值被剪切到这个最大长度。
GROUP_CONCAT() 函数是一个增强的 Sybase SQL Anywhere 支持的基本 LIST() 函数。
如果只有一个列,并且没有其它选项被指定,GROUP_CONCAT() 是向后兼容有极大限制的 LIST() 函数。

最新文章